OnClick使用FCM Flutter应用程序打开一个空活动

enyaitl3  于 2023-05-01  发布在  Flutter
关注(0)|答案(2)|浏览(190)

我遵循了flutter文档中关于如何使用新的flutter android embedding v2实现firebase_messaging的每一步
我在每个应用程序状态(前台/后台/已终止或已分离)下接收通知都没有问题。单击通知在其他状态下也能正常工作,但已终止状态除外。

在终止状态下,当我尝试点击应用程序分离时收到的通知时,它会打开应用程序,但我没有收到getInitialMessage函数中的任何数据(根据文档)。此外,当我试图通过按下后退按钮离开应用程序时,不知何故,应用程序**在它下面显示一个额外的空活动,我不知道为什么会发生这种情况。我想摆脱它。

仅当在已终止状态下接收到通知时才会出现这些问题。
onBackgroundMessage侦听器即使在终止状态下也能完美工作。
主箭头:

Future<void> _backgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print("Message from background : " + message.notification.body);
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  //init notification service
  await NotificationService().initNotification();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_backgroundHandler);

主页屏幕。dart:setupInteractedMessage()在initState()中被调用

Future<void> setupInteractedMessage() async {
    //Terminated State
    //Comes in from terminated app's notification
    FirebaseMessaging.instance.getInitialMessage().then((value) => {
          if (value != null)
            {print("ContentAvailable : " + value.contentAvailable.toString())}
        });

    //Foreground state
    FirebaseMessaging.onMessage.listen((event) {
      String title = event.notification.title;
      String body = event.notification.body;
      int tag = int.parse(event.data["tag"]);
      //Dont show notification if inside conversation with the sender
      if (!Provider.of<ConversationBloc>(context, listen: false)
          .disableNotification(tag)) {
        NotificationService()
            .showNotification(tag, title, body, json.encode(event.data));
      } else {
        soundHandler.playOnNewMessageSound();
      }

      print("Received in foreground");
    });

    //Opened from background notification trigger and handler
    //It does not work if the app is detached only works in paused state
    FirebaseMessaging.onMessageOpenedApp.listen((event) {
      NotificationService().selectNotification(json.encode(event.data));
      print("Received in background while the app is paused and not detached");
    });
  }

视频:https://drive.google.com/file/d/1-De9Buv6ToLB13hIat7i9uZsQOSS2XJO/view?usp=drivesdk
我的推荐人:https://firebase.flutter.dev/docs/messaging/usage

fwzugrvs

fwzugrvs1#

尝试将此Intent标记添加到AndroidManifest中的Activity标记中。xml文件。

<activity>

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

....

在发送FCM时,不要忘了将“click_action”字段添加到您的有效载荷中。例如:'click_action' : 'FLUTTER_NOTIFICATION_CLICK'

agxfikkp

agxfikkp2#

from docs for onMessageOpenedApp如果应用已从后台状态打开///(未终止),则将发送Stream事件。//////如果您的应用在终止时通过通知打开,///请参阅[getInitialMessage]。
因此您必须使用getInitialMessage尝试添加一些延迟,

Future.delayed(Duration(seconds: 2),(){
  // handle app opened from notification
  RemoteMessage? remoteMessage = await FirebaseMessaging.instance.getInitialMessage();
 // add your custom code here
});

相关问题