如何使用 firebase 推送通知唤醒 fl​​utter android 应用程序

har*_*ita 8 firebase flutter firebase-cloud-messaging flutter-test flutter-dependencies

当收到 firebase 消息时,如何使用广播接收器/意图打开 flutter android 应用程序。

Dev*_*777 1

实现 FirebaseMessagingService 并从 onMessageReceived 启动 Main 活动:


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);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且在 MainActivity 中您可能想解锁设备:


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

  }
}
Run Code Online (Sandbox Code Playgroud)