SwiftUI/Firebase Silent Notification Construction/Handling

481 Views Asked by At

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.

0

There are 0 best solutions below