将推送通知从 firebase_messaging 6 迁移到 firebase_messaging 10+

时间:2021-06-26 14:47:42

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

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

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

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

1 个答案:

答案 0 :(得分: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 来测试它。

相关问题