iOS 本地推送 UILocalNotification

来源:互联网 发布:nginx laravel 编辑:程序博客网 时间:2024/06/11 19:43

创建通知

[objc] view plain copy
  1. UILocalNotification *notification = [[UILocalNotification alloc] init];  
  2.     if (notification)  
  3.     {  
  4.         NSDate *now = [NSDate new];  
  5.         notification.fireDate = [now dateByAddingTimeInterval:10]; //10秒后通知  
  6.         notification.repeatInterval=0//重复次数,kCFCalendarUnitWeekday一周一次  
  7.         notification.timeZone = [NSTimeZone defaultTimeZone]; //设置时区  
  8.         notification.applicationIconBadgeNumber = 1//应用的角标  
  9.         notification.soundName = UILocalNotificationDefaultSoundName; //声音,可以换成alarm.soundName = @"sound.wav"  
  10.         //去掉下面2行就不会弹出提示框  
  11.         notification.alertBody = @"通知内容"//提示信息 弹出提示框  
  12.         notification.alertAction = @"打开"//提示框按钮  
  13.         //notification.hasAction = NO; //是否显示额外的按钮,为no时alertAction消失  
  14.         NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];  
  15.         //设置userinfo 方便在之后需要撤销的时候使用 也可以传递其他值,当通知触发时可以获取  
  16.         notification.userInfo = infoDict;  
  17.           
  18.         [[UIApplication sharedApplication] scheduleLocalNotification:notification];  
  19.     }  


推送的内容

[objc] view plain copy
  1. //推送的内容  
  2. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification  
  3. {  
  4.   
  5.   
  6.     //这里,你就可以通过notification的useinfo,干一些你想做的事情了  
  7.     if ([[notification.userInfo objectForKey:@"key"] isEqualToString:@"value"])  
  8.     {  
  9.            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示" message:@"你的系统存在严重威胁" delegate:nil cancelButtonTitle:@"关闭" otherButtonTitles:nil,nil];  
  10.            [alert show];  
  11.           
  12.     }  
  13.     application.applicationIconBadgeNumber = 0//移除角标  
  14. }  

[objc] view plain copy
  1. - (void)applicationDidBecomeActive:(UIApplication *)application  
  2. {  
  3.     //不通过推送 通过应用图标打开应用 移除角标  
  4.     application.applicationIconBadgeNumber = 0;  
  5. }  

取消通知

[objc] view plain copy
  1. //获取当前所有的本地通知  
  2.     NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];  
  3.     if (!notificaitons || notificaitons.count <= 0)  
  4.     {  
  5.         return;  
  6.     }  
  7.     //取消一个特定的通知  
  8.     for (UILocalNotification *notify in notificaitons)  
  9.     {  
  10.         if ([[notify.userInfo objectForKey:@"key"] isEqualToString:@"value"])  
  11.         {  
  12.             [[UIApplication sharedApplication] cancelLocalNotification:notify];  
  13.             break;  
  14.         }  
  15.     }  
  16.       
  17. //    //取消所有的本地通知  
  18. //    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
0 0
原创粉丝点击