Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didReceiveRemoteNotification function doesn't called with FCM notification server

I'm using FCM server for remote notifications, it worked perfectly with sending and receiving notifications.

My problem is when the device is in the background, this function func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) doesn't called.

Solutions that I've tried:

  • This function:

    userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
    

is called when user tapped on the notification. Otherwise, it's not triggered.

  • Adding "content-available": true, "priority": "high" to the payload. Also, I tried this value "content-available": 1.

  • This is my function code:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        
        print("userInfo: \(userInfo)")
      completionHandler(.newData);
  
    }
like image 422
Noura Aziz Avatar asked Sep 13 '25 04:09

Noura Aziz


2 Answers

Just adding on accepted answer: in my case the problem was the same, but as I'm sending the Notification through an http "POST" NSMUtableRequest the way to apply the accepted answer was to actually add it in the notification parameters together with sound, badge, tiles etc etc..

let postParams: [String : Any] = [
                "to": receiverToken,
                "notification": [
                    "badge" : 1,
                    "body": body,
                    "title": title,
                    "subtitle": subtitle,
                    "sound" : true, // or specify audio name to play
                    "content_available": true, // this will call didReceiveRemoteNotification in receiving app, else won't work
                    "priority": "high"
                ],
                "data" : [
                    "data": "ciao",
            ]
                ]

Hope this will also help others, as I spent a long time trying to figure out how to apply this solution. Many thanks.

like image 52
Vincenzo Avatar answered Sep 15 '25 17:09

Vincenzo


If you are testing with Postman then try changing "content-available": true to "content_available" : true. content-available will send notification but it doesn't call didReceiveRemoteNotification.

like image 30
iUser Avatar answered Sep 15 '25 19:09

iUser