8

How can i use broadcast receiver/intent to open flutter android app when receive firebase message.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
harsha.kuruwita
  • 1,315
  • 12
  • 21
  • 1
    When it's not an open application i.e in background or foreground(means when app is visible to the user). – Arul Sep 13 '21 at 14:54
  • 1
    you can check here https://stackoverflow.com/questions/48403786/how-to-open-particular-screen-on-clicking-on-push-notification-for-flutter – Muhammad Ashir Sep 13 '21 at 15:14

2 Answers2

1

Implment FirebaseMessagingService and start Main activity from onMessageReceived:


public class FirebaseMsgService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

    //...
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    }
}

and also in MainActivity you may want to unlock device:


public class MainActivity extends FlutterActivity {

    @Override
  public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
    GeneratedPluginRegistrant.registerWith(flutterEngine);
        

    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        
       

  }
}
Develocode 777
  • 1,125
  • 9
  • 14
1

The answer provided by @Develocode777 will work until android 9. From android 10, this behavior is not allowed anymore. You can learn more about it in this page, Restrictions on starting activities from the background. If you are planning to show a notification, and upon clicking the notification you want to open the app, then it it will work normally, but opening the app without no user interaction, not allowed in Android 10 and above.

Chinmay Kabi
  • 489
  • 4
  • 9
  • what about incominhcalls on whatsapp? – mosleim Sep 16 '21 at 19:24
  • Those are a special type of notification called Full screen intent notification. When you get a whatsapp call and device is being used, you will see a heads up notification, and not the whole app. The whole app will be shown only if either phone is locked, or you tap on the heads up notification. – Chinmay Kabi Sep 17 '21 at 14:47
  • how to implement to flutter? – mosleim Sep 20 '21 at 18:26
  • Very sorry for such late response, I had forgot about this totally. This is to be implemented individually for android and iOS natively. If you try showing your mainactivity as a full screen app then flutter app itself will show up, but it behaves very weird. Things get even more weird if a wakelock is in place. Better implement natively – Chinmay Kabi Dec 15 '21 at 09:16