我实现了推送通知(云消息传递),但是当应用程序完全关闭时我无法收到通知。
所以,我搜索了一下,发现我需要使用本地通知。
我开始使用本地通知,但我发现本地通知是基于用户方面的,例如由用户自己安排事件。所以,问题是如何使用flutter_local_notifications向所有用户发送通知?
谢谢
最佳答案
这个问题实际上分为两部分。
flutter_local_notifications
向其他设备发送通知?包裹。 For the 'Not receiving' part:
如果您已正确集成 FCM,则不必担心
在后台或
该应用程序已经被杀死。
FCM 会自动为您处理推送通知,而您
在后台或应用程序已经被杀死。
只需确保您已将 FCM 正确集成到您的 flutter 中
项目。
关注此 link进行适当的整合。此外,iOS 集成有点棘手和冗长,请遵循 link用于 APNs 集成。
如果您没有使用 FCM 正确设置 APN,Apple 将无法识别后台状态的推送通知触发器。
Now, for the "Sending notification to other devices via
flutter_local_notifications
package:
您不能通过将委托(delegate)给 FCM 的 flutter_local_notifications 推送通知。您必须发送 HTTP request通过 REST API 调用。
示例代码如下:
Future<Response> publishNotification() async {
return await post(
'https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$firebaseServerKey',
},
body: jsonEncode(
<String, dynamic>{
"to": firebaseDeviceToken,
'priority': 'high',
"data": <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
"body": "Order#10",
"title": "New Order",
"status": "done",
"screen": "NotificationPage",
"description": "Order Details",
}
},
),
);
}
您可能会添加我想向多个设备发送通知。为此,只需替换
token
与 registration_ids
并将您的设备 token 列表放在 registration_ids
.示例代码:
Future<Response> publishNotification() async {
return await post(
'https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$firebaseServerKey',
},
body: jsonEncode(
<String, dynamic>{
"registration_ids": ["token_1", "token_2", ...],
'priority': 'high',
"data": <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
"body": "Order#10",
"title": "New Order",
"status": "done",
"screen": "NotificationPage",
"description": "Order Details",
}
},
),
);
}
我希望我能覆盖你。快乐编码:D
关于flutter - 当应用程序关闭时在 Flutter 中推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66852722/