尝试通过单击通知进入深层链接,但应用程序打开,但停留在第一个屏幕上

问题描述 投票:0回答:1

我已经使用 flutter 和 firebase 构建了一个 Android 应用程序。我使用 go_router 来设置所有导航和深层链接。 当我单击链接(例如粘贴在 Whatsapp 上)时,深层链接可以正常工作。但是,当我发送通知时,我会收到通知,但当我单击它时,它不会进入相应的页面,而是会打开应用程序。 以下是 Codelabs 建议的相关代码片段。

全球范围

    // TODO: Add stream controller
final _messageStreamController = BehaviorSubject<RemoteMessage>();
// TODO: Define the background message handler

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  if (kDebugMode) {
    print("Handling a background message: ${message.messageId}");
    print('Message data: ${message.data}');
    print('Message notification: ${message.notification?.title}');
    print('Message notification: ${message.notification?.body}');
  }
}

main 内部(调用运行应用程序之前)

final messaging = FirebaseMessaging.instance;

  final settings = await messaging.requestPermission(
    alert: true,
    announcement: false,
    badge: true,
    carPlay: false,
    criticalAlert: false,
    provisional: false,
    sound: true,
  );

  if (kDebugMode) {
    print('Permission granted: ${settings.authorizationStatus}');
  }

  String? token = await messaging.getToken();
  token = await messaging.getToken();

  if (kDebugMode) {
    print('Registration Token=$token');
  }

  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    if (kDebugMode) {
      print('Handling a foreground message: ${message.messageId}');
      print('Message data: ${message.data}');
      print('Message notification: ${message.notification?.title}');
      print('Message notification: ${message.notification?.body}');
    }
    _messageStreamController.sink.add(message);
  });
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

在我的应用程序类中

Future<void> setupInteractedMessage() async {
    RemoteMessage? initialMessage =
        await FirebaseMessaging.instance.getInitialMessage();
    if (initialMessage != null) {
      _handleMessage(initialMessage);
    }
    FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
  }

  void _handleMessage(RemoteMessage message) {
    RemoteNotification? notification = message.notification;
    if (kDebugMode) {
      print("notification: $notification");
      print("message data: ${message.data}");
    }
    if (message.data['type'] == 'newArticle') {
      final channelId = message.data['channelId'];
      final articleId = message.data['articleId'];
      context.go('/RMYCHANNELS/ROneChannel/$channelId}');
    }
  }

  @override
  void initState() {
    super.initState();
    setupInteractedMessage();
    tokenListener =
        FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
      // final user = FirebaseAuth.instance.currentUser;
      // if (user != null) {
      //   AppUser.addToken(user.uid, fcmToken);
      // }
      // Note: This callback is fired at each app startup and whenever a new
      // token is generated.
    });
  }
android flutter firebase notifications firebase-cloud-messaging
1个回答
0
投票

您的应用程序是否已终止或在后台运行?如果在后台运行,

onMessageOpenedApp()
会触发,否则您会收到来自
getInitialMessage()
的初始消息。

如果您的应用程序在后台运行,只需在此行添加断点:

message.data['type'] == 'newArticle'
,查看远程消息。

如果您的应用程序未运行,只需显示一个带有

RemoteMessage
数据和通知详细信息的信息栏。

查看推送通知对象:

"click_action": "FLUTTER_NOTIFICATION_CLICK"
很重要,必须位于
data
键下。

工作示例:

push notification working example

如果仍然不起作用,请尝试创建一个

NotificationChannel
: 使用 flutter_local_notifications 包创建通知通道。

示例:

将此代码添加到

requestPermission()
之后。

if (Platform.isAndroid) {
    final AndroidNotificationChannel channel = const 
    AndroidNotificationChannel(
        "my_app_channel", 
        "The channel name of my app",
        importance: Importance.high,
        playSound: true,
        enableVibration: true
    );
    
    await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
}
   

不要忘记更新 AndroidManifest.xml 文件中默认通知通道的元数据:

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="my_app_channel" />

我认为你自己的通知通道可以解决这个问题,因为目前系统默认的通知通道处理你的消息,它只显示通知,不转发你的自定义数据对象。

© www.soinside.com 2019 - 2024. All rights reserved.