推送相关代码

本文介绍了一个iOS应用如何处理从远程服务器接收到的通知。包括设备token的注册、接收通知的处理逻辑以及不同iOS版本间的适配策略。

//登录成功后调用注册deviceToken

-(void)registerNotification{

    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) {

                NSLog(@"succeeded!");

            }

        }];

    } else {

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound) categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    }

    [[UIApplication sharedApplication] registerForRemoteNotifications];

}

 

//收到通知后调用

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{

    if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)

    {

    }else{

    }

    completionHandler(UIBackgroundFetchResultNewData);

}

 

//ios10以后收到通知调用

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)){

    

    NSDictionary *userInfo = response.notification.request.content.userInfo;

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:NSJSONWritingPrettyPrinted error:nil];

    NSString *str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSLog(@"%@", str);

   if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive)

    {

    }else{

    }

}

 

//获取注册后的deviceToken

-(void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值