4

I have integrated firebase-messaging plugin for notification but on clicking notification app is not opening if it is in the background or killed.I want to open app when i click on the natification.

Below is my code for same

    void firebaseCloudMessagingListeners() {
      FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
      _firebaseMessaging.getToken().then((token){
        print(token);
      });

     _firebaseMessaging.requestNotificationPermissions(
            const IosNotificationSettings(sound: true, badge: true, alert: true));
        _firebaseMessaging.onIosSettingsRegistered
            .listen((IosNotificationSettings settings) {
          print("Settings registered: $settings");
        });

      _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          _showNotificationWithDefaultSound(message['notification']['body']);

          print('on message $message');
          return;
        },
        onResume: (Map<String, dynamic> message) async {
          _showNotificationWithDefaultSound(message['data']['body']);
          print('on message $message');
          return;
        },

        onLaunch: (Map<String, dynamic> message) async {
          _showNotificationWithDefaultSound(message['data']['body']);
          print('on message $message');
        },
      );
    }

    _showNotificationWithDefaultSound(message) async {
      FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
          new FlutterLocalNotificationsPlugin();

      var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
      var ios = new IOSInitializationSettings();
      var initSettings = new InitializationSettings(android, ios);
      flutterLocalNotificationsPlugin.initialize(initSettings,
          onSelectNotification: onSelectNotification);

      var android1 = new AndroidNotificationDetails(
          'channel id', 'channel NAME', 'channel DESCRIPTION',
          importance: Importance.Max, priority: Priority.High, ticker: 'ticker');

      var ios1 = new IOSNotificationDetails();
      var platform = new NotificationDetails(android1, ios1);
      await flutterLocalNotificationsPlugin
          .show(0, message, 'App Notification!', platform, payload: message);
    }
Kaushik Burkule
  • 816
  • 1
  • 12
  • 27
kunaljosh369
  • 195
  • 1
  • 3
  • 12

2 Answers2

6

You need add in AndroidManifest.xml

  <intent-filter>
      <action android:name="FLUTTER_NOTIFICATION_CLICK" />
      <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>

then, you need send click_action:FLUTTER_NOTIFICATION_CLICK in your data body of firebase notification like bellow:

  DATA='{
  "notification": {
    "body": "this is a body",
    "title": "this is a title"        
  },
  "data": {
    "att1": "value..", 
    "att2": "value..",        
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
  },
  "to": "<FCM TOKEN>"
}'

When your app is killed, if you click in notification, the method onLaunch is invoked, you must get message['data'] to get params.

see more here: https://stackoverflow.com/a/48405551/7105694

Higor Senna
  • 161
  • 1
  • 5
0

May be you need to implement onSelectNotification() method

Future<void> onSelectNotification(String payload) async {
        Navigator.push(
            context, PageTransition(
            type: PageTransitionType.leftToRight,
            child: /* your home screen name */));
  }
Sanjayrajsinh
  • 15,014
  • 7
  • 73
  • 78