ios local notification

来源:互联网 发布:梅雨知时节小说集 编辑:程序博客网 时间:2024/05/19 22:47

做了很多有Push Notification和Locale Notification的app,来整理下开发的笔记。今天先说说Local Notification

在iOS4.0后Apple加入了Local Notification。这里是NSLocaleNotification的Class Reference http://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html

如何注册一个Notification

[cpp] view plaincopy
  1. UILocalNotification *localNotification = [[UILocalNotification alloc] init];  
  2.   
  3. // 设置notification的属性  
  4. localNotification.fireDate = [startTimePicker.picker.date dateByAddingTimeInterval:36000]; //出发时间  
  5. localNotification.alertBody = @"Time To Schedule Our Service"// 消息内容  
  6. localNotification.repeatInterval = NSSecondCalendarUnit; // 重复的时间间隔  
  7. localNotification.soundName = UILocalNotificationDefaultSoundName; // 触发消息时播放的声音  
  8. localNotification.applicationIconBadgeNumber = 1; //应用程序Badge数目  
  9.   
  10. //设置随Notification传递的参数  
  11. NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"reminder", @"notificationId", @"phone", txtPhone.text, nil];  
  12. localNotification.userInfo = infoDict;  
  13.       
  14. [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; //注册  
  15. [localNotification release]; //释放  

遍历已经注册的所有LocaleNotification

[cpp] view plaincopy
  1.     NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];  
  2.     for (UILocalNotification *notification in notifications ) {  
  3.         if( [[notification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] ) {  
  4.             [[UIApplication sharedApplication] cancelLocalNotification:notification];  
  5.             break;  
  6.         }  
  7.     }  

撤销LocaleNotification

[cpp] view plaincopy
  1. [[UIApplication sharedApplication] cancelAllLocalNotifications]; // 撤销所有的Notification  
  2. <pre name="code" class="cpp">[[UIApplication sharedApplication] cancelLocalNotification:notification]; // 撤销某个Notificiation,若要删除某个特定的Notification,则可以在UserInfo中加入标记,遍历所有的Notification来删除。</pre>  
  3. <pre></pre>  
  4. <p></p>  
  5. <pre></pre>  
  6. <strong></strong>  
  7. <p></p>  
  8. <p></p>  
  9. <p><strong>响应事件</strong></p>  
  10. <p>当Notification被触发后,你的应用需要对此作出反应。应用此时可能会处于以下几个状态:</p>  
  11. <ul>  
  12. <li>在前台运行 - 当应用在前台运行时,则ApplicationDelegate的didReceiveLocalNotification会被调用。<pre name="code" class="cpp">- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {  
  13.     NSLog(@"Notification Body: %@", notification.alertBody);  
  14.     NSLog(@"%@", notification.userInfo);  
  15.     application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;  
  16. }</pre><br>  
  17. </li><li>在后台运行 - 用户可以看到类似Push Notification的提醒,若用户选择查看提醒详情,则应用通过 ApplicationDelegate的didFinishLaunchingWithOptions进入<br>  
  18. <pre name="code" class="cpp">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  19.     UILocalNotification *localNotification =  
  20.     [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];  
  21.     if (localNotification) {  
  22.         NSLog(@"Notification Body: %@",localNotification.alertBody);  
  23.         NSLog(@"%@", localNotification.userInfo);  
  24.         application.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber-1;  
  25.     }  
  26.     // set up everything else  
  27.     return YES;  
  28. </pre>}<br>  
  29. </li></ul>  
原创粉丝点击