【问题标题】:Migrating Push notification from firebase_messaging 6 to firebase_messaging 10+将推送通知从 firebase_messaging 6 迁移到 firebase_messaging 10+
【发布时间】:2021-09-09 13:58:11
【问题描述】:

我从旧的 Firebase_messaging 插件 6 + 升级到新的 Firebase_messenger 插件 10 +,我可以做大部分事情但无法获取消息数据,我想将此代码从旧插件转换为一个较新的,使用 configurelaunchonResume等方法。我可以收到关于消息的推送通知、前景和背景信息,但无法阅读。

class _ChatScreenState extends State<ChatScreen> {
  @override
  void initState() {
    super.initState();
    final fbm = FirebaseMessaging();
    fbm.requestNotificationPermissions();
    fbm.configure(onMessage: (msg) {
      print(msg);
      return;
    }, onLaunch: (msg) {
      print(msg);
      return;
    }, onResume: (msg) {
      print(msg);
      return;
    });
   
  }

到目前为止我做了什么 AndroidManifest.xml下添加

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

开启main.dart

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print('Handling a background message ${message.messageId}');
}

Future <void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
   runApp(MyApp());
}

这里是我想从服务器获取数据的地方

class _ChatScreenState extends State<ChatScreen> {
  @override
  void initState() {
    FirebaseMessaging.instance
        .getInitialMessage()
        .then((RemoteMessage message) {
      if (message != null) {
        print(message);
      }

      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
        print('Got a message whilst in the foreground!');
        print('Message data: ${message.data}');

        if (message.notification != null) {
          print(
              'Message also contained a notification: ${message.notification}');
        }
      });
    });

    super.initState();
  }

应用在前台时调试

D/FLTFireMsgReceiver(15437): broadcast received for message
I/flutter (15437): Got a message whilst in the foreground!
I/flutter (15437): Message data: {}
I/flutter (15437): Message also contained a notification: Instance of 'RemoteNotification'

在背景上

D/FLTFireMsgReceiver(15437): broadcast received for message
W/FirebaseMessaging(15437): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.
I/flutter (15437): Handling a background message 0:1624718321445677%ba7e1d8bba7e1d8b

虽然通知文本和正文在通知窗口中很好,但在调试屏幕中无法获得相同的信息,它返回空。我的实现也正确吗?

【问题讨论】:

    标签: flutter dart visual-studio-code google-cloud-functions


    【解决方案1】:

    经过一番搜索,我相信缺少的部分是 flutter_local_notifications: ^5.0.0+4 我所做的更改 main.dart

    Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
      // If you're going to use other Firebase services in the background, such as Firestore,
      // make sure you call `initializeApp` before using other Firebase services.
      await Firebase.initializeApp();
      print('Handling a background message ${message.messageId}');
    }
    
    const AndroidNotificationChannel channel = const AndroidNotificationChannel(
      //for notificaiton initialization
      'high_importance_channel', // id
      'High Importance Notifications', // title
      'This channel is used for important notifications.', // description
      importance: Importance.high,
      playSound: true,
    );
    
    //initialize plugin
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
    //for background messaging  
    FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
      //Local Notification implementation
      await flutterLocalNotificationsPlugin
          .resolvePlatformSpecificImplementation<
              AndroidFlutterLocalNotificationsPlugin>()
          ?.createNotificationChannel(channel);
      //for firebase  plugin and messaging required
    
      await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
          alert: true, badge: true, sound: true);
    
      runApp(MyApp());
    }
    
    

    在我想要通知的班级中

    class _ChatScreenState extends State<ChatScreen> {
      @override
      void initState() {
        //foreground messaging
    
        FirebaseMessaging.onMessage.listen((RemoteMessage message) {
          RemoteNotification notification = message
              .notification; //assign two variables for remotenotification and android notification
          AndroidNotification android = message.notification?.android;
          if (notification != null && android != null) {
            print(message);
            flutterLocalNotificationsPlugin.show(
              notification.hashCode,
              notification.title,
              notification.body,
              NotificationDetails(
                android: AndroidNotificationDetails(
                    channel.id, channel.name, channel.description,
                    color: Colors.blue,
                    playSound: true,
                    icon: '@mipmap/ic_launcher'),
              ),
            );
            print(notification.title);
            print(notification.body);
          }
        });
        //Do when the user taps the notification
        FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
          //same as above
        });
        super.initState();
      }
    

    如果有人为 IOS 更新此流程会很好,因为我没有开发人员 ID 来测试它。

    【讨论】:

      猜你喜欢
      • 2021-08-09
      • 2020-06-29
      • 2021-07-24
      • 2019-12-29
      • 2019-09-26
      • 2019-05-31
      • 2021-11-26
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多