百度推送的那些坑
首先先代码
第一步
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions //在这个方法中写
// iOS10 下需要使用新的 API
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound + UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
if (granted) {
[application registerForRemoteNotifications];
}
}];
#endif
}
else
{
UIUserNotificationType myTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
第二步
// 注册推送成功后调用该方法
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[BPush registerDeviceToken:deviceToken];
[BPush bindChannelWithCompleteHandler:^(id result, NSError *error)
{
NSString *deviceTokenString = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<"withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *myChannel_id = [BPush getChannelId];
[[NSUserDefaults standardUserDefaults] setObject:myChannel_id forKey:@"myChannel"];
[[NSUserDefaults standardUserDefaults] setObject:deviceTokenString forKey:@"deviceToken"];
[[NSUserDefaults standardUserDefaults] synchronize];
[DeviceInfoService requestConfig];
}];
}
// 注册推送失败
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
}
ios10之前用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[BPush handleNotification:userInfo];
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground)
{
[[MessageService sharedMessageService] requestRecentMsgList];
}
else
{
[self pushDic:userInfo[@"jm"]];
}
}
ios10之后
// 此方法是 用户点击了通知,应用在前台 或者开启后台并且应用在后台 时调起
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
completionHandler(UIBackgroundFetchResultNewData);
// 应用在前台,不跳转页面,让用户选择。
if (application.applicationState == UIApplicationStateActive)
{
[[MessageService sharedMessageService] requestRecentMsgList];
}
// 应用在后台。当后台设置aps字段里的 content-available 值为 1 并开启远程通知激活应用的选项
if (application.applicationState == UIApplicationStateBackground)
{
// 此处可以选择激活应用提前下载邮件图片等内容。
isBackGroundActivateApplication = YES;
[[MessageService sharedMessageService] requestRecentMsgList];
}
//杀死状态下,直接跳转到跳转页面。
if (application.applicationState == UIApplicationStateInactive && !isBackGroundActivateApplication)
{
[self pushDic:userInfo[@"jm"]];
}
}
当然除了这些程序里面还有一些需要配置的也是必须的