0

I am trying to send background notifications using FCM, however, I have not been able to get it to work. I can get push notifications to work fine, just not background notifications. I have looked at numerous sources on this subject and tried many things, however, nothing seems to be working.

  1. I have "Push Notifications" enabled in XCode > Capabilities.
  2. I have "Remote notifications" checked in Background Modes in XCode > Capabilities
  3. I have registered the device token properly and configured APNs properly (Push notifications work)

The problem is this method never gets called in the background:

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

Some of the JSON formats I've tried are

{
   "notification": {
      "title": "title",
      "body": "body",
    },
    "data":{  
      "name":"name",
   },
   "content_available": true,

   "apns":{
    "headers":{
      "apns-priority": "5",
      "apns-push-type": "background"
    },
  },
    topic: topic,
  }

{  
   "data":{  
      "title":'title",
      "body":"body",
   },
   "notification":{  
      "title":"mytitle",
      "body":"mybody",
      "content_available": true
   },
   topic: topic
}

And many others.

I have used different combinations of "priority" and "content_available": true. Including "notifications" and not including them. I have put "content_available": true on different levels, and nothing seems to be working.

I've looked at:

FCM background notifications not working in iOS https://izziswift.com/fcm-background-notifications-not-working-in-ios/

https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

Firebase FCM silent push notifications for iOS

https://firebase.google.com/docs/cloud-messaging/ios/receive

And many other stack overflow posts.

With some of the JSON configurations that I have tried, I get this error and I'm not sure why:

Error: Process exited with code 16
    at process.on.code (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:275:22)
    at process.emit (events.js:198:13)
    at process.EventEmitter.emit (domain.js:448:20)
    at process.exit (internal/process/per_thread.js:168:15)
    at Object.sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:37:9)
    at process.on.err (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:271:22)
    at process.emit (events.js:198:13)
    at process.EventEmitter.emit (domain.js:448:20)
    at emitPromiseRejectionWarnings (internal/process/promises.js:140:18)
    at process._tickCallback (internal/process/next_tick.js:69:34) 

I'm really struggling and any help would be greatly appreciated!

Sdanson
  • 125
  • 2
  • 5

2 Answers2

0

The solution was to put contentAvailable: true

Sdanson
  • 125
  • 2
  • 5
0

Try add code in AppDelegate:

'Messaging.messaging().delegate =  self'

'Messaging.messaging().isAutoInitEnable = true'

'Messaging.messaging().shouldEstablishDirectChannel = true'
C. Peck
  • 3,641
  • 3
  • 19
  • 36
JsonX
  • 59
  • 3