Warm tip: This article is reproduced from stackoverflow.com, please click
firebase firebase-cloud-messaging ionic-framework ios push-notification

Push notifications via Firebase not appearing on iOS with app on background

发布于 2020-04-03 23:47:56

We are developing an Ionic app for Android and iOS that has Push Notifications. We send these notifications via Firebase Cloud Messaging

On iOS we have this problem (we have accepted Notifications Permission for the app):

  • When the app is in foreground, we get the notification instantly (we get it inside the app, not with a card).
  • When the app is closed or in background it seems like we do not get the notification. However wehn we open or resume the app, we get the notification as is it was recieved with the app in foreground. Our hypothesis is that we are getting silent notifications.
  • (When we send Firebase test notification we get the same behaviour).

We have the next configurations:

  • Xcode: Capabilities -> Background Modes -> Remote Notifications (shows one check)
  • Xcode: Capabilities -> Push Notifications (shows two checks)
  • Apple Developer: Identifiers -> my app -> Push Notifications (with certificates for both dev and prod)
  • Apple Developer: Keys -> .p8 key with "Apple Push Notifications service (APNs)"
  • Firebase: my app -> Configuration -> Cloud Messaging (.p8 file uploaded with key and team IDs)

This is one of the several notifications we have tried to send via Firebase POST API, which combines almost everything we have read while researching this problem. (we also send required Firebase HTTP headers)

{
   "to": "<firebase_token>",
   "notification": {
      "body": "NOTIFICATION BODY",
      "title": "NOTIFICATION TITLE",
   },
   "apns": {
      "headers": {
        "apns-push-type": "alert",
        "apns-expiration": 0,
        "apns-priority": 5,
        "apns-topic": "<my_app>"
      },
      "payload": {
         "alert": {
            "title": "NOTIFICATION TITLE",
            "body": "NOTIFICATION BODY"
         },
         "aps": {
            "content-available": 1,
            "alert": {
                "title": "NOTIFICATION TITLE",
                "body": "NOTIFICATION BODY"
             }
         },
         "sound": "default",
         "content-available": 1
      }
   },
   "data": {
      "field": "1",
      "type": "CHAR"
   }
}

Despite this POST request may be incorrect for bacground notifications, we believe that would not be the problem, as Firebase test notifications also fail to show when on background.

Questioner
Antonio Parra
Viewed
84
Antonio Parra 2020-01-08 00:31

We finally were able to get notifications with app on background and on foreground.

The payload for Firebase is:

{
   "to": "<firebase token>",
   "notification": {
      "title": "TITLE",
      "body": "BODY"
   },
   "data": {
      "title": "TITLE",
      "body": "BODY",
      "extraField1": "extra value 1",
      "extraField2": "extra value 2"
   },
   "apns": {
      "headers": {
         "apns-topic": "<my-app>",
         "apns-push-type": "background",
         "apns-priority": 10
      }
   }
}

It looked like there were an problem with .p12 certificates after updating to iOS 13 that we solved using a .p8 cert. After changing the certificates at Firebase the background notifications started to be recieved, but foreground stopped for the first hours.