LoginSignup
21
24

More than 3 years have passed since last update.

iOS 13以上でサイレントプッシュのdidReceiveRemoteNotificationが呼ばれない問題

Last updated at Posted at 2019-10-24

didReceiveRemoteNotificationメソッドがiOS 13以上で呼ばれない理由

特定の手続きをしないと、iOS 13以上でサイレントプッシュを使っている時にAppDelegatedidReceiveRemoteNotificationが呼ばれないことがわかりました。

AppDelegate.swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    print("PUSH arrived.")
    completionHandler(.noData)
}

サーバー側でヘッダーにapns-push-type: backgroundを付けるか、もしくは以下のようにペイロードJSONのaps部分に空のalertを付けると、無事呼ばれるようになりました。

body.json
{
    "aps": {
        "alert": {},
        "content-available": 1
    }
}

WWDC2019で発表された正式な方法らしい前者が理想ですが、もしサーバーがカスタムヘッダーをサポートしていない場合は後者のほうが簡単です。

注意点

ヘッダーにapns-push-type: alertを明示的につけていると、ペイロードJSONの内容に関わらずサイレントプッシュとみなされなくなり、didReceiveRemoteNotificationが呼ばれないらしいので、気をつけましょう。

参考文献

21
24
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
24