- (void)registerPush{ float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue]; if(sysVer < 8){ [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; }else{ #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:[NSSet setWithObject:categorys]]; [[UIApplication sharedApplication] registerUserNotificationSettings:userSettings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; #endif } } #pragma mark 应用程序启动之后 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self registerPush]; }
#pragma mark 注册推送通知之后 //在此接收设备令牌,发送给服务器 -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ [self addDeviceToken:deviceToken]; NSLog(@"device token:%@",deviceToken); } #pragma mark 获取device token失败后 -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"didFailToRegisterForRemoteNotificationsWithError:%@",error.localizedDescription); [self addDeviceToken:nil]; } #pragma mark 接收到推送通知之后 -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"receiveRemoteNotification,userInfo is %@",userInfo); }
2.信鸽推送
1.首先在信鸽网站进行注册appid
2.开始代码的初始化工作
1.第一次初始化
// 信鸽推送 [XGPush startApp:kXGPush_Id appKey:kXGPush_Key]; [Login setXGAccountWithCurUser]; //注销之后需要再次注册前的准备 @weakify(self); void (^successCallback)(void) = ^(void){ //如果变成需要注册状态 if(![XGPush isUnRegisterStatus] && [Login isLogin]){ @strongify(self); [self registerPush]; } }; [XGPush initForReregister:successCallback];
+ (void)setXGAccountWithCurUser{
if ([self isLogin]) {
User *user = [Login curLoginUser];
if (user && user.global_key.length > 0) {
NSString *global_key = user.global_key;
[XGPush setAccount:global_key];
[(AppDelegate *)[UIApplication sharedApplication].delegate registerPush];
}
}else{
[XGPush setAccount:nil];
[XGPush unRegisterDevice];
}
}
#pragma mark - XGPush Message - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString * deviceTokenStr = [XGPush registerDevice:deviceToken]; DebugLog(@"deviceTokenStr : %@", deviceTokenStr); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { DebugLog(@"didReceiveRemoteNotification-userInfo:-----\n%@", userInfo); [XGPush handleReceiveNotification:userInfo]; [BaseViewController handleNotificationInfo:userInfo applicationState:[application applicationState]]; }
浙公网安备 33010602011771号