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

2017-09-27  本文已影响15人  Lxin_

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新增加了这两个方法。具体这两个方法有什么区别呢?经过一天的钻研,今天直接介绍这两个方法区别及使用吧。

- (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,

- (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];
    }
}
上一篇下一篇

猜你喜欢

热点阅读