简体   繁体   中英

Do FCM silent push notifications (Background push) in iOS work as defined by Apple?

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 ?

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM