百度推送的那些坑
首先先代碼
第一步
- (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"]];
}
}
當然除了這些程序里面還有一些需要配置的也是必須的
EDCBE7FD-F688-4B85-9D8C-A44D38F171EF.png
38CFB325-8714-4B9B-9292-3F2D15C5E3A6.png