美文网首页
适配iOS10(友盟推送)

适配iOS10(友盟推送)

作者: CocoaJason | 来源:发表于2016-11-26 13:47 被阅读458次

1.在友盟头文件中加入版本判断 否则在低版本中会失效

#if __IPHONE_OS_VERSION_MAX_ALLOWED >=100000

#import

#endif

2.在APPdelegate中加入iOS10通知代理#if __IPHONE_OS_VERSION_MAX_ALLOWED >=100000

@interfaceAppDelegate()

@end

#endif

3.注册友盟及推送

-(void)initUMessage:(NSDictionary*)launchOptions

{

//设置AppKey及LaunchOptions

[UMessagestartWithAppkey:UMessageAppKeylaunchOptions:launchOptions];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >=100000

//如果要在iOS10显示交互式的通知,必须注意实现以下代码

if([[[UIDevicecurrentDevice]systemVersion]intValue]>=10)

{

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

UNUserNotificationCenter*center = [UNUserNotificationCentercurrentNotificationCenter];

center.delegate=self;

UNAuthorizationOptionstypes10=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;

[centerrequestAuthorizationWithOptions:types10completionHandler:^(BOOLgranted,NSError*_Nullableerror) {

if(granted) {

//点击允许

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

}else{

//点击不允许

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

}

}];

}

#endif

//1.3.0版本开始简化初始化过程。如不需要交互式的通知,下面用下面一句话注册通知即可。

[UMessageregisterForRemoteNotifications];

[UMessagesetLogEnabled:YES];

}

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

{

#if!TARGET_IPHONE_SIMULATOR

[UMessageregisterDeviceToken:deviceToken];

NSString*pushToken = [[[[deviceTokendescription]

stringByReplacingOccurrencesOfString:@"<"withString:@""]

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

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

[httpUtilsSET_deviceToken:pushToken];

#endif

}

-(void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error

{

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

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

}

4.处理推送(判断是不是大于10)

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

{

//关闭友盟自带的弹出框

[UMessagesetAutoAlert:NO];

[UMessagedidReceiveRemoteNotification:userInfo];

}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >=100000

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

-(void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{

NSDictionary* userInfo = notification.request.content.userInfo;

if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {

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

//必须加这句代码

[UMessagesetAutoAlert:NO];

[UMessagedidReceiveRemoteNotification:userInfo];

}else

{

//应用处于前台时的本地推送接受

;

}

}

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

-(void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler{

NSDictionary* userInfo = response.notification.request.content.userInfo;

if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {

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

//必须加这句代码

[UMessagesetAutoAlert:YES];

[UMessagedidReceiveRemoteNotification:userInfo];

}else{

//应用处于后台时的本地推送接受

}

}

#endif

相关文章

网友评论

      本文标题:适配iOS10(友盟推送)

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