'Schedule notification in flutter using firebase
How can I schedule the flutter notification with Firebase to run in the background even if the mobile phone is turned off.
Solution 1:[1]
Make sure to check out Firebase cloud messaging if you want to make notifications for marketing purposes. If you want to store notifications locally check this video out: https://www.youtube.com/watch?v=KlgVI4dQC4E
Solution 2:[2]
There are several options for this task depending on your specific scheduling requirements.
- Schedule a local notification, which is for example triggered by the users themselves (e.g. some reminder or timer). In this case, the Flutter local notifications plugin does the trick.
These options make use of the Firebase cloud messaging service, for setup follow this guide:
- For scheduling notifications once in a while (e.g. for marketing purposes), the cloud messaging tab in Firebase offers a user interface for scheduling notifications.
- Schedule notifications repeating at a fixed interval with cloud functions. This can be done by utilizing the cronjob-like scheduling functionality.
- Schedule notifications starting from a certain point in time (with a delay) using cloud functions combined with cloud tasks like described here.
- A more complicated way of implementing tasks is described here, but I would not recommend it if you want an elegant solution.
In the end, scheduling notifications comes down to implementing a scheduling mechanism for cloud functions.
Solution 3:[3]
TLDR: You can use the firebase_notification_scheduler package to schedule notifications in Flutter.
Creating API Keys
This plugin makes use of a Rapid API service. You need to Signup for the Firebase Notification Scheduler Rapid API and get API Key from here. Then create your authentication key from here to work with package
Initialising Package
final FirebaseNotificationScheduler firebaseNotificationScheduler =
FirebaseNotificationScheduler(
authenticationKey: <YOUR-RAPID-API-KEY> ,
rapidApiKey: <YOUR-AUTHENTICATION-KEY>
);
Scheduling a notification
//Schedules a notification to the topic 'any' for next minute
final String _payload = {
"to": "/topics/any",
"notification": {
"title": "Title of Your Notification",
"body": "Body of Your Notification"
},
"data": {"key_1": "Value for key_1", "key_2": "Value for key_2"}
}.toString();
final DateTime _now = DateTime.now().toUtc();
final DateTime _dateTimeInUtc = _now.add(const Duration(minutes: 1));
await firebaseNotificationScheduler.scheduleNotification(
payload: _payload, dateTimeInUtc: _dateTimeInUtc);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Nedim Karavdic |
Solution 2 | |
Solution 3 | Rashid Abdulla |