This question has two-part actually.
- Not receiving a notification while the app is closed
- How can you send a notification to other devices by using the
flutter_local_notifications package.
For the 'Not receiving' part:
If you've integrated FCM properly, you shouldn't have to worry about
not getting push notifications at all while in the background or the
the app is killed already.
FCM will automatically handle push notifications for you while you're
in the background or the app is killed already.
Just make sure, you've integrated FCM properly to your flutter
project.
Follow this link for proper integration. Also, iOS integration is a bit tricky and lengthy, follow this link for APNs integration.
If you did not set up APNs properly with FCM, apple will not be able to recognize push notification trigger for background state.
Now, for the "Sending notification to other devices via flutter_local_notifications package:
you can't push notification via flutter_local_notifications which will delegate with FCM. You have to send an HTTP request via REST API call for that.
Sample code is given below:
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",
}
},
),
);
}
You might add that I want to send notifications to multiple devices.
For that, just replace token with registration_ids and put your device token list on registration_ids.
Sample code:
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",
}
},
),
);
}
I hope that I got you covered. Happy coding :D