IOS集成友盟推送

一.证书:

1.先去苹果官网申请推送证书,安装到本地后

需要导出推送调试和发布P12到友盟后台

IOS集成友盟推送_第1张图片

二.代码:

- (void)startUMessageWith:(NSDictionary *)launchOptions{

[UMessage startWithAppkey:UMessageKey launchOptions:launchOptions httpsEnable:true];

//注册通知,如果要使用category的自定义策略,可以参考demo中的代码。

[UMessage registerForRemoteNotifications];

//iOS10必须加下面这段代码。

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

center.delegate=self;

UNAuthorizationOptions types10=UNAuthorizationOptionBadge|  UNAuthorizationOptionAlert|UNAuthorizationOptionSound;

[center requestAuthorizationWithOptions:types10    completionHandler:^(BOOL granted, NSError * _Nullable error) {

if (granted) {

//点击允许

//这里可以添加一些自己的逻辑

} else {

//点击不允许

//这里可以添加一些自己的逻辑

}

}];

[UMessage setLogEnabled:YES];

[UMessage openDebugMode:YES];

}

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

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

[UMessage didReceiveRemoteNotification: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];

//

//    }

}

//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]]) {

//应用处于前台时的远程推送接受

//关闭U-Push自带的弹出框

[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)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{

NSString *error_str = [NSString stringWithFormat: @"%@", error];

NSLog(@"Failed to get token, error:%@", error_str);

}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

{

[UMessage registerDeviceToken:deviceToken];

// 1.2.7版本开始不需要用户再手动注册devicetoken,SDK会自动注册

MYLog(@"UmengDeviceToken-%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]

stringByReplacingOccurrencesOfString: @">" withString: @""]

stringByReplacingOccurrencesOfString: @" " withString: @""]);

}



三.调试

坑的地方在于,安装好后去umeng后台添加设备,测试环境测试单播,如果设备无效,多卸载重装几遍,是黑色就可以了

IOS集成友盟推送_第2张图片

如果看到注册成功了,说明设备有效了

IOS集成友盟推送_第3张图片
IOS集成友盟推送_第4张图片

你可能感兴趣的:(IOS集成友盟推送)