發現他真是個不可多得的好物阿!
跟remote push 結合在一起,可以一直提醒使用者"快來用我 快來用我",
再進化下去,可以在收到推撥的時候放一段2秒的音樂
以下code全部來自這邊
http://blog.csdn.net/alexjames_83/article/details/7481750
註冊一個Notification
- UILocalNotification *localNotification = [[UILocalNotification alloc] init];
- // 設置notification的屬性
- localNotification.fireDate = [startTimePicker.picker.date dateByAddingTimeInterval:36000]; //出發時間
- localNotification.alertBody = @"Time To Schedule Our Service"; // 消息內容
- localNotification.repeatInterval = NSSecondCalendarUnit; // 重複的時間間隔
- localNotification.soundName = UILocalNotificationDefaultSoundName; // 觸發消息時播放的聲音
- localNotification.applicationIconBadgeNumber = 1; //應用程序Badge數目
- //設置隨Notification傳遞的參數
- NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"reminder", @"notificationId", @"phone", txtPhone.text, nil];
- localNotification.userInfo = infoDict;
- [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //註冊
- [localNotification release]; //釋放
遍歷已經註冊的所有LocaleNotification
- NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
- for (UILocalNotification *notification in notifications ) {
- if( [[notification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] ) {
- [[UIApplication sharedApplication] cancelLocalNotification:notification];
- break;
- }
- }
撤銷LocaleNotification
- [[UIApplication sharedApplication] cancelAllLocalNotifications]; // 撤銷所有的Notification
- <pre name="code" class="cpp">[[UIApplication sharedApplication] cancelLocalNotification:notification]; // 撤銷某個Notificiation,若要刪除某個特定的Notification,則可以在UserInfo中加入標記,遍歷所有的Notification來刪除。</pre>
- <pre></pre>
- <p></p>
- <pre></pre>
- <strong></strong>
- <p></p>
- <p></p>
- <p><strong>響應事件</strong></p>
- <p>當Notification被觸發後,你的應用需要對此作出反應。應用此時可能會處於以下幾個狀態:</p>
- <ul>
- <li>在前台運行 - 當應用在前台運行時,則ApplicationDelegate的didReceiveLocalNotification會被調用。<pre name="code" class="cpp">- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
- NSLog(@"Notification Body: %@", notification.alertBody);
- NSLog(@"%@", notification.userInfo);
- application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
- }</pre><br>
- </li><li>在後台運行 - 用戶可以看到類似Push Notification的提醒,若用戶選擇查看提醒詳情,則應用通過 ApplicationDelegate的didFinishLaunchingWithOptions進入<br>
- <pre name="code" class="cpp">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- UILocalNotification *localNotification =
- [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
- if (localNotification) {
- NSLog(@"Notification Body: %@",localNotification.alertBody);
- NSLog(@"%@", localNotification.userInfo);
- application.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber-1;
- }
- // set up everything else
- return YES;
- </pre>}<br>
- </li></ul>
沒有留言:
張貼留言