在iOS上使用flutter进行开发时无法收到firebase的通知
我已经同意了,并使用邮递员进行测试。
首先,我得到tokenRestarted application in 516ms.
flutter: User granted permission
flutter: This token is eWQJLcN1zEQ_rQ79sBqXI6:APA91bHMnuhkPaf8JpcTbLQavxqq5erUIeRTQYPv9NAhsdfasdfasdaDSA0BTXgVb2RseAmUsi5uAa5SDvGWA7wgIcWPP5xDqTyo4s3xEdWkhHoQc77G59GGZptafaxNIW5QG
秒码:当我去邮递员测试时,我得到了成功
{
"multicast_id": 2305740741419558081,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1654957148235526%2734e82c2734e82c"
}
]
}
但我仍然没有收到任何通知。
哪里有问题?
这是代码
late int _totalNotifications;
late final FirebaseMessaging _messaging;
PushNotification? _notificationInfo;
void requestAndRegisterNotification() async {
await Firebase.initializeApp();
_messaging = FirebaseMessaging.instance;
FirebaseMessaging.onBackgroundMessage(_firebaseMessageingBackgroundHandler);
NotificationSettings settings = await _messaging.requestPermission(
alert: true,
badge: true,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
print('User granted permission');
String? token = await _messaging.getToken();
print('This token is ' + token!);
FirebaseMessaging.onMessage.listen(
(RemoteMessage message) {
PushNotification notification = PushNotification(
title: message.notification?.title,
body: message.notification?.body,
);
setState(() {
_notificationInfo = notification;
_totalNotifications++;
});
if (_notificationInfo != null) {
showSimpleNotification(
Text(_notificationInfo!.title!),
leading:
NotificationBadge(totalNotification: _totalNotifications),
subtitle: Text(_notificationInfo!.body!),
background: Colors.cyan.shade700,
duration: Duration(seconds: 2),
);
}
},
);
} else {
print('User decliend or has not appected permission');
}
}
@override
void initState() {
super.initState();
requestAndRegisterNotification();
_totalNotifications = 0;
}
如果您使用的是iOS模拟器,则无法接收推送通知。你需要一个真正的设备来测试这个