这个问题实际上分为两部分。
- 应用关闭时未收到通知
- 如何使用
flutter_local_notifications 包向其他设备发送通知。
对于“未接收”部分:
如果您已正确集成 FCM,则不必担心
在后台或
该应用程序已被杀死。
FCM 会在您使用时自动为您处理推送通知
在后台或应用程序已被杀死。
请确保您已将 FCM 正确集成到您的颤振中
项目。
关注此link 以进行正确集成。此外,iOS 集成有点棘手和冗长,请按照此link 进行 APNs 集成。
如果您没有使用 FCM 正确设置 APN,Apple 将无法识别后台状态的推送通知触发器。
现在,对于“通过flutter_local_notifications 包向其他设备发送通知:
您不能通过 Flutter_local_notifications 推送通知,该通知将委托给 FCM。为此,您必须通过 REST API 调用发送HTTP request。
示例代码如下:
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 并将您的设备令牌列表放在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