apns推送相关

2018-12-27  本文已影响6人  jameiShi

apns 推送

-(void)registerUserinfoNoti
{
    if (@available(iOS 10.0, *)) {        
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        //必须写代理,不然无法监听通知的接收与点击事件
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (!error && granted) {
                //用户点击允许
                NSLog(@"iOS10注册通知成功");
                // 可以通过 getNotificationSettingsWithCompletionHandler 获取权限设置
                //之前注册推送服务,用户点击了同意还是不同意,以及用户之后又做了怎样的更改我们都无从得知,现在 apple 开放了这个 API,我们可以直接获取到用户的设定信息了。注意UNNotificationSettings是只读对象哦,不能直接修改!
                [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                    //                NSLog(@"========%@",settings);
                    if(settings.authorizationStatus!=UNAuthorizationStatusAuthorized){
                        //用户没有授权或拒绝权限
                    }
                }];
            }else{
                //用户点击不允许
                NSLog(@"iOS10注册通知失败");
                //提示用户是否打开或设备支持授权
            }
        }];                
    } else {
        // Fallback on earlier versions
        if (@available(iOS 8.0, *)) {
            //iOS8+
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];            
        }
    }
    //iOS8+,包括iOS10+都是需要这个方法,才能获取
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 分发事件
    [self registerUserinfoNoti];   
    return YES;
}

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    NSLog(@"Romote Notification = %@", notificationSettings);
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    RedLog(@"Remote Notification Error = %@", error.description);
    NSString *pushToken = [[MMCPushService sharedInstance] generatePushToken:nil];
    [[MMCPushService sharedInstance] savePushToken:pushToken];
    
    [[MMCPushService sharedInstance] connectPushServer];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token=[NSString stringWithFormat:@"%@",deviceToken];
    token=[token stringByReplacingOccurrencesOfString:@"<" withString:@""];
    token=[token stringByReplacingOccurrencesOfString:@">" withString:@""];
    token=[token stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    // TODO: 如果用户授权,我们上传DeviceToken, 用户没授权也能获取DeviceToken,但收不到通知,
    // 也上传DeviceToken,用户中途开启可以获取到通知。  也就是说只要能获取到DeviceToken就上传,
    // 不管用户是否授权
    NSString *pushToken = [[MMCPushService sharedInstance] generatePushToken:token];
    [[MMCPushService sharedInstance] savePushToken:pushToken];
    RedLog(@"DeviceToken = %@",pushToken);

    [[MMCPushService sharedInstance] updateDeviceToken:pushToken completionBlock:^(NSError *error, BOOL isSuccessed) {        
    }];
    
    [[MMCPushService sharedInstance] connectPushServer];
}
//iOS10+在前台模式下接受消息,正常推送
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center
        willPresentNotification:(UNNotification *)notification
          withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler API_AVAILABLE(ios(10.0))  {
     //    UNNotificationContent *content = notification.request.content;//通知消息体内容  title subtitle  body
     if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]
         ]) {
         //远程通知
         //       [[WFURIManager shared] runActionWithString:@"fcar2017://WFInsuranceListViewController"];
     } else {
         //本地通知
     }
     //MARK:解析推送的消息,并做处理和跳转   completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
     //这里是有点问题 的,如果用户关闭通知,但是角标不会消失,
     [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
     
 }

// iOS10.0 从后台进入的时候回调,无论App进程是否存在
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)) {
     [[MMCPushService sharedInstance] handleReceiveRemoteNotification:response.notification.request.content.userInfo];
    completionHandler();
}

// iOS10.0 以下在后台, TODO:这个方法在前台居然能收到推送.iOS10.3
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"Remote Notification Info = %@", userInfo);
    
    if([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
       // return;//前台推送不接受
    } else {
    }
    
    [[MMCPushService sharedInstance] handleReceiveRemoteNotification:userInfo];
}

上一篇 下一篇

猜你喜欢

热点阅读