简体   繁体   English

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

[英]Firebase push notifications callback doesn't work when app is terminated

So I updated the firebase_messaging and I had to change my code because FirebaseMessagin.configure() is deprecated and now when I receive the notification and click on the notification it doesn't open another screen.所以我更新了firebase_messaging并且我不得不更改我的代码,因为FirebaseMessagin.configure()已被弃用,现在当我收到通知并单击通知时,它不会打开另一个屏幕。

This is how I implemented the notifications:这就是我实现通知的方式:

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

But .onMessageOpenedApp isn't called when I click on the notification because I don't get that debugPrint message in my console (VSCode) and I get the following errors:但是,当我单击通知时,不会调用.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

I sent my notification from the firebase with the click_action: FLUTTER_NOTIFICATION_CLICK and in my manifest I've added我通过click_action: FLUTTER_NOTIFICATION_CLICK从 firebase 发送了我的通知,并在我的清单中添加了

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

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

So I don't know what I've missed or why it's not working.所以我不知道我错过了什么或为什么它不起作用。 If you need more details please feel free to ask.如果您需要更多详细信息,请随时询问。

I resolved this by using the .getInitialMessage() function (This is the callback if the app is terminated. My notifications worked when the app was on background but not terminated.我通过使用.getInitialMessage() function 解决了这个问题(这是应用程序终止时的回调。当应用程序在后台但未终止时,我的通知有效。

To resolve this I just added this to my code:为了解决这个问题,我刚刚将它添加到我的代码中:

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

I've made a working demo here我在这里做了一个工作演示

It should work when the app is in the background, but when it is terminated you should use getInitialMessage .当应用程序在后台时它应该可以工作,但是当它终止时你应该使用getInitialMessage

onMessageOpenedApp : A Stream event will be sent if the app has opened from a background state (not terminated). onMessageOpenedApp :如果应用程序已从后台 state(未终止)打开,则将发送 Stream 事件。

If your app is opened via a notification whilst the app is terminated, see getInitialMessage .如果您的应用在应用终止时通过通知打开,请参阅getInitialMessage

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

if you want go to any page or lunch link when click notification before app started then you must use如果您希望 go 在应用程序启动之前单击通知时进入任何页面或午餐链接,那么您必须使用

getInitialMessage()

example:例子:

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

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

  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序终止时的 FCM 推送通知 FLUTTER - FCM push notifications FLUTTER when app is terminated 应用未运行时 Firebase 不显示通知 - Firebase doesn't show notifications when app not running 应用程序不显示 firebase 通知(广播意图回调:result=CANCELLED forIntent) - App doesn't show firebase notifications (broadcast intent callback: result=CANCELLED forIntent) 离子:通过 firebase 从设备到设备发送推送通知:从 iOS 发送不起作用 - Ionic: Send Push Notifications via firebase from device to device : Sending doesn't work from iOS 推送通知仅在应用程序运行时有效 - Push notifications only work when the app is running Firebase Cloud Messaging android项目不会发送推送通知 - Firebase Cloud Messaging android project doesn't send push notifications Android中的Firebase Push不起作用 - Firebase Push in Android doesn't work Phonegap PushPlugin Android:当应用程序更新时,push不起作用 - Phonegap PushPlugin Android: push doesn't work when app is updated Firebase 推送通知在后台应用程序时显示错误图标 - Firebase push notifications show wrong icon when app in background 当应用程序在带有 firebase 的后台时,使推送通知显示为弹出 - Make push notifications appear as pop up when app is on background with firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM