How to Send Push Notifications Even If the App is closed?
Earlier we have been through an Implement push notification in flutter So, in this article, we will go through how to send push notifications even if the app is closed?
How to send push notifications even if the app is closed?
For reminders, we would recommend the Flutter Local Notifications Plugin. It has a powerful scheduling API. Flutter Agency is team of experienced Flutter developers for hire provides all types of support to make perfect applications for your organization.
Scheduling when notifications should appear – periodically show a notification interval-based – Schedule a notification to be shown daily at a specified time – Schedule a notification to be shown weekly on a specified day and time – Ability to handle when a user has tapped on a notification when the app in the foreground, background or terminated.
And for Push Notification, you can use Firebase Cloud Messaging or one signal plugin or you can implement natively through platform-channels
We can also register the Local Notification Plugin in the Application class.
First, create a class FlutterLocalNotificationPluginRegistrant code snippet will look like a below:
class FlutterLocalNotificationPluginRegistrant { companion object { fun registerWith(registry: PluginRegistry) { if (alreadyRegisteredWith(registry)) { Log.d(“Local Plugin”, “Already Registered”); return } FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor(“com.dexterous.f lutterlocalnotifications.FlutterLocalNotificationsPlugin”)) Log.d(“Local Plugin”, “Registered”); } private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean { val key = FlutterLocalNotificationPluginRegistrant::class.java.canonicalName if (registry.hasPlugin(key)) { return true } registry.registrarFor(key) return false } }} |
- Now create an application class extending the flutter application and implement PluginRegistry.PluginRegistrantCallback.
cclass Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback { override fun onCreate() { super.onCreate() } override fun registerWith(registry: PluginRegistry?) { if (registry != null) { FlutterLocalNotificationPluginRegistrant.registerWith(registry) } }} |
and register the application class in the AndroidManifest.xml
<application android:name=”com.packagename.Application”/> |
Now write a function to show notification and call it from the background handler method of firebase messaging.
Future _showNotificationWithDefaultSound(String title, String message) async { var androidPlatformChannelSpecifics = AndroidNotificationDetails( ‘channel_id’, ‘channel_name’, ‘channel_description’, importance: Importance.Max, priority: Priority.High); var iOSPlatformChannelSpecifics = IOSNotificationDetails(); var platformChannelSpecifics = NotificationDetails( androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics); await flutterLocalNotificationsPlugin.show( 0, ‘$title’, ‘$message’, platformChannelSpecifics, payload: ‘Default_Sound’, ); } |
and call it like this.
Future myBackgroundMessageHandler(Map message) async { if (message[‘data’] != null) { final data = message[‘data’]; final title = data['title']; final body = data['message']; await _showNotificationWithDefaultSound(title, message); } return Future.value(); } |
In some cases when the user is not receiving a notification user can also try with the below code snippet.
{notification: {body: null, title: null}, data: {body: hello, title: world}} |
To receive notification in app-closed state we changed notification to
{notification: {body: abc, title: abc}, data: {url: string, body: string, title: string}} |
Conclusion
Hope you guys are enjoying our articles.
In this article, we have been through how to send push notifications even if the app is closed?
Keep Learning !!! Keep Fluttering !!!