前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS集成友盟推送

iOS集成友盟推送

作者头像
周希
发布2019-10-15 11:45:56
2.1K0
发布2019-10-15 11:45:56
举报
文章被收录于专栏:APP自动化测试APP自动化测试

之前有写过利用Python自己写一个推送服务器, 今天说下如果集成友盟的推送服务

在这之前我们需要做一些准备动作

#1. 注册一个App ID

#2. Enable Push NotificationSerivice, 并创建和下载开发/发布推送证书

#3. 安装推送证书, 然后把推送证书导出为p12文件

#4. 注册友盟账号

#5. 创建一个推送应用, 并上传推送证书的p12文件和填写密码

#6. 下载SDK, 添加到项目中

在AppDelegatez

代码语言:javascript
复制
#import "UMessage.h"

添加一个属性

代码语言:javascript
复制
@property (nonatomic, strong) NSDictionary *userInfo;

添加协议:

代码语言:javascript
复制
@interface AppDelegate ()<UNUserNotificationCenterDelegate>

设置友盟AppKey

代码语言:javascript
复制
static NSString *UMessageAppKey          = @"112345678901234523";

创建一个配置友盟推送的方法

代码语言:javascript
复制
- (void)configureUMessageWithLaunchOptions:(NSDictionary *)launchOptions {
    
    //设置AppKey & LaunchOptions
    [UMessage startWithAppkey:UMessageAppKey launchOptions:launchOptions];
    
    //初始化
    [UMessage registerForRemoteNotifications];
    
    //开启log
    [UMessage setLogEnabled:YES];
    
    //检查是否为iOS 10以上版本
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
        
    } else {
        
        //如果是iOS 10以上版本则必须执行以下操作
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        
        center.delegate                = self;
        
        UNAuthorizationOptions types10 = \
        UNAuthorizationOptionBadge | UNAuthorizationOptionAlert |UNAuthorizationOptionSound;
        
        [center requestAuthorizationWithOptions:types10
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                  
                                  if (granted) {
                                      //点击允许
                                      //这里可以添加一些自己的逻辑
                                      
                                  } else {
                                      //点击不允许
                                      //这里可以添加一些自己的逻辑
                                      
                                  }
                                  
                              }];
    }
}

协议方法:

代码语言:javascript
复制
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //关闭友盟自带的弹出框
    [UMessage setAutoAlert:NO];
    
    [UMessage didReceiveRemoteNotification:userInfo];
    
    self.userInfo = userInfo;
    //定制自定的的弹出框
    if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
                                                            message:self.userInfo[@"aps"][@"alert"]
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        
    }
}

//iOS10新增:处理前台收到通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        
        //应用处于前台时的远程推送接受
        //关闭友盟自带的弹出框
        [UMessage setAutoAlert:NO];
        //必须加这句代码
        [UMessage didReceiveRemoteNotification: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.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        
        //应用处于后台时的远程推送接受
        //必须加这句代码
        [UMessage didReceiveRemoteNotification:userInfo];
        
    }else{
        
        //应用处于后台时的本地推送接受
    }
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    [UMessage sendClickReportForRemoteNotification:self.userInfo];
}

最后是在ApplicationDidFinishLaunch中调用配置友盟推送的方法即可

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //配置友盟推送
    [self configureUMessageWithLaunchOptions:launchOptions];
    
    
    return YES;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档