码迷,mamicode.com
首页 > 移动开发 > 详细

iOS集成友盟推送

时间:2017-03-10 22:08:11      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:前台   ict   info   erro   elb   present   hand   start   amp   

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

 

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

#1. 注册一个App ID

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

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

#4. 注意友盟账号

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

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

 

在AppDelegatez

#import "UMessage.h"

 

添加一个属性

@property (nonatomic, strong) NSDictionary *userInfo;

 

添加协议:

@interface AppDelegate ()<UNUserNotificationCenterDelegate>

 

设置友盟AppKey

static NSString *UMessageAppKey          = @"112345678901234523";

 

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

- (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 {
                                      //点击不允许
                                      //这里可以添加一些自己的逻辑
                                      
                                  }
                                  
                              }];
    }
}

 

协议方法:

- (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中调用配置友盟推送的方法即可

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //配置友盟推送
    [self configureUMessageWithLaunchOptions:launchOptions];
    
    
    return YES;
}

 

iOS集成友盟推送

标签:前台   ict   info   erro   elb   present   hand   start   amp   

原文地址:http://www.cnblogs.com/zhouxihi/p/6533058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!