【发布时间】:2020-06-26 15:13:48
【问题描述】:
我正在开发一个颤振应用程序,其中很多服务器请求都在我们的自定义服务器上实现,包括登录/注册。但是,我们决定使用 Firebase Cloud Messaging 进行通知。例如,应用程序中包含一个聊天功能。
在端到端消息传递过程中,随着消息被发布到我们的服务器,用户应该在他们的特定设备上收到通知。我已经在应用程序中成功实现和配置了firebase,并且可以从控制台发送消息,但我需要让它在特定设备上工作。
// Replace with server token from firebase console settings.
final String serverToken = '<Server-Token>';
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
Future<Map<String, dynamic>> sendAndRetrieveMessage() async {
await firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true, provisional: false),
);
await http.post(
'https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$serverToken',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': 'this is a body',
'title': 'this is a title'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': await firebaseMessaging.getToken(),
},
),
);
这是在 Flutter 中发送消息的文档化方式。但是,这会将通知发送给所有用户,而不是单个用户。那么,如何定位特定用户呢?
【问题讨论】:
-
这不会向所有用户发送消息。这仅向当前设备发送消息,仅用于测试目的。您应该设置一个后端来进行正确的设备消息传递。
标签: firebase flutter dart push-notification firebase-cloud-messaging