iOS推送(一):本地推送

以iOS8.0和iOS10.0两个版本来创建一个本地推送:

- (void)localPush{
  // iOS8.0下本地推送的创建方式
   if ([UIDevice currentDevice].systemVersion.floatValue < 10.0) {
     UILocalNotification *localNotification = [[UILocalNotification alloc] init];
     localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
     localNotification.alertBody = @"这是一条本地推送,这是推送主体!"; 
     localNotification.soundName = UILocalNotificationDefaultSoundName;
     localNotification.applicationIconBadgeNumber += 1; 
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
     // iOS10.0 
   }else {
  // 给推送一个标识符
  NSString *identifier = @"localPush.identifier"; 
  // 推送内容
  UNMutableNotificationContent *content = [UNMutableNotificationContent new];
  content.body = @"这是iOS10的本地推送,这是推送主体!";
  content.badge = @1; 
   // 推送触发器 
   // UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
   NSDateComponents *components = [NSDateComponents new]; components.second = 5.0f; 
   UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES]; 
 // 推送请求
   UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; 
 // 把推送请求添加到推送中心
  [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
       NSLog(@"%@", [error localizedDescription]);
   }];
}}

推送创建完毕后,那么我们便要考虑收到推送后会执行什么样的操作,这里主要介绍处理接收到推送的地方。首先我们要先注册推送通知。8.0和10.0注册推送还是很不一样的。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 8.0系统注册推送
     if ([[UIDevice currentDevice].systemVersion floatValue] < 10.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:settings];
  // iOS10.0注册推送
  }else{
    [UNUserNotificationCenter currentNotificationCenter].delegate = self; 
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
   if (error != nil) {
       NSLog(@"%@", [error localizedDescription]);
   }
 }]; 
       [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
       NSLog(@"%@", settings);
   }];
 } 
   // 如果是点击推送进入的App,那么lauchOptions会有一个UIApplicationLaunchOptionsLocalNotificationKey字段,如果这个字段不为空,代表是点击推送进入,并且可以获得推送的内容。注意这个key在iOS10以后也有值,不要和userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:这个方法重复了。所以要判断一下版本是否小于10.0。
   if (([[UIDevice currentDevice].systemVersion floatValue] < 10.0) && launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
   // ......在点击推送进入App后会执行的代码。
   } 
  return YES;
}

iOS10以后,如果程序被销毁后(即程序退出后),收到推送,点击推送进入app,会调用该方法;在该方法里可以执行需要执行的代码,注意两点:

一.要调用一下completionHandler()
二.要在

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

中设置一下代理:
[UNUserNotificationCenter currentNotificationCenter].delegate = self;

否则该代理方法不会执行。

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
 //......程序退出时收到推送执行的操作
 completionHandler();
}

iOS8.0以后,如果程序在前台,收到推送会调用该方法:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ 
  //......程序在前台时收到推送执行的操作
}

iOS10.0以后,如果程序在前台,收到推送会调用该方法:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
     //......程序在前台时收到推送执行的操作 
    completionHandler(UNNotificationPresentationOptionSound |  UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}

你可能感兴趣的:(iOS推送(一):本地推送)