Skip to content

Android: Handling Push Notifications

Neerhaj N Joshi edited this page Dec 6, 2023 · 5 revisions

Overview

This guide provides steps for configuring your mobile app to identify and display push notifications from Responsys if your app intercepts and handles the push notifications.

To complete the steps, you will need to add firebase_core and firebase_messaging dependencies, if not done already.

flutter pub add firebase_core

flutter pub add firebase_messaging

and import the related class in your project file,

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

Foreground Notifications


To handle messages when your app is in the foreground, listen to the onMessage stream.

It is recommended to add this code to your first/Home screen.

@override
void initState() {
    super.initState();

    initFirebaseMessaging(); 
}

void initFirebaseMessaging() async {

    await Firebase.initializeApp();

    await FirebaseMessaging.instance.getToken().then((token){

      if(token != null) {

        print("Received FCM token");

        PushIOManager.setDeviceToken(token);

      }

    });

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {

      print('Got a message in foreground');
      print('Message data: ${message.data}');

      if(message.data.isNotEmpty) {

        PushIOManager.isResponsysPush(message.data).then((isResponsysPush) {

          if (isResponsysPush ?? false) {

            // Push message is from Responsys, SDK will process message.
            PushIOManager.handleMessage(message.data);

          } else {

            // Push message not from Responsys, handle it appropriately
            print("Push message not from Responsys");

          }

        });

      }else{

        // message.data is empty, not a Responsys Push message
        // handle it appropriately
        print("message.data is empty");

      }

    });

}

Background Notifications


When the app is in background or killed-state (not in memory), a separate listener must be defined.

In main.dart file, create the following top-level function, i.e. place it outside any class definition.

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {

  await Firebase.initializeApp();

  print("Handling a background message: ${message.data}");

  if(message.data.isNotEmpty) {

    PushIOManager.isResponsysPush(message.data).then((isResponsysPush) {

      if (isResponsysPush ?? false) {

        // Push message is from Responsys, SDK will display the message.
        PushIOManager.handleMessage(message.data);

      } else {

        // Push message not from Responsys, handle it appropriately
        print("Push message not from Responsys");

      }

    });

  }else{

    // message.data is empty, not a Responsys Push message, 
    // handle it appropriately
    print("message.data is empty");

  }
}

Next, update the main() function as follows,

void main() {
    WidgetsFlutterBinding.ensureInitialized();
    
    FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

    runApp(MyApp());
}

Reference

FlutterFire - Cloud Messaging

Clone this wiki locally