ios10百度推送收不到

百度推送的那些坑
首先先代碼
第一步

- (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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,288評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,923評論 18 139
  • 寫作原因:網上看了很多推送文章都沒有完美的解答我的疑惑;主要有以下兩點,1:推送來了我點擊應用圖標進入應用怎么取到...
    Thebloodelves閱讀 4,567評論 26 71
  • 有一種愛叫放手;有一種愛叫陪伴;有一種愛叫等待;有一種愛叫母愛.....耳熟能詳的還有,有一種愛叫父愛、奶奶的愛、...
    半夏拾光閱讀 2,233評論 1 3
  • 乞丐不一定會妒忌百萬富翁,但可能會妒忌收入更高的乞丐。沒有更高的視野,你會糾結于現在的圈子;有了更高的視野,你會把...
    盞茶輕茗閱讀 229評論 0 1