How to wake up flutter android app with firebase push notification

Viewed 872

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

2 Answers

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);
        
       

  }
}

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.

Related