I'm trying to send notification to the android and Ios device as well. It's working on android devcice but something fishy with ios token , not getting notification on ios devices , I have enabled background notification for ios. In fact it was working before few days ago but suddenly stopped working on ios device. I have updated firebase_messaging plugin but still not getting notification, also visited firebase_messaging issue ,this as well .
I think there is something wrong with my ios token , because I have tested from pushtry website and throw error saying Please check device token
, but testing with android token , not getting any errors. so I could say that ios token is not generating properly.
I did everything what I could do but every time got disappointed.
Here's my test Code:
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");
}
}
}
Okay, finally I found my stupidity, what I exactly did. actually It was 2 major mistakes.
plist
file.google-service.json
) from the another and ios(google-service.plist
) from the other projects. and added server-key
to the backend from one of them. Nothing wrong with FCM token.that's why it were working for android only.
Now I have recognised my silliness and deleted duplicate firebase project one, and added configuration file from the original one and updated my server-key
to my backend. and also asking permission for notification using permission_handler
. That's all.