1 // 注册 发送通知的方法 2 -(void)pushNotfation{ 3 4 5 //--------------初始化本地通知 alloc init 虽然是UI控件 但继承NSObject 6 UILocalNotification *not = [[UILocalNotification alloc]init]; 7 // 设置本地通知启动的时间 8 not.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; 9 // 设置通知的标题 10 not.alertTitle = @"开始工作了。。。"; 11 // 设置通知的内容 12 not.alertBody = @"起床做码农了,,,,hahhhhh"; 13 // 通过通知 传递 内容--传值 14 not.userInfo = @{@"key":@"Value"}; 15 // 设置App图标上面红点的 显示的数字 16 not.applicationIconBadgeNumber = 1; 17 //******************************************************************* 18 // 设置重复发送通知的时间间隔 19 /* 20 NSCalendarUnitEra = kCFCalendarUnitEra,一个世纪 21 NSCalendarUnitYear = kCFCalendarUnitYear, 一年 22 NSCalendarUnitMonth = kCFCalendarUnitMonth, 一个月 23 NSCalendarUnitDay = kCFCalendarUnitDay, 天 24 NSCalendarUnitHour = kCFCalendarUnitHour, 时 25 NSCalendarUnitMinute = kCFCalendarUnitMinute,分 26 NSCalendarUnitSecond = kCFCalendarUnitSecond,秒 27 NSCalendarUnitWeekday = kCFCalendarUnitWeekday, 一个礼拜 28 NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal, 29 */ 30 // not.repeatCalendar = kCFCalendarUnitEra; 31 //******************************************************************* 32 33 //——----------------注册通知 34 //****** 35 // UIUserNotificationTypeNone 36 // UIUserNotificationTypeBadge 37 // UIUserNotificationTypeSound 38 // UIUserNotificationTypeAlert 振动 39 //****** 40 // respondsToSelector 方法是否可以响应 41 if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) { 42 // 解决 43 [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert categories:nil]]; 44 45 } 46 // 设置声音 47 not.soundName = UILocalNotificationDefaultSoundName; 48 49 // 发送通知 50 [[UIApplication sharedApplication]scheduleLocalNotification:not]; 51 52 } 53 54 55 56 //本地移除 57 -(void)removeLocalPushNotification:(UIButton*)sender 58 { 59 NSLog(@"%s",__FUNCTION__); 60 UIApplication* app=[UIApplication sharedApplication]; 61 //获取当前应用所有的通知 62 NSArray* localNotifications=[app scheduledLocalNotifications]; 63 64 if (localNotifications) { 65 66 for (UILocalNotification* notification in localNotifications) { 67 68 NSDictionary* dic=notification.userInfo; 69 70 if (dic) { 71 NSString* key=[dic objectForKey:@"key"]; 72 if ([key isEqualToString:@"name"]) { 73 //取消推送 (指定一个取消) 74 [app cancelLocalNotification:notification]; 75 76 break; 77 } 78 } 79 80 } 81 } 82 //取消当前应用所有的推送 83 //[app cancelAllLocalNotifications]; 84 85 86 }
下一篇博客将会详细讲述--远程通知
时间: 2024-07-31 14:22:43