0

I'm trying to use silent notifications with FCM to trigger my iOS app to download new data from the database. I've defined my payload and options as such:

var payload = {
  notification: {
    title: "DataSync",
    body: "DataSync"
  }
};

var options = {
    contentAvailable: true,
    priority: "high"
};

I also have the following code in my didFinishLaunchingWithOptions

    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        FIRMessaging.messaging().remoteMessageDelegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

I need to make sure that data downloading works even when the app is in the background or closed. In order to do that, I need to trigger performFetchWithCompletionHandler from didReceiveRemoteNotification. However, only applicationReceivedRemoteMessage is being called right now (which only gets called when app is in foreground) even though I've defined my payload to be a notification, not a data message.

Update: Additional Info

When I use NWPusher to send a notification directly through APNs, if the app is in the foreground, the following method from UNUserNotificationCenterDelegate is called.

userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

And if the app is in the background, the following method is called:

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

However, these methods are not called when sending a silent notification through FCM admin sdk. Only applicationReceivedRemoteMessage is called.

My question is: 1. How do I get didReceiveRemoteNotification to be called so I that I can download data? 2. How should I write my code in performFetchWithCompletionHandler to retrieve information from the Firebase database and ensure that the data has finished downloading? Is it just a regular Firebase query?

abcd123
  • 77
  • 11
  • The value of `content_available` is a boolean, not a string. And it belongs at the top level of `payload`, not in `notification`. Example here: https://stackoverflow.com/a/44729283/4815718 – Bob Snyder Jun 25 '17 at 03:13
  • I think you may be referencing an older version of the API? When I try that, I get the error: "Messaging payload contains an invalid "content_available" property. Valid properties are "data" and "notification". Here's the link to the current documentation: https://firebase.google.com/docs/cloud-messaging/admin/send-messages – abcd123 Jun 25 '17 at 08:33
  • More specifically: https://firebase.google.com/docs/reference/admin/node/admin.messaging.MessagingOptions#contentAvailable content_available is supposed to be defined in options, but that did not fix the issue for me – abcd123 Jun 25 '17 at 09:03
  • You're right. I failed to recognize that you are using the admin API. Update your post to show what your payload and options look like now. In options you should have `contentAvailable: true`. – Bob Snyder Jun 25 '17 at 12:40
  • Just updated the post and also included some additional info after testing with NWPusher. – abcd123 Jun 26 '17 at 01:52
  • I can't offer anything more. I know Android FCM but almost nothing about iOS. I was looking at this [related question](https://stackoverflow.com/a/37621274/4815718) about "silent notifications". It seems like you need to put your payload in `data`, not `notification`. – Bob Snyder Jun 26 '17 at 05:55
  • have you figured it out ? @abcd123 . i am also facing the same problem . i am sending notification through FCM message console , and in that i am sending contentAvailable: true to make it silent but i get normal notification only which user has to tapped on to make app launch and then it enters into the method didReceiveRemoteNotification . but it not happening in background like silent notification . what should i do to send silent notification which would make the app active in background mode ? – Moxarth Dec 11 '17 at 13:01

0 Answers0