1

I am planning to implement silent notifications via FCM in my iOS app. I found many articles and SO posts describing how to achieve this. But my question stems from the fact that I found this code snippet in the FCM documentation. Please note the first line of the comment.

func application(_ application: UIApplication,
                 didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)
                   -> Void) {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired till the user taps on the notification launching the application.
  // TODO: Handle data of notification

  // With swizzling disabled you must let Messaging know about the message, for Analytics
  // Messaging.messaging().appDidReceiveMessage(userInfo)

  // Print message ID.
  if let messageID = userInfo[gcmMessageIDKey] {
    print("Message ID: \(messageID)")
  }

  // Print full message.
  print(userInfo)

  completionHandler(UIBackgroundFetchResult.newData)
}

This code is provided under the heading Handle silent push notifications. As per my understanding, silent notifications do not appear on the screen or alert the user in any way, no matter in what state the app is in. Then why is there a mention about tapping the notification ?

My intention is to send a silent notification to the iOS device and perform some background task without any user interaction. The limitations of background notifications are acceptable. As I do not currently have the permissions to a apple developer account to test this out, could any one tell me if this can be achieved with FCM ? Or should I consider using APNS directly ?

humblePilgrim
  • 1,818
  • 4
  • 25
  • 47
  • When you use `application(_:didReceiveRemoteNotification:fetchCompletionHandler:)`, the app will show a notification. When the user taps it, the app will not respond. – El Tomato Oct 14 '21 at 05:01
  • So it is not possible to implement silent notifications without user interaction via FCM ? But it would be possible with APNS ? – humblePilgrim Oct 14 '21 at 05:17
  • If you want user interaction such that the app will launch itself when a notification is tapped, you need a different method under `AppDelegate`. – El Tomato Oct 14 '21 at 05:19
  • No, I do not need user interaction. Neither do I want the app to launch. I do need to gain some execution time in the background. Specifically start off a long running background upload task. – humblePilgrim Oct 14 '21 at 05:19
  • Actually I read silent notification do not present any alerts or sounds. Not sure why it should be different in case of FCM – humblePilgrim Oct 14 '21 at 05:21

1 Answers1

1

The system calls this method when your app is running in the foreground or background, thats why the comment is there. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

You can also achive by using FCM To send a background notification, create a remote notification with an aps dictionary that includes only the content-available key, as shown bellow. You may include custom keys in the payload, but the aps dictionary must not contain any keys that would trigger user interactions.

{
   "aps" : {
      "content-available" : 1
   },
   "acme1" : "bar",
   "acme2" : 42
}

The comment inside the code snippet is old and wrong see the apple latest explanation about the method

enter image description here

Jayesh Patel
  • 938
  • 5
  • 16