How to wake up flutter android app with firebase push notification

454

Solution 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.

Solution 2

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

  }
}
Share:
454
harsha.kuruwita
Author by

harsha.kuruwita

I am a iOS developer, technical consultant, and open-source contributor from 2013. Past 8 years i was iOS (Swift) developer and offer my services as an iOS developer for hire to work with businesses of all sizes including start-ups, creative agencies, designers, individuals and other freelancers & developers. And early 2019 I noticed that using Flutter/Dart greatly reduces development time, enhance app performance and can make beautiful UI/UX therefore I completely switched to Flutter/Dart and studying deeply flutter and Dart.

Updated on December 30, 2022

Comments

  • harsha.kuruwita
    harsha.kuruwita over 1 year

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

  • mosleim
    mosleim over 2 years
    what about incominhcalls on whatsapp?
  • Chinmay Kabi
    Chinmay Kabi over 2 years
    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.
  • mosleim
    mosleim over 2 years
    how to implement to flutter project?
  • mosleim
    mosleim over 2 years
    how to implement to flutter?
  • Chinmay Kabi
    Chinmay Kabi over 2 years
    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