美文网首页
iOS10_极光推送_两个新方法区别

iOS10_极光推送_两个新方法区别

作者: Lxin_ | 来源:发表于2017-09-27 16:27 被阅读15次

    iOS极光推送的集成问题,不用多做介绍了吧,极光集成文档写的挺详细的,有需要的可以移步极光文档

    现在介绍两个在极光文档含糊不清的问题,iOS10下接收推送的两个方法:

    //方法1
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler;
    //方法2
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler;
    

    对以上两个方法,极光文档并没有写具体使用方法,只是简单提到了iOS10新增加了这两个方法。具体这两个方法有什么区别呢?经过一天的钻研,今天直接介绍这两个方法区别及使用吧。

    • 方法1为 通知的展示,此方法控制通知的展示,当app在前台、后台、退出时怎么展示通知,是只显示角标,声音,还是弹框。
      具体使用如下:
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
        NSDictionary *userInfo = notification.request.content.userInfo;
        //APNs推送 与 应用内消息 皆为UNPushNotificationTrigger类
        if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            /*
             app状态,三种状态。
             UIApplicationStateActive,
             UIApplicationStateInactive,
             UIApplicationStateBackground
             */
            /*
             极光推送消息展示方式,三种方法
             UNNotificationPresentationOptionBadge,//角标
             UNNotificationPresentationOptionSound,//声音
             UNNotificationPresentationOptionAlert,//弹框
             */
            if([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
                completionHandler(UNNotificationPresentationOptionSound);   //选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
                //此时app在前台运行,弹出一个alert,展示推送内容
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                message:[NSString stringWithFormat:@"%@",userInfo[@"aps"][@"alert"]]
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil,nil];
                [alert show];
            }else {
                [JPUSHService handleRemoteNotification:userInfo];           //处理收到的 APNs 消息
                completionHandler(UNNotificationPresentationOptionAlert);   //选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
            }
        }else {
            //本地通知
        }
    }
    

    其中
    ** [UIApplication sharedApplication].applicationState == UIApplicationStateActive有三种状态可选,分别为UIApplicationStateActive,UIApplicationStateInactive,UIApplicationStateBackground
    ** completionHandler(UNNotificationPresentationOptionAlert);有三种状态可选,分别为UNNotificationPresentationOptionBadge,UNNotificationPresentationOptionSound,UNNotificationPresentationOptionAlert,

    • 方法2为通知的触发,此方法为点击Alert时触发的方法。
      具体使用方法如下:
    - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
        // Required
        NSDictionary * userInfo = response.notification.request.content.userInfo;
        if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
            [JPUSHService handleRemoteNotification:userInfo];
        }
        completionHandler();
        
        if([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
            
        }else {
            //这里是app未运行或者在后台,通过点击手机通知栏的推送消息打开app时可以在这里进行处理
            [self performSelector:@selector(presentViewControllerWithPushInfo:) withObject:userInfo afterDelay:1.0f];
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS10_极光推送_两个新方法区别

          本文链接:https://www.haomeiwen.com/subject/impuextx.html