【发布时间】:2021-12-21 21:20:35
【问题描述】:
我也在尝试向 android 和 Ios 设备发送通知。 它在 android 设备上工作,但是 ios 令牌有些可疑,没有在 ios 设备上收到通知,我已经为 ios 启用了后台通知。实际上它在几天前就可以工作,但突然停止在 ios 设备上工作。我已经更新了firebase_messaging 插件但仍然没有收到通知,还访问了firebase_messaging issue,this as well。
我认为我的 ios 令牌有问题,因为我已经从 pushtry 网站测试并抛出错误说 Please check device token ,但是使用 android 令牌进行测试,没有收到任何错误。所以我可以说 ios 令牌没有正确生成。
我做了我能做的一切,但每次都感到失望。
这是我的测试代码:
class PUSHTest extends StatefulWidget {
const PUSHTest({Key key}) : super(key: key);
@override
_PUSHTestState createState() => _PUSHTestState();
}
class _PUSHTestState extends State<PUSHTest> {
String token = '';
var serverKey =
'AAAAL4uGYoY:.............................';
@override
void initState() {
super.initState();
getToken();
showAlertNotification();
}
getToken() async {
String t = await FirebaseMessaging.instance.getToken();
print(
"FCM TOKEN: $t");
setState(() {
token = t;
});
}
showAlertNotification() {
FirebaseMessaging.instance
.getInitialMessage()
.then((RemoteMessage message) {
if (message != null) {
print("NOTIFICATIONNNNNNNNNN RESPONSE11${message.data}");
}
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
RemoteNotification notification = message.notification;
AndroidNotification android = message.notification?.android;
AppleNotification ios = message.notification?.apple;
print("ios ios ios:$ios");
if (notification != null && android != null && !kIsWeb) {
if (message != null) {
print("NOTIFICATIONNNNNNNNNN RESPONSE22${message.data}");
}
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
if (message != null) {
print("NOTIFICATIONNNNNNNNNN RESPONSE33${message.data}");
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => sendNotification(),
child: Text("SEND NOTIFICATION"),
),
),
);
}
// My FCM Payload To send notification to the device:
sendNotification() async {
print("send notification button pressed");
try {
http.Response response = await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$serverKey',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': 'this is a body',
'title': 'this is a title',
"content_available": true
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': token,
},
),
);
if (response.statusCode == 200) {
print("SENT NOTIFICATION TO THE DEVICE :$token");
Fluttertoast.showToast(msg: "SENT NOTIFICATION TO THE DEVICE :$token");
} else {
print("error push notification");
Fluttertoast.showToast(msg: "error push notification");
}
} catch (e) {
print("error push notification");
}
}
}
【问题讨论】:
-
让您拥有通知权限的用户。
-
您是否在 Firebase 控制台中添加了您的 APN 证书?它也不适用于 iOS 模拟器
标签: flutter dart push-notification firebase-cloud-messaging apple-push-notifications