iOS本地推送
阿新 • • 發佈:2018-07-03
Nid 推送 self from system body ali require hour
通過iOS本地推送實現定時推送
- 先在appdelegate的didFinishLaunch方法中請求推送許可
if (@available(iOS 10.0, *)) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert+UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { }]; center. - 收到本地通知的處理
//iOS10之前收到本地通知
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSLog(@"receive local notifi%@",notification.userInfo); }
//iOS10以後,這個代理代表app在前臺的時候
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler NS_AVAILABLE_IOS(10){
NSLog(@"willPresentNotification local notifi%@",notification.request.content.userInfo);
//代表交給didReceiveNotificationResponse處理
completionHandler(UNNotificationPresentationOptionSound);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler NS_AVAILABLE_IOS(10){ NSLog(@"userNotificationCenter local notifi%@",response.notification.request.content.userInfo); if ([response.actionIdentifier isEqualToString:@"sure"]) { NSLog(@"notif sure"); }else if ([response.actionIdentifier isEqualToString:@"cancel"]){ NSLog(@"notif cancel"); } completionHandler(); }
設置本地推送
- (void)addAlarmIndentifier:(NSString*)requestIndetifier isRepeat:(BOOL)repeat{ NSDateComponents*cmp = [[NSCalendar currentCalendar] components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitWeekday fromDate:self.datepicker.date]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.hour = cmp.hour; components.minute = cmp.minute ; components.weekday = cmp.weekday; //通知聲音 NSString *soundName = @"soundName"; if (@available(iOS 10.0, *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = @"wake up!!"; content.body = @"time to massage" ; //類別,用於自定義通知動作 content.categoryIdentifier = @"alarm"; content.userInfo =@{@"name":@"jack"}; //通知聲音 content.sound = [UNNotificationSound soundNamed:soundName]; //觸發器repeats:代表是否重復 UNCalendarNotificationTrigger *trig = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:(repeat?YES:NO)]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIndetifier content:content trigger:trig]; //動作1 UNNotificationAction*action1 = [UNNotificationAction actionWithIdentifier:@"sure" title:@"sure" options:UNNotificationActionOptionForeground]; //動作2 UNNotificationAction*action2 = [UNNotificationAction actionWithIdentifier:@"cacel" title:@"cancel" options:UNNotificationActionOptionAuthenticationRequired]; //identifier:alarm UNNotificationCategory*category = [UNNotificationCategory categoryWithIdentifier:@"alarm" actions:@[action1,action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction]; //設置分類 [center setNotificationCategories: [NSSet setWithObject:category]]; //設置通知 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error) { NSLog(@"error===%@",error.localizedDescription); } }]; } else {//ios 10 以下 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm"; UILocalNotification *notification = [[UILocalNotification alloc] init]; //如果需要循環可以以過去的時間為標準,然後設置repeatInterval notification.fireDate = self.datepicker.date; notification.repeatCalendar = [NSCalendar currentCalendar]; notification.repeatInterval = repeat?NSCalendarUnitWeekOfYear:0; // // NSMinuteCalendarUnit notification.timeZone = [NSTimeZone systemTimeZone]; notification.alertBody = @"wake up!!!"; notification.soundName = soundName; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } }
取消通知(全部取消)
- (void)cancel{ if (@available(iOS 10.0, *)) { [[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications]; } else { [[UIApplication sharedApplication] cancelAllLocalNotifications]; } }
- 效果如下圖:
iOS本地推送