iOS推送消息处理

注册远程推送

极光推送跟着极光推送官方的文档集成就行,使用控制台测试是否收到通知,如果收不到,可以先检查远程通知是否注册成功。

所有苹果设备,在联网状态下都会与苹果的服务器建立长连接,调用[application registerUserNotificationSettings],如果注册成功会调用application:didRegisterForRemoteNotificationsWithDeviceToken方法,发送苹果设备的deviceToken给苹果服务器。

-(void)application:didRegisterForRemoteNotificationsWithDeviceToken:

这里可以打印远程推送是否注册失败

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError - %@", error);
}

接收远程推送消息通知

接收到的通知可以在这个方法里打印并操作

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // 这里处理userInfo,接收到的推送消息

   if (userInfo) {
       
   }
    completionHandler(UIBackgroundFetchResultNewData);
}

处理点击远程推送的通知

当用户点击远程推送通知时,会自动打开APP,这里有2种情况:
1> App并没有关闭,一直隐藏在后台

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // 这里处理userInfo,接收到的推送消息

   if (userInfo) {
       
       NSLog(@"收到了推送消息");
       
   } 
    completionHandler(UIBackgroundFetchResultNewData);
}

2> App被杀死

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   // 判断launchOptions是否有推送消息 跳转到对应的界面
   NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

   if (userInfo) {
       
       NSLog(@"收到了推送消息");
       
   } else {
       
       NSLog(@"没有收到推送消息,从icon点击进来的");
   }
   return YES;
}

获取用户在【iPhone - 设置 - 通知 - AppName】是否允许通知的开关状态


if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if ( setting.types == UIUserNotificationTypeNone)  {
            NSLog(@"推送关闭");
        }else{
            NSLog(@"推送打开");
        }
    } else {
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if(type == UIRemoteNotificationTypeNone){
            NSLog(@"推送关闭");
        }else{
            NSLog(@"推送打开");
        }
    } 

你可能感兴趣的:(iOS推送消息处理)