Reputation: 533
I am working on a flutter application whereby a lot of server requests were implemented on our custom server including login/signup. However, we decided to use Firebase Cloud Messaging for notifications. For example, included in the application is a chat feature.
During end to end messaging, as messages get posted to our server, users should receive notifications on their specific devices. I have already successfully implemented and configured firebase
within the app and can send messages from the console but i need to make it work on specific devices.
// 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(),
},
),
);
This is the documented way of sending messages within flutter. However, this will send notifications to all users not a single user. So, how do i target a specific user?
Upvotes: 4
Views: 7316
Reputation: 482
I have read some implementations for what you are trying to achieve and I guess you should follow this steps:
Hope it helps. I provide no code for any of this because I believe it is not what you are searching for.
Upvotes: 8
Reputation: 143
You need change
'to': await firebaseMessaging.getToken(),
to
'to': '${token of specific devices}',
.
Upvotes: 2