iOS 推送問題全解答《十萬個爲啥吖?》 頂 原 薦

Q 1:爲啥收不到推送(1)?

如果收到推送時,App 在前臺運行,那麼:

  • iOS 10 before 頂部橫幅不會彈出。沒有任何展示,你以爲「沒有收到推送」。
  • iOS 10 after 如果沒有實現以下代碼,也是不會有任何提示的,你以爲「沒有收到推送」。
//UNUserNotificationCenterDelegate
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}

Q 2:爲啥收不到推送(2)?

iOS 使用推送需要 配置推送證書,如果你配過了,仍然收不到推送,建議再配一遍。筆者曾經多次遠程控制手把手給其他開發者配置證書,結果全都重新配一遍就能收到推送了。

Q 3:爲啥收不到推送(3)?

Xcode 8 這裏打開。

Q 4:爲啥收不到推送(4)?

推送時開發環境、發佈環境一一對應。環境不對應,收不到。

Q 5:爲啥收不到推送(5)?

要在 Apple Developer Center 把你的測試設備加入到 Device 裏面。

Q 6:爲啥收不到推送(6)?

一臺手機能收到,另一臺不能收到。要把你的另一臺測試設備也加入到 Apple Deveice 裏面。。。

Q 7:爲啥收不到推送(7)?

高峯時段你再等幾秒就收到了。

Q 8:爲啥還是收不到推送(8)???

Apple 服務器宕機了,並不是所有的設備都收不到推送了,而是新的設備無法成功註冊推送服務了。

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
}

在該方法中,無法返回有效的 deviceToken 值了。等兩天就好了。

Q 9:爲啥生產環境收不到推送?

打包,安裝,測試。即可。

Q 10:爲啥 App 右上角的角標 badge 不清 0?

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Q 11:爲啥 App 右上角的角標 badge 不自增?

推送時 badge 參數設置: +n自增、-n自減、n固定值。 自增、自減一般第三方推送服務支持纔有。

Q 12:爲啥 App 在後臺收到推送代碼不執行?

Background Remote Notification

Q 13:爲啥 App 在殺死後收到推送代碼不執行?

App 殺死啥代碼都不能執行。iOS 10 你可以試試 Notification Service Extension,詳情見 這篇文章 的 Service Extension 部分。

Q 14:爲啥收到推送沒聲音(1)?

哥哥手機左側靜音鍵麻煩撥上來。

Q 15:爲啥收到推送沒聲音(2)?

推送時 sound 字段設置爲:default

Q 16:爲啥收到推送不播放自定義聲音(1)?

1.聲音文件拖拽並拷貝並生成索引到工程任意位置(在 Xcode 裏拖拽) 2.推送時 sound 字段設置爲:name.mp3。需要名字後綴完全一樣。

Q 17:爲啥收到推送不播放自定義聲音(2)?

拖拽文件時沒有選擇拷貝,連線測試有聲音,拔線沒聲音,聲音文件在電腦裏,拔線後缺失。從工程目錄刪除該文件,再重新添加再 build。

Q 18:爲啥點擊 App 圖標進入 App 收不到推送(1)?

要點推送橫幅、通知中心條目進入 App 才能收到。

Q 19:爲啥點擊 App 圖標進入 App 收不到推送(2)?

解決方法:再發一條透傳消息。只處理消息,不處理推送。不論點哪裏進的 App,都能保證該事件被處理。

Q 20:爲啥推送一條手機會收到兩條一樣的?

iOS 9 的 bug。

Q 21:爲啥點擊橫幅不能跳轉到指定的界面?

  • 點擊推送啓動 App 在這裏捕獲推送內容:
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
      NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
}
  • 點擊推送從後臺喚醒 App 在這裏捕獲推送內容:
    • iOS 7 before
   - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
}
  • iOS 7~9
   - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
}
  • iOS 10
//UNUserNotificationCenterDelegate
   - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
       NSDictionary *userInfo = response.notification.request.content.userInfo;
}

Q 22:爲啥點擊了推送,通知中心條目不消除(1)?

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Q 23:爲啥點擊了推送,通知中心條目不消除(2)?

推送時 badge 參數爲 0,再調用 Q 22 導致通知中心沒有發生變化,此時應設爲1,再設爲0:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Q 24:爲啥用戶不允許通知,以後再也不能彈出請求通知權限的提示窗了?

先獲取用戶推送權限設置情況:

//  iOS 8 before
  UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

//  iOS 8~9
  UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
  UIUserNotificationType type = settings.types;

//  iOS 10
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  }

如果沒開,跳轉到 設置 - yourApp 頁面讓用戶設置:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

Q 25:爲啥 iOS 10 不能註冊推送服務?

//  iOS 8 before
UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:type];

//  iOS 8~9
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:type categories:nil];
[application registerUserNotificationSettings:setting];

//  iOS 10
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (!error) {
      NSLog(@"request authorization succeeded!");
    }
}];

Q 26:爲啥樓主懂的這麼多?

因爲他是極光推送的可耐程序猿葛格^^。

作者:pikacode - 極光

原文:iOS 推送問題全解答《十萬個爲啥吖》

知乎專欄:極光日報

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章