0

I am hoping to get some help on how to correctly send/receive a silent Firebase Push Notification in SwiftUI. I have read several similar threads, each with unique recommendations for the special sauce needed to structure silent push notifications correctly. I think that I have now tried every possible combination of ContentAvailable, content_available, content-available, "apns-priority": 5, "priority": "high", pray_to_the_Push_Gods: true, etc recommended in the various threads. I have also tried sending messages with an API tester, Curl, and Google Cloud Functions.

In each case, I can always get a normal message to send, and it will correctly deliver data payloads. In each case, I can never get a silent message to send. I have Remote and Background capabilities enabled. Low Data Mode on the testing device is off.

For example, posting

{
  "content_available": true,
  "apns-priority": 5,
  "notification": {
    "title": "Not Silent",
  },
  "data": {
    "sampleData": "7777"
  },
  "to": "[my_FCM_token]",
}

delivers fine to the device, and tapping the notification executes the appropriate code to display my sample data.

Message ID: 1628420135470598
Updating userID to: 8888
[AnyHashable("sampleData"): 8888, AnyHashable("gcm.message_id"): 1628420135470598, AnyHashable("aps"): {
    alert =     {
        title = "Not Silent";
    };
    "content-available" = 1;
}, AnyHashable("google.c.sender.id"): 298615355806, AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.fid"): cJqpr3rMAkqMqffuvL4LXe]

However, if I post:

{
  "content_available": true,
  "apns-priority": 5,
  "notification": {
    "empty": "body",
  },
  "data": {
    "sampleData": "5555"
  },
  "to": "[my_FCM_token]",
}

I get a successful completion (either using POST or by using the associated CURL code), but there is no evidence that the silent push is received by my message handler.

func application(_ application: UIApplication,
                   didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)
                     -> Void) {

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Silent Message ID: \(messageID)")
    }
    
    if let sampleData = userInfo["sampleData"] as? String {
        userID = sampleData
        print ("Updating userID to: \(sampleData)")
    }

    // Print full message.
    print("Silent Message: \(userInfo)")

    completionHandler(UIBackgroundFetchResult.newData)
  }

I have also tried omitting "notification": { "empty": "body", }, from the push entirely, but with no luck.

Given everything that I have tried, I can't figure out if I'm doing something wrong with the way silent messages are configured, or if I've done something wrong with the receive/handle side of things in Xcode.

Any help/direction is much appreciated.

Swifter
  • 67
  • 6
  • It's not a solution, but make sure your dictionary doesn't end with the comma. – El Tomato Aug 07 '21 at 23:50
  • What's this? "to": "[my_FCM_token]" – El Tomato Aug 08 '21 at 03:12
  • @ElTomato — [my_FCM_token] is just a placeholder in my message post for the Firebase device token. `"to": "cJqpr3r...NPnnJ"`. Also, removed comma(s) per your suggestion. Non-silent messages still send fine; silent messages...no luck still. – Swifter Aug 08 '21 at 11:12
  • Also open to any workaround with regular notifications with data payloads. Currently, I can only act on regular notifications if 1) the app is in the foreground when the notification arrives (via `willPresent notification: UNNotification` extension), or 2) when the user taps the notification (via `didReceive response: UNNotificationResponse`). However, if the user opens the app from the Home Screen (without tapping the notification), the payload is ignored. – Swifter Aug 08 '21 at 11:53
  • @Swifter any luck with this one? I have the same issue – makis.k Aug 01 '22 at 09:51

0 Answers0