Read now

Push Notifications for Flutter using Firebase

Push notifications are a message that pops up on a mobile device. App publishers can send them at any time, users don’t have to be in the app or using their devices to receive them. They can do a lot of things. for example, they can show the latest sports scores, get a user to take any action, such as downloading a coupon or let a user know about an event, such as a flash sale.

Push notifications look like SMS text messages and mobile alerts, but they only reach users who have installed your app. Each mobile platform has support for push notifications — iOS, Android, and many other OS have their own services.

To implement notification, add firebase_messaging as a dependency in your pubspec.yaml file.

Push Notifications Integration for Android

To integrate your plugin into the Android part of your app, follow these steps:

  1. Using the Firebase Console add an Android app to your project: Follow the assistant, download the generated google-services.json file and place it inside android/app.
  2. Add the classpath to the [project]/android/build.gradle file.

  1. Add the apply plugin to the [project]/android/app/build.gradle file.

Note: If this section is not completed you will get an error like this:

Note: When you are debugging on Android, use a device or AVD with Google Play services. Otherwise, you will not be able to authenticate.

  1. (optional, but recommended) If want to be notified in your app (via onResume and onLaunch, see below) when the user clicks on a notification in the system tray include the following intent-filter within the <activity> tag of your android/app/src/main/AndroidManifest.xml:

Optionally handle background messages

Background message handling is intended to be performed quickly. Do not perform long running tasks as they may not be allowed to finish by the Android system. See Background Execution Limits for more.

By default background messaging is not enabled. To handle messages in the background:

  1. Add the com.google.firebase:firebase-messaging dependency in your app-level build.gradle file that is typically located at <app-name>/android/app/build.gradle.

    Note: you can find out what the latest version of the plugin is here (“Cloud Messaging”).
  2. Define a TOP-LEVEL or STATIC function to handle background messages

    Note: the protocol of data and notification are in line with the fields defined by a RemoteMessage.
  3. Set onBackgroundMessage handler when calling configure

Push Notifications Integration for iOS

To integrate your plugin into the iOS part of your app, follow these steps:

  1. Generate the certificates required by Apple for receiving push notifications following this guide in the Firebase docs. You can skip the section titled “Create the Provisioning Profile”.
  2. Using the Firebase Console add an iOS app to your project: Follow the assistant, download the generated GoogleService-Info.plist file, open ios/Runner.xcworkspace with Xcode, and within Xcode place the file inside ios/RunnerDon’t follow the steps named “Add Firebase SDK” and “Add initialization code” in the Firebase assistant.
  3. In Xcode, select Runner in the Project Navigator. In the Capabilities Tab turn on Push Notifications and Background Modes, and enable Background fetch and Remote notifications under Background Modes.
  4. Follow the steps in the “Upload your APNs certificate” section of the Firebase docs.
  5. If you need to disable the method swizzling done by the FCM iOS SDK (e.g. so that you can use this plugin with other notification plugins) then add the following to your application’s Info.plist file.

After that, add the following lines to the (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method in the AppDelegate.m/AppDelegate.swift of your iOS project.

Objective-C:

Swift:

Dart/Flutter Integration

From your Dart code, you need to import the plugin and instantiate it:

Next, you should probably request permissions for receiving Push Notifications. For this, call _firebaseMessaging.requestNotificationPermissions(). This will bring up a permissions dialog for the user to confirm on iOS. It’s a no-op on Android. Last, but not least, register onMessageonResume, and onLaunch callbacks via _firebaseMessaging.configure() to listen for incoming messages (see table below for more information).

Receiving Messages

Messages are sent to your Flutter app via the onMessageonLaunch, and onResume callbacks that you configured with the plugin during setup.

Notification messages with additional data

It is possible to include additional data in notification messages by adding them to the "data"-field of the message.

On Android, the message contains an additional field data containing the data. On iOS, the data is directly appended to the message and the additional data-field is omitted.

To receive the data on both platforms:

Sending Messages

Refer to the Firebase documentation about FCM for all the details about sending messages to your app. When sending a notification message to an Android device, you need to make sure to set the click_action property of the message to FLUTTER_NOTIFICATION_CLICK. Otherwise, the plugin will be unable to deliver the notification to your app when the users click on it in the system tray.

For testing purposes, the simplest way to send a notification is via the Firebase Console. Make sure to include click_action: FLUTTER_NOTIFICATION_CLICK as a “Custom data” key-value-pair (under “Advanced options”) when targeting an Android device. The Firebase Console does not support sending data messages.

Alternatively, a notification or data message can be sent from a terminal:

Remove the notification property in DATA to send a data message.

Output

You will get the notification same like the native apps and also customize the notification alert design like this-

Thank You for reading this article!!!

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close