为什么不使用flutter中的firebasemessaging插件在ios中获取通知



我正在尝试向android和Ios设备发送通知。它在android devcice上工作,但ios代币有点可疑,在ios设备上没有收到通知,我已经为ios启用了后台通知。事实上,几天前它还可以工作,但突然在ios设备上停止了工作。我已经更新了firebase_messaging插件,但仍然没有收到通知,还访问了firebase_messaging问题,这也是。

我认为我的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");
}
}
}

好吧,我终于发现了我的愚蠢,我到底做了什么。实际上是两个大错误。

  • 首先,当我将远程通知添加到plist文件时,我使用了permission_hander,并且没有请求通知的权限,因为我认为这将是自动请求
  • 其次,我的巨大愚蠢,我创建了2个firebase项目,从另一个项目中添加了android(google-service.json(和ios(google-service.plist(的配置文件。并将CCD_ 5从其中一个添加到后端。FCM代币没有问题

这就是为什么它只适用于android。

现在我已经意识到了我的愚蠢,删除了重复的firebase项目一,并从原始项目中添加了配置文件,并将我的server-key更新到了后端。并且还请求使用CCD_ 7进行通知的许可。仅此而已。

最新更新