Notification

Flutter push notification setup for real time updates

Push Notification setup

Android

  • Goto /android/build.gradle and add the following under buildscript -> dependencies classpath 'com.google.gms:google-services:4.0.1'

  • Add the following at the bottom of the file: apply plugin: 'com.google.gms.google-services'

It will look something like this:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    // Switch the Android Gradle plugin version requirement depending on the
    // installed version of Gradle. This dependency is documented at
    // http://tools.android.com/tech-docs/new-build-system/version-compatibility
    // and https://issues.apache.org/jira/browse/CB-8143
    dependencies {
      classpath 'com.android.tools.build:gradle:3.3.0'
      classpath 'com.google.gms:google-services:4.0.1'
    }
}

apply plugin: 'com.google.gms.google-services'
  • From login success callback, call ApplozicFlutter.registerForPushNotification();

  • Refer to the following FcmListenerService class and update the the one in your app. If you don't have one, you can create a new file.

package com.example.applozic_flutter;

import android.util.Log;
import com.applozic.mobicomkit.Applozic;
import com.applozic.mobicomkit.api.account.register.RegisterUserClientService;
import com.applozic.mobicomkit.api.account.user.MobiComUserPreference;
import com.applozic.mobicomkit.api.notification.MobiComPushReceiver;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FcmListenerService extends FirebaseMessagingService {
    private static final String TAG = "ApplozicFcmListener";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.i(TAG, "Message data:" + remoteMessage.getData());

        if (remoteMessage.getData().size() > 0) {
            if (MobiComPushReceiver.isMobiComPushNotification(remoteMessage.getData())) {
                Log.i(TAG, "Applozic notification processing...");
                MobiComPushReceiver.processMessageAsync(this, remoteMessage.getData());
                return;
            }
        }
    }

    @Override
    public void onNewToken(String registrationId) {
        super.onNewToken(registrationId);

        Log.i(TAG, "Found Registration Id:" + registrationId);
        Applozic.getInstance(this).setDeviceRegistrationId(registrationId);
        if (MobiComUserPreference.getInstance(this).isRegistered()) {
            try {
                new RegisterUserClientService(this).updatePushNotificationId(registrationId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
  • Add the FcmListenerService entry to your manifest.
<service android:name="<CLASS_PACKAGE>.FcmListenerService"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

For push notifications, you must have a Firebase account:

  • Sign-up to https://console.firebase.google.com/ and create your application and generate push notification services file.

  • Download google-services.json from your Firebase Console and paste it to /android/app folder

  • Go to Applozic console, update the FCM Server Key from Firebase account to your Applozic account push notification section here.

iOS

  • Create APNs certificates and upload development and distributions certificates in applozic console you can refer this link here.

  • Do not forget to update capabilities.

  • Add the following file (ApplozicWrapper.swift) to your {APP_ROOT_FOLDER}/ios/Runner

  • Open {APP_ROOT_FOLDER}/ios/Runner/AppDelegate.swift file and add the functions that are present in the AppDelegate class below. If the file does not exists, create new one.

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    UNUserNotificationCenter.current().delegate = self
            ApplozicWrapper.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    
    override func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            ApplozicWrapper.shared.userNotificationCenter(center, willPresent: notification, withCompletionHandler: { options in
                completionHandler(options)
            })
        }
        
        override func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            ApplozicWrapper.shared.userNotificationCenter(center, didReceive: response, withCompletionHandler: {
                completionHandler()
            })
        }
        
        override func applicationWillTerminate(_ application: UIApplication) {
            ApplozicWrapper.shared.applicationWillTerminate(application: application)
        }
        
        override func applicationWillEnterForeground(_ application: UIApplication) {
            print("APP_ENTER_IN_FOREGROUND")
            UIApplication.shared.applicationIconBadgeNumber = 0
        }
        
        override func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            print("DEVICE_TOKEN_DATA :: \(deviceToken.description)") // (SWIFT = 3) : TOKEN PARSING
            ApplozicWrapper.shared.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
        }
}

Using flutter firebase_messaging plugin

If you are using firebase_messaging plugin in your app for push notifications, follow the below steps to make Applozic notifications work with the Firebase plugin:

  • Open FcmListenerService.java and extends it with FlutterFirebaseMessagingService instead of FirebaseMessagingService.

  • In the FcmListenerService.java file add:

import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
  • In AndroidManifest.xml file add or replace the below service inside tag:
<service
        android:name="<CLASS_PACKAGE>.FcmListenerService"
        tools:node="replace"
        android:exported="true"
        android:stopWithTask="false">
       <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
      </intent-filter>
</service>
  • Add the below tools entry (If not already added) inside the tag of AndroidManifest.xml file as below: xmlns:tools="http://schemas.android.com/tools"
    For example (2nd line in the below snippet):
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.applozic_flutter_example">
  • Add the below entry in within the tag in your AndroidManifest.xml file:
<service   android:name="io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService"
         tools:node="remove"/>

What’s Next

Unread count

Did this page help you?