ios客户端一分钟即成友盟推送
2017-10-16 本文已影响23人
面皮大师
#pragma mark - 友盟推送
/**
友盟推送
*/
- (void)setupUMengPushWithOptions:(NSDictionary *)launchOptions{
//设置AppKey
[UMessage startWithAppkey:kAppKey_UMeng launchOptions:launchOptions];
//注册通知
[UMessage registerForRemoteNotifications];
//设置debug模式
[UMessage openDebugMode:YES];
//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 {
//点击不允许
}
}];
//for log
[UMessage setLogEnabled:YES];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
DLog(@"application didRegisterForRemoteNotificationsWithDeviceToken");
//1.2.7版本开始不需要用户再手动注册devicetoken,SDK会自动注册
//[UMessage registerDeviceToken:deviceToken];
NSString *tokenString = [self stringDevicetoken:deviceToken];
DLog(@"tokenString = %@",tokenString);
}
#pragma mark - iOS10以下使用这个方法接收通知
//ios10以下使用此方法推送
-(void)application:(UIApplication *)application didReceiveRemoteNotification:userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
//关闭友盟自带的弹出框
[UMessage setAutoAlert:NO];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
DLog(@"%@",userInfo);
UIApplicationState state = [application applicationState];
//关闭或者后台模式
if (state == UIApplicationStateInactive || state == UIApplicationStateBackground) {
//处理消息跳转
[self pushNotificationNavigation:userInfo];
} else {//前台模式
/////////////////////////////////////////////////////////////////////
////////////////////////震动////////////////////////
////////////////////////////////////////////////////////////////////
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
}
#pragma mark UNUserNotificationCenterDelegate
//iOS10新增:处理前台收到通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary *userInfo = notification.request.content.userInfo;
DLog(@"%@",userInfo);
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于前台时的远程推送接受
//关闭友盟自带的弹出框
[UMessage setAutoAlert:NO];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
/////////////////////////////////////////////////////////////////////
////////////////////////震动////////////////////////
////////////////////////////////////////////////////////////////////
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
} else {
//应用处于前台时的本地推送接受
}
}
//iOS10新增:处理后台点击通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
DLog(@"%@",userInfo);
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台时的远程推送接受
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
//处理消息跳转
[self pushNotificationNavigation:userInfo];
} else {
//应用处于后台时的本地推送接受
}
}
#pragma mark - 收到通知后的处理页面
- (void)pushNotificationNavigation:(NSDictionary *)userInfo{
UIViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject];
if ([homeVc isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)homeVc;
UIViewController *vc = [nav.viewControllers objectAtIndex:0 ];
MessageViewController *vcnext = MessageViewController.new;
vcnext.view.backgroundColor = [UIColor whiteColor];
[vc.navigationController pushViewController:vcnext animated:YES];
}
}
#pragma mark - 以下的方法仅作调试使用
-(NSString *)stringDevicetoken:(NSData *)deviceToken {
NSString *token = [deviceToken description];
NSString *pushToken = [[[token stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">"withString:@""]
stringByReplacingOccurrencesOfString:@" "withString:@""];
return pushToken;
}
-(NSString *)openUDID {
NSString* openUdid = nil;
if (openUdid==nil) {
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef cfstring = CFUUIDCreateString(kCFAllocatorDefault, uuid);
const char *cStr = CFStringGetCStringPtr(cfstring,CFStringGetFastestEncoding(cfstring));
unsigned char result[16];
CC_MD5( cStr,(CC_LONG)strlen(cStr), result );
CFRelease(uuid);
CFRelease(cfstring);
openUdid = [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%08lx",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15],
(unsigned long)(arc4random() % NSUIntegerMax)];
}
return openUdid;
}