Push Notification in Flutter when the app is turn off
Solution 1
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
Solution 2
I can't imagine local notificaions would solve your problem.
According to the docs:
There are a few preconditions which must be met before the application can receive message payloads via FCM:
- The application must have opened at least once (to allow for registration with FCM).
- On iOS, if the user swipes away the application from app Switcher, it must be manually reopened again for background messages to start working again.
- On Android, if the user force quits the app from device settings, it must be manually reopened again for messages to start working.
If these conditions are not met, your notification is simply never received. If no notification is received, then what are you going to display with local notifications?
What you could do is use local notificaions in combination with background fetch. With background fetch you can check some api whether there are some notifications. You'd have to somehow figure out which ones have been presented already and which ones not. Then you could use local notifications to present those. In that case I think it would be easier to drop cloud messaging and only use background fetch + local notifications.
On iOS you'd still face the same problem with background fetch. According to their docs:
When your app is terminated, iOS no longer fires events.
![Abdulsalam Fadhel](https://lh6.googleusercontent.com/-nelJn6psQZ0/AAAAAAAAAAI/AAAAAAAAAAA/AMZuuclmiKi3reGXb6zXi90y201mz_pvpA/photo.jpg?sz=256)
Abdulsalam Fadhel
Updated on December 28, 2022Comments
-
Abdulsalam Fadhel over 1 year
I implemented push notification(cloud messaging ) however I could not get the notification when the app is totally turned off. So, I searched about that and i found that I need to use local notification. I started using local notifcation but I found that local notification is based on user's sides by schedualing events for example by the users themselves. So, the question is that how can I send a notification to all users using flutter_local_notifications?
Thank you
-
Magnus over 3 yearsWhat exactly do you mean by "totally turned off"? If you mean after they force close the app, then you might want to think about whether you really want and need to show notifications to those users. Users who force close an app have no reason to expect the app to function normally, so this might be a non-issue.
-
Abdulsalam Fadhel over 3 yearsI mean when the app is closed(Not working in the background)
-
Robin Dijkhof about 3 yearsCan you share where/how you found that you'd need local notifications?
-
ahmad bajwa about 3 yearssuppose he is using api call for notification data. In background if the application is killed completely, get the notification data from api and show that as notification.
-
ahmad bajwa about 3 yearsI have achieved solution for the same problem in java android. Now I would like to see that in flutter too. In java I used Work Manager for local notifications.
-
-
ahmad bajwa about 3 yearswhat do you mean by background fetch?
-
ahmad bajwa about 3 yearscan you provide example code of background fetch + local notifications?
-
ahmad bajwa about 3 yearsbro can you explain the backgroud fetch code please.
-
ahmad bajwa about 3 yearsit would be great if you add the background fetch code with explanation in your answer.
-
ahmad bajwa about 3 yearsWhen your app is terminated, iOS no longer fires events. I think without IOS it is useless. What the meaning of using flutter if this does not support IOS. Can you provide anything for both android and IOS for same logic.
-
danypata about 3 yearsPush notifications are received even if the app is killed. If you receive push notifications while the app is in background (not killed) then the integration is correct. If you receive push when the app is in bg but not when the app is killed then your push notification payload is wrong.
-
ahmad bajwa about 3 yearsHow can we send a FCM notification without its data. For example a user did a comment on a post of someone. Now we want to send the notification to that user. How we can send that notification by FCM?
-
Omar Khaium Chowdhury about 3 yearsThis link might help you to understand, firebase.google.com/docs/cloud-messaging/…
-
ahmad bajwa about 3 yearsbut its not for the flutter.
-
Omar Khaium Chowdhury about 3 yearsDo you want to send a notification from the flutter project? not from the server end?
-
Omar Khaium Chowdhury about 3 yearsYou can simply place an HTTP request from your flutter project on that case.
-
ahmad bajwa about 3 yearsSo, the question is that how can I send a notification to all users using flutter_local_notifications
-
Omar Khaium Chowdhury about 3 yearsBro, you can't push a notification via flutter_local_notifications which will delegate with FCM. You have to send an HTTP request via REST API call for that.