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

时间:2021-04-07 08:45:56

标签: android flutter push-notification firebase-cloud-messaging remote-notifications

所以我更新了 firebase_messaging 并且我不得不更改我的代码,因为 onChange 已被弃用,现在当我收到通知并点击通知时,它不会打开另一个屏幕。

这是我实现通知的方式:

FirebaseMessagin.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'); }); } } ,因为我在我的控制台 (VSCode) 中没有收到 .onMessageOpenedApp 消息并且我收到以下错误:

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 从 Firebase 发送了我的通知,并在我的清单中添加了

click_action: FLUTTER_NOTIFICATION_CLICK

我的 <intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> 版本是 firebase_messaging

所以我不知道我错过了什么或者为什么它不起作用。如果您需要更多详细信息,请随时询问。

3 个答案:

答案 0 :(得分:4)

我使用 .getInitialMessaging() 函数解决了这个问题(这是应用终止时的回调。当应用处于后台但未终止时,我的通知有效。

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

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

我制作了一个工作演示here

答案 1 :(得分:0)

如果您想在应用启动前点击通知时转到任何页面或午餐链接,则必须使用

<块引用>

getInitialMessage()

示例:

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

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

  }
});

答案 2 :(得分:-1)

当应用程序在后台时它应该可以工作,但是当它被终止时你应该使用 getInitialMessage

<块引用>

onMessageOpenedApp:如果应用程序从后台状态打开(未终止),则将发送 Stream 事件。

如果您的应用在应用终止时通过通知打开,请参阅getInitialMessage

查看示例:https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/firebase_messaging/example/lib/main.dart#L116

相关问题