firebase-ios-sdk: [Messaging] didReceiveRemoteNotification does not working in background

Environment

  • Xcode version: 9.0
  • Firebase SDK version: FBSDKLoginKit (4.27.1)
  • Firebase Component: Messaging
  • Component version: FirebaseCommunity (0.1.3)
  • iOS: 11.0.2, also tested on 9.3.5

The problem

I download Example, run the project and find out that didReceiveRemoteNotification callback works always when phone connected to mac (by cable or wireless debugging), in this case it works in foreground, background and on locked screen as well. But if phone does not connected to mac, the didReceiveRemoteNotification callback fires only when app in foreground.

To check this I added simple request to server in didReceiveRemoteNotification, which send current time to database. Here is my code:

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

    let date = Date()
    let formatter = DateFormatter()
    formatter.dateFormat = "dd.MM.yyyy hh:mm:ss"
    let result = formatter.string(from: date)

    // prepare json data
    let json: [String: Any] = ["title": result]

    let jsonData = try? JSONSerialization.data(withJSONObject: json)

    // create post request
    let url = URL(string: "https://testfirebase-5d1ab.firebaseio.com/testCollection.json")!
    var request = URLRequest(url: url)
    request.httpMethod = "PUT"

    // insert json data to the request
    request.httpBody = jsonData

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            print(error?.localizedDescription ?? "No data")
            return
        }
        let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
        if let responseJSON = responseJSON as? [String: Any] {
            print(responseJSON)
        }
    }

    task.resume()

    print("\(userInfo.jsonString ?? "{}")")
    completionHandler(.newData)
  }

My notification payload to FCM is:

{
 "to": "device_id",
   "content_available": true,
   "priority": "high",
   "data": {
       "someParam": true
   }
}

In firebase console, I set up my APNs Authentication Key. In xCode in project capabilities Enabled Push Notification and Background Modes->Remote notification.

For simple testing I create firebase database, you can login by this account

testfirebase1110@gmail.com
zxc123zxc

Steps to reproduce:

  1. Change didReceiveRemoteNotification according to my example.
  2. Run app on device
  3. Unplug cable
  4. Move app to background
  5. Send remote notification as described above
  6. Check database for changes

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 24 (11 by maintainers)

Most upvoted comments

There is a known issue in iOS 11.0 where iOS is not processing silent notifications often. This is fixed in iOS 11.1 beta 1.

In my own testing on iOS 11.0, I’ve noticed this issue on my test devices. Are you able to see if this is fixed in 11.1?

If you are receiving the silent notification sometimes, it generally means that your FCM API calls / FCM SDK integration is working fine. If it was broken it would never work.

If this is happening also in iOS 9.3.5, keep in mind that silent notifications are disabled by iOS if:

  1. User force quits the app (Stopping debugging in Xcode is ok), until next app start
  2. User turns on low-power mode
  3. User disables background refresh
  4. User restarts phone, until next app start

Hi, I am using the same silence push notifications to schedule local push notifications for a calendar app.

  1. The server notifies by a silence push that there is a new event in the calendar.
  2. The client must use that silence push to schedule reminders for that event using local push notifications.

But I can only receive this push when I am in foreground. Is possible to receive them when I am in background or the app is killed? In android this feature is working.

You can find the logs retroactively by plugging in the phone. Alternatively you can opt for something simpler, like presenting a UILocalNotification.

I try to use PushNotificationIOS on iOS 9.3.5 and 11.0.2 by APNs. So I find out that on 9.3.5 it works in background without connected cable, didReceiveRemoteNotification fires each time well. But it didn’t work on 11.0.2, it works like I described above.