firebase - Flutter - 后台通知在 iOS 中不起作用
问题描述
我正在 Flutter 中创建一个公共聊天应用程序,当我从 Cloud Function触发该方法时,我无法在iOS上接收后台通知。admin.messaging().send(payload)
这是我在 Cloud Function 中的有效负载:
var payload = {
notification: {
title: `My Title`,
body: `My message`,
},
android: { priority: "high" },
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
"apns-push-type": "background",
"apns-priority": "10",
"apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
},
},
topic: `mytopic`,
};
我尝试了一堆不同的有效载荷:
- 我试过通知+数据
- 我只尝试了数据(没有通知)(在这种情况下,我必须在本地显示通知,但它不起作用,因为
FirebaseMessaging.onBackgroundMessage
从未调用过该函数) - 我试图将“apns-priority”更改为 5
- 我尝试使用令牌而不是主题
感谢官方文档,我仔细设置了我需要配置的所有内容: https ://firebase.flutter.dev/docs/messaging/usage/
我还检查了 Github 问题,例如: 1041、6112、5988、1644、4300、4097
什么工作:
- 从 Firebase 控制台发送时Android 和 iOS上的后台和前台通知(在“云消息传递”部分)
- 从 Cloud Function 发送时,仅适用于 Android上的后台和前台通知
- 从 Cloud Function 发送时,iOS 上的前台通知
什么不工作:
- 从 Cloud Function 发送时iOS 上的后台通知
- 当应用程序终止时,它也不能正常工作。
解决方案
我终于解决了这个问题!
我只是删除了"apns-push-type": "background"
有效载荷中的行。现在它正在工作。
这条线似乎"apns-topic"
也没用。
这是我的最终有效载荷:
var payload = {
notification: {
title: `# ${context.params.passion}`,
body: `${newMsg["senderPseudo"]} ${
type == "image" ? "a envoyé une image." : `: ${newMsg["message"]}`
}`,
},
// Set Android priority to "high"
android: {
priority: "high",
},
// Add APNS (Apple) config
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
//"apns-push-type": "background", // This line prevents background notifications to be displayed
"apns-priority": "10",
},
},
token: "dnqTQVso60GfnnuOjHv8_e:APA91bElr-K3xkQMdYHX8VMrMZNCYCjO4zJlGseRh25AS_GT7cg9zlOGdQl4KXvr88ypeWjZjrPzrLRHitsQ-JKQK057ZQb_36c_lfsNjHXbYMYI2iS3jV_HGWf7Ene-ZlPvOb0aRr8u"
};
推荐阅读
- c - 从文件中读取,C
- wordpress - 增加默认 Gutenberg 'search' 块的宽度和高度
- reactjs - 在 useState 钩子中使用 prop 值会给出未定义的值
- python - 在 micropython 中从 uart 接收数据
- python-3.x - 逆变换
- python - 计算每个模型 ID 的流失率
- javascript - 如何使用 AJAX 自动检测页面上的更改并刷新内容?
- java - 如何从另一个类调用方法以在我的 ActionListener 类中使用
- sql - 是否可以在 django 模型中存储属性外键?
- html - 我应该如何更改移动设备的 React 输入按钮宽度?