• APNs推送的系统做法


    1、

    #pragma mark - 远程推送注册获得device Token
    if (IOS_VERSION >= 10.0) {
        
        UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
        [center setDelegate:self];
        UNAuthorizationOptions type = UNAuthorizationOptionBadge | UNAuthorizationOptionSound |UNAuthorizationOptionAlert;
        
        [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
            
            if (granted) {
                NSLog(@"注册成功");
            }else{
                NSLog(@"注册失败");
            }
            
        }];
        
    }else if (IOS_VERSION >= 8.0){
        
        UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound |
    UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes
    categories:nil];
        [application registerUserNotificationSettings:settings];
        
    }else{
        //ios8以下不适配了
    }
    // 注册获得device Token
    [application registerForRemoteNotifications];

    2、

    #pragma mark - 远程推送注册获得device Token
    // 将得到的deviceToken传给SDK
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {
        NSString *deviceTokenStr = [[[[deviceToken description]
                                     stringByReplacingOccurrencesOfString:@"<" withString:@""]
                                     stringByReplacingOccurrencesOfString:@">" withString:@""]
                                     stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        NSLog(@"deviceTokenStr:
    %@",deviceTokenStr);
    }
    
    // 注册deviceToken失败
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
    {
        NSLog(@"注册deviceToken失败error -- %@",error);
    }

    3、

    #pragma mark - 远程推送消息的处理 - iOS10以后
    //在前台
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
    {
        // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
        completionHandler(UNNotificationPresentationOptionBadge |
                          UNNotificationPresentationOptionSound |
                          UNNotificationPresentationOptionAlert);
        
    }
    
    //在后台、关闭状态
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
    {
        //处理推送过来的数据
        NSLog(@"%@", response.notification.request.content.userInfo);
        completionHandler();
    }
    
    #pragma mark - 远程推送消息的处理 - iOS10之前
    //在前台、在后台、关闭状态统一进入该方法
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary * _Nonnull)userInfo fetchCompletionHandler:(void (^ _Nonnull)(UIBackgroundFetchResult))completionHandler{
        
        /*
         UIApplicationStateActive 应用程序处于前台
         UIApplicationStateBackground 应用程序在后台,用户从通知中心点击消息将程序从后台调至前台
         UIApplicationStateInactive 用用程序处于关闭状态(不在前台也不在后台),用户通过点击通知中心的消息将客户端从关闭状态调至前台
         */
        
        //应用程序在前台给一个提示特别消息
        if (application.applicationState == UIApplicationStateActive) {
            
            //应用程序在前台
            NSLog(@"应用程序在前台%@", userInfo);
            
        }else{
            
            //其他两种情况,一种在后台程序没有被杀死,另一种是在程序已经杀死。用户点击推送的消息进入app的情况处理。
            NSLog(@"应用程序在后台、关闭状态%@", userInfo);
            
        }
        
        completionHandler(UIBackgroundFetchResultNewData);
        
    }
  • 相关阅读:
    swagger 接口文档,控制器 和 object类型的参数与返回值 的 注释不显示问题
    python学习——练习题(13)
    python学习——练习题(12)
    女生生日祝词
    python学习——练习题(11)
    python学习——练习题(10)
    python学习——练习题(9)
    python学习——练习题(8)
    python学习——练习题(7)
    python学习——练习题(6)
  • 原文地址:https://www.cnblogs.com/cchHers/p/8712301.html
Copyright © 2020-2023  润新知