FirebaseMessaging.onBackgroundMessage不工作

xj3cbfub  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(108)

FirebaseMessaging.onBackgroundMessage不行,我是按照官方文件做的,我用的版本是:firebase_messaging: ^14.2.1

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  print("Handling a background message: ${message.messageId}");
}

void main() async {
  await Firebase.initializeApp()
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
}

这是我如何通过云函数发送我的fcn:

admin.messaging().sendMulticast({
        tokens: [user.fcmToken!],
        apns: {
          payload: {
            aps: {
              "mutable-content": 1,
              "content-available": 1,
            },
          },
        },
        notification: {
          body: body,
          title: title,
        },
        data: {
          hello: "world?!",
        },
      });

我可以看到推送通知在我的设备上弹出,但是FirebaseMessaging.onBackgroundMessage没有被调用。你知道问题出在哪里吗?

omhiaaxx

omhiaaxx1#

我想你忘了确保初始化:)

void main() async {
WidgetsFlutterBinding.ensureInitialized();//Try add this
await Firebase.initializeApp()
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
}

相关问题