iOS本地通知和远程推送

iOS 本地通知和远程推送

推送通知的应用,可以推送最新的消息给用户,获得更多的关注。
推送分为本地推送和远程推送两种。并且在 iOS 8.0之后做了相关的改变。iOS 8.0之后不管是远程推送还是本地推送,都需要执行一个注册方法。

// 通知类型,显示未读信息标志, 显示提示通知,播放声音。
UIUserNotificationType notificationType =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

// 设置通知类型和执行动作
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type                                                             categories:nil];

// 注册通知
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

在调用此方法之后, 若是第一次调用注册,则会弹出是否允许此应用发送通知。并会调用 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 方法。
在此方法中做操作。

本地通知

创建本地通知

- (void)sendLocalNotifi { 
// 添加本地通知
     UILocalNotification *localNoti = [[UILocalNotification alloc] init];
     NSDate *Date = [NSDate date];
     localNoti.fireDate = Date; // 发送通知时间 这里没做具体处理,若是要推送时间无误差,时间要精确到秒。
     localNoti.timeZone = [NSTimeZone localTimeZone];     // 设置时区 默认时区
     localNoti.repeatInterval = NSCalendarUnitSecond;     // 发送通知的间隔
     localNoti.alertTitle = @"通知";                      // 弹窗title
     localNoti.alertBody = @"起来搬砖了!!";               // 弹窗body显示内容

     localNoti.soundName = UILocalNotificationDefaultSoundName;
     localNoti.alertLaunchImage = @"1.jpg"; // 用于点击启动显示启动页,必须是 UILaunchImageFile
     localNoti.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"value", @"key", nil];
     //    localNoti.soundName = @"1";                       // 响铃音乐文件名称, 放在main bundle 里边
      localNoti.applicationIconBadgeNumber = 2;         // app 的未读消息个数
          

     [[UIApplication sharedApplication] cancelAllLocalNotifications];
     [[UIApplication sharedApplication] scheduleLocalNotification:localNoti];    // 按照指定时间发送通知
     // [[UIApplication sharedApplication] presentLocalNotificationNow:localNoti];  // 立即发送通知

    
 }

在注册回调方法中

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
   // 判断若 types 不为 UIUserNotificationTypeNone 这可以添加本地通知
  if (notificationSettings.types != UIUserNotificationTypeNone) {
     [self sendLocalNotifi]; // 当然设置本地推送的方法不一定要要放在这里。 这仅仅是个例子。
   }
}

以上获得通知为最基本的通知,提示作用, 若是要有比如回复等操作, 则需要添加相应的 action
方法。

// 添加动作
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
action1.identifier = @"1";       // 标示用于区分点击的哪个按钮。
action1.title = @"OK";
action1.activationMode = UIUserNotificationActivationModeForeground; 
// UIUserNotificationActivationModeForeground   在前台处理事件,则在处理此事件时,系统会把 app 切换到前台。
// UIUserNotificationActivationModeBackground   在后台处理事件,点击事件在后台处理,不需要使app 切换到前台状态,
action1.destructive = NO;
action1.authenticationRequired = NO;
action1.behavior = UIUserNotificationActionBehaviorDefault; 
/*
UIUserNotificationActionBehaviorTextInput // 输入框样式
UIUserNotificationActionBehaviorDefault   // 默认样式
*/


UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
    action2.identifier = @"2";
action2.title = @"cacel";
action2.activationMode = UIUserNotificationActivationModeForeground; 
action2.destructive = NO;       // 
action2.authenticationRequired = NO;

// 设置扩展 Categories 
    UIMutableUserNotificationCategory *catergory = [[UIMutableUserNotificationCategory alloc] init];

catergory.identifier = @"localCatergory"; // 设置识别的标示符

[catergory setActions:@[action1, action2] forContext:UIUserNotificationActionContextMinimal];
[catergory setActions:@[action2, action1] forContext:UIUserNotificationActionContextDefault];

// 别忘了给添加的本地通知添加扩展的识别符
   - (void)sendLocalNotifi { 
    ................
    localNoti.category = @"localCatergory";

   }
 
// 在第一步注册的时候别忘了 categories 
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
                                               categories:[NSSet setWithObjects:catergory, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];   

注册完成,回调方法。当app处于活跃状态的时候, 会调用以下代理方法- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification ,开着的状态可以调用此方法进行处理 。

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
 {
   NSDictionary *infoDic = notification.userInfo;
   NSLog(@"%@", infoDic);
 }

若是在后台或者关闭app的状态,如下图所示,这不同的点击方法则会走不同的方法。

  • 若点击对应的动作这回调用 - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler 方法对不同的按钮分别进行判别,以进行不同的处理。
    此方法为iOS8.0 方法, 若是iOS 9.0 之后,可以执行- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler方法进行判别处理相应的操作。但别忘了在方法最后执行 completionHandler(); 以告诉系统已经处理完毕。
    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler {
        if ([identifier isEqualToString:@"accept_action"]) {
    NSLog(@"-----accept action");
         } else if ([identifier isEqualToString:@"cancel_action"]) {
         NSLog(@"-----cancel action");
         }

        if (completionHandler) {
        completionHandler();
        } 
     }
  • 若点击显示信息的区域,未点击 action 按钮, 若app只是在后台,系统则会把app 转化为前台状态,并在- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 方法中启用操作, 但若是app是关闭状态, 这系统 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 初始化方法中,在 launchOptions 中通过键值对获取到对应的消息。

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
          
          UILocalNotification * localNotify = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
       return YES;
      }
    
  • 取消本地通知

     // 取消所有本地通知
     [[UIApplication sharedApplication] cancelAllLocalNotifications];
     [[UIApplication sharedApplication] cancelLocalNotification:localNoti];
    
远程推送

远程推送的 Action 和 Categories 以及 Settings 与上边的设置一样。
不一样的地方在于接受的方法。

远程推送,所有消息大小不超过2KB,我们获取远程推送的json格式的消息,解析这个消息就是我们的远程推送了;

在注册的回调方法中,

  - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
   if (notificationSettings.types != UIUserNotificationTypeNone) {
      [application registerForRemoteNotifications];
      }  
  }

在获取远程推送成功之后,APNs 服务器会返回一个 deviceToken,如果是自己家的服务器,则需要获取到相应的 deviceToken 返回到自身的服务器,以方便后台的服务器做推送使用。若是用第三方的,则需要包 deviceToken 发送给第三方。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// 注册远程通知成功之后,会接收到 APNs 服务器的 deviceToken
}

获取 deviceToken 失败调用, 可以重新发送注册远程推送请求。

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
// 获取 device token 失败, 则会回调此方法,并报出错误
  if(error){
   [application registerForRemoteNotifications];
  }
}

以下几个方法和本地通知方法类似

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// 接收到远程通知信息
}

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
//在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮
    
    completionHandler();
 }
  
// iOS 9.0 之后的方法, 和上边的方法类似,但多个参数更方便操作
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler {

completionHandler();
}

// 若是点击重新启动app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
         
           NSDictionary   * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
      return YES;
      
     }
  • 如何实现远程推送的时候,设置Categories,以便系统找到对应的Categories来实现 action 操作。这需要在推送的消息格式中添加相应的字段来识别。当 推送消息到达用户时, iOS能识别这个类别的键值对,并查找到与之对应的注册的通知,并显示相应的操作, 添加的字段如下:

      {
      "aps" :  {
          "alert" : "You’re invited!",
          "category" : "INVITE_CATEGORY"
           }
      }

你可能感兴趣的:(iOS本地通知和远程推送)