Firebase推送通知在应用程序终止时回调不起作用

6

我更新了 firebase_messaging,由于 FirebaseMessaging.configure() 被弃用了,所以我必须更改我的代码。现在当我接收到通知并单击通知时,它不会打开另一个屏幕。

这是我实现通知的方式:

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print('Handling a background message ${message.messageId}');
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'high_importance_channel', // id
  'High Importance Notifications', // title
  'This channel is used for important notifications.', // description
  importance: Importance.high,
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();
Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  runApp(MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'e-Rădăuți',
      debugShowCheckedModeBanner: false,
      initialRoute: '/',
      routes: {
        '/': (_) => MenuScreen(),
        '/events': (BuildContext context) => EventsScreen(),
      },
    );
  }
}
class MenuScreen extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

 Widget build(BuildContext context) {
    return Scaffold();
  }

  @override
  void initState() {
    super.initState();
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channel.description,
                icon: 'launch_background',
              ),
            ));
      }
    });
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      debugPrint('A new onMessageOpenedApp event was published!');
      
      Navigator.pushNamed(context, '/events');
    });
  }
}

但是当我点击通知时,.onMessageOpenedApp 没有被调用,因为在我的控制台(VSCode)中没有看到 debugPrint 消息,并且我收到了以下错误:
D/FLTFireMsgReceiver( 4799): broadcast received for message
W/civic.e_radaut( 4799): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (greylist,test-api, reflection, allowed)
W/civic.e_radaut( 4799): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (greylist,test-api, reflection, allowed)
W/civic.e_radaut( 4799): Accessing hidden method Landroid/os/WorkSource;->get(I)I (greylist, reflection, allowed)
W/civic.e_radaut( 4799): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (greylist, reflection, allowed)
W/FirebaseMessaging( 4799): Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used.
I/flutter ( 4799): Handling a background message 0:1617783965733220%2ebdcc762ebdcc76

我使用 click_action: FLUTTER_NOTIFICATION_CLICK 在firebase中发送了我的通知,在我的清单文件中添加了以下内容:

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

我的 firebase_messaging 版本是 ^8.0.0-dev.15

因此,我不知道我漏掉了什么或者为什么它没有起作用。如果您需要更多细节,请随时提问。


1
你好,我也遇到了FirebaseMessaging ^9.1.3的同样问题。当通知被点击且应用在前台时,FirebaseMessaging.onMessageOpenedApp.listen没有被触发。你找到任何解决方案了吗? - ALEXANDER LOZANO
1
@ALEXANDERLOZANO 很抱歉回复晚了。我认为,如果应用程序在前台,则应使用.onMessage.listen',因为应用程序已经打开并在前台,所以实际上不会打开应用程序,因此不会调用FirebaseMessaging.onMessageOpenedApp.listen,而是调用FirebaseMessaging.onMessage.listen`。我不确定这是否有效,因为我没有在应用程序在前台时实现回调。如果您没有找到解决方案,请给我发消息,我将尝试在我的代码中实现并查看它是否有效。 - t3nsa
感谢您的回答,我使用以下代码:FirebaseMessaging.onMessage.listen((RemoteMessage message) { print("新消息"); });但是没有成功。通知到达但监听器没有被触发。 - ALEXANDER LOZANO
我在 .onMessage 中添加了一个 debugPrint();,如果应用程序在前台时它会被触发,但当我点击它时不会触发,只有在收到通知时才会触发。如果我找到解决方案,我会告诉你。 - t3nsa
@ALEXANDERLOZANO 看起来你需要添加本地推送通知并在前台工作,以便在应用程序处于前台时进行回调。我明天会制作一个演示应用程序...现在是凌晨1:40 :( - t3nsa
显示剩余3条评论
3个回答

16

我通过使用.getInitialMessage()函数解决了这个问题(如果应用程序被终止,这是回调函数)。 当应用在后台运行但被终止时,我的通知无法正常工作。

为了解决这个问题,我只需将以下代码添加到我的代码中:

FirebaseMessaging.instance
    .getInitialMessage()
    .then((RemoteMessage message) {
  if (message != null) {
    Navigator.pushNamed(context, message.data['view']);
  }
});

我已经在这里制作了一个可工作的演示版。


谢谢!我尝试了你的代码,它可以正常工作。 - MSaudi
终于!谢谢!<3 - Amir Ali Aghamali
如何使Android在终止的Firebase通知上工作? - chanrlc

1
如果您想在应用程序启动前点击通知时转到任何页面或链接,则必须使用以下代码:

getInitialMessage()

例如:

FirebaseMessaging.instance
    .getInitialMessage()
    .then((RemoteMessage message) {
  if (message != null) {

   //to do your operation
   launch('https://google.com');

  }
});

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接