玩 iOS 友盟推送

作者: HH思無邪 | 来源:发表于2018-05-25 22:10 被阅读194次

    1.准备app推送证书,证书创建

    钥匙串访问->证书助理->从证书颁发机构申请

    2.上Account Developer 创建推送证书

    image

    2.1先创建应用,在创建推送证书

    image image

    2.2 创建 有推送功能的开发或者发布证书

    2.3 下载证书 —》再安装证书 —导出证书—等待上传到友盟

    3.友盟创建应用

    image

    4.项目配置

    image

    5.下载sdk集成

    http://dev.umeng.com/sdk_integate/ios-integrate-guide/common

    image

    6.代码

    
     #import  <UMCommon/UMCommon.h>  // 公共组件是所有友盟产品的基础组件,必选 
    
    #import  <UMPush/UMessage.h>  // Push组件
    
    #import  <UserNotifications/UserNotifications.h>// Push组件必须的系统库
    
    #define UMAppKey @“5b06be93f43e485fae000077"
    
    @interface AppDelegate ()<UNUserNotificationCenterDelegate>
    
    
    
    - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
    
     // 配置友盟SDK产品并并统一初始化
    
        [UMConfigureinitWithAppkey:UMAppKeychannel:@"App Store"];
    
     // Push组件基本功能配置
    
       [UNUserNotificationCentercurrentNotificationCenter].delegate= self;
    
     UMessageRegisterEntity* entity = [[UMessageRegisterEntityalloc] init];
    
     //type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标等
    
        entity.types= UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert;
    
        [UNUserNotificationCentercurrentNotificationCenter].delegate= self;
    
        [UMessageregisterForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOLgranted, NSError* _Nullableerror) {
    
     if(granted) {
    
     // 用户选择了接收Push消息
            }else{
     // 用户拒绝接收Push消息
            }
        }];
     // Override point for customization after application launch.
     returnYES;
    
    }
    

    pragma -mark ———————————————————友盟推送代理————————————————————

    //iOS10以下使用这两个方法接收通知,

    
    -(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
    
    {
        [UMessagesetAutoAlert:NO];
     //统计点击数
       [UMessagedidReceiveRemoteNotification:userInfo];
     if([[[UIDevicecurrentDevice] systemVersion]intValue] < 10){
            [UMessagedidReceiveRemoteNotification:userInfo];
     //    self.userInfo = userInfo;
     //    //定制自定的的弹出框
     //    if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
     //    {
     //        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题"
    
     //                                                            message:@"Test On ApplicationStateActive"
    
     //                                                           delegate:self
    
     //                                                  cancelButtonTitle:@"确定"
    
     //                                                  otherButtonTitles:nil];
     //
     //        [alertView show];
     //
     //    }
            completionHandler(UIBackgroundFetchResultNewData);
        }
    }
    

    //iOS10新增:处理前台收到通知的代理方法

    
    -(void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{
    
     NSDictionary* userInfo = notification.request.content.userInfo;
    
     if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
    
     //应用处于前台时的远程推送接受
    
     //关闭U-Push自带的弹出框
           [UMessagesetAutoAlert:NO];
     //(前台、后台)的消息处理
            [UMessagedidReceiveRemoteNotification:userInfo];
        }else{
     //应用处于前台时的本地推送接受
        }
     //当应用处于前台时提示设置,需要哪个可以设置哪一个
    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
    }
    

    //iOS10新增:处理后台点击通知的代理方法

    
    -(void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler{
    
     NSDictionary* userInfo = response.notification.request.content.userInfo;
    
     if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
    
     //应用处于后台时的远程推送接受
    
     //(前台、后台)的消息处理
            [UMessagedidReceiveRemoteNotification:userInfo];
     if(userInfo.count>0){
      //消息处理
                NSLog(@"跳转到你想要的");
              }
         }
     else{ //应用处于后台时的本地推送接受
        }
    }
    

    //打印设备注册码,需要在友盟测试设备上自己添加deviceToken

    
    - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
    
    {
    
        [UMessageregisterDeviceToken:deviceToken];
    
     NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken success");
    
     NSLog(@"deviceToken————>>>%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<"withString: @""]
    
     stringByReplacingOccurrencesOfString: @">"withString: @""]
    
     stringByReplacingOccurrencesOfString: @" "withString: @""]);
    
    }
    
    

    //放上推送成功截图😁😁

    image

    别名推送

    我的项目需求是登录成功后绑定别名,退出登录是移除别名

    /*
    * test:一般用用户ID,后台返回的用户唯一标识
    *UMENGTEST:类型随便填,要和后台的类型一样,这点要注意
    */
    //绑定别名
    [UMessage addAlias:@"test@umeng.com" type:@"UMENGTEST" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
    }];
    //移除别名
    [UMessage removeAlias:@"test@umeng.com" type:@"UMENGTEST" response:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
    }];
    

    还未上线的应用只能在测试环境收到推送,所以要是后台把环境改为正式环境收不到消息不要慌,要是真想在未上线也能收到正式环境推送,那么就搞个正式环境的来玩呗。

    • 用AD Hoc方式打包项目,上传蒲公英进行下载安装,就可以收到正式环境的推送啦。
    • AD HOC 打包教程

    相关文章

      网友评论

      • 59f3c2e351d3:写得不错!可是为什么appkey不打马赛克,万一被别人乱推送怎么办:joy:
        HH思無邪:@冬天ing 没经验 谢啦
      • IT人故事会:太感谢了,写的不错!
        HH思無邪:@IT人故事会 才开始写简书,记录下学习,感谢支持

      本文标题:玩 iOS 友盟推送

      本文链接:https://www.haomeiwen.com/subject/rviyjftx.html