【问题标题】:Sending Background Notifications With FCM使用 FCM 发送后台通知
【发布时间】:2021-04-06 15:21:57
【问题描述】:

我正在尝试使用 FCM 发送后台通知,但是我无法让它工作。我可以让推送通知正常工作,而不是后台通知。我查看了有关此主题的大量资源并尝试了很多方法,但是似乎没有任何效果。

  1. 我在 XCode > Capabilities 中启用了“推送通知”。
  2. 我在 XCode > 功能的后台模式中选中了“远程通知”
  3. 我已正确注册设备令牌并正确配置 APN(推送通知有效)

问题是这个方法永远不会在后台被调用:

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

我尝试过的一些 JSON 格式是

{
   "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
}

还有很多其他的。

我使用了“priority”和“content_available”的不同组合:true。包括“通知”而不包括它们。我已将 "content_available": true 放在不同的级别上,但似乎没有任何效果。

我看过:

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

还有许多其他堆栈溢出帖子。

使用我尝试过的一些 JSON 配置,我得到了这个错误,我不确定为什么:

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) 

我真的很挣扎,任何帮助将不胜感激!

【问题讨论】:

  • 你有没有让这个工作?我遇到了类似的问题

标签: ios firebase notifications firebase-cloud-messaging


【解决方案1】:

解决办法是把contentAvailable: true

【讨论】:

  • 在哪里?我也有同样的问题
【解决方案2】:

尝试在 AppDelegate 中添加代码:

'Messaging.messaging().delegate =  self'

'Messaging.messaging().isAutoInitEnable = true'

'Messaging.messaging().shouldEstablishDirectChannel = true'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2020-09-02
    • 1970-01-01
    • 2022-08-15
    相关资源
    最近更新 更多