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
{
//設(shè)置AppKey及LaunchOptions
[UMessagestartWithAppkey:UMessageAppKeylaunchOptions:launchOptions];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >=100000
//如果要在iOS10顯示交互式的通知,必須注意實現(xiàn)以下代碼
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
{
//關(guān)閉友盟自帶的彈出框
[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]]) {
//應(yīng)用處于前臺時的遠(yuǎn)程推送接受
//必須加這句代碼
[UMessagesetAutoAlert:NO];
[UMessagedidReceiveRemoteNotification:userInfo];
}else
{
//應(yīng)用處于前臺時的本地推送接受
;
}
}
//iOS10新增:處理后臺點擊通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler{
NSDictionary* userInfo = response.notification.request.content.userInfo;
if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
//應(yīng)用處于后臺時的遠(yuǎn)程推送接受
//必須加這句代碼
[UMessagesetAutoAlert:YES];
[UMessagedidReceiveRemoteNotification:userInfo];
}else{
//應(yīng)用處于后臺時的本地推送接受
}
}
#endif