I was trying to send a notification from one device to another device to let the user know there is a waiting message for him.
When I was trying to catch and handle the RemoteMessage this is what console prints. It happens on iOS physical devices.
That function works perfectly before but now it is impossible to send notification to other user over the device, client side.
Function overview:
static Future<void> sendNotificationToOtherUser(
String title, String body, String token) async {
final headers = {
'content-type': 'application/json',
'Authorization':
'key={I hide KEY but I assure you there is nothing wrong with key}'
};
List<String> list = List();
// token is not null
list.add(token);
final postUrl = 'https://fcm.googleapis.com/fcm/send';
final data = {
"registration_ids": list,
"notification": {
"title": '$title',
"body": '$body',
},
};
final response = await http.post(Uri.parse(postUrl),
body: json.encode(data),
encoding: Encoding.getByName('utf-8'),
headers: headers);
if (response.statusCode == 200) {
// it prints 200 so works perfectly
print(response.body);
print('test ok push CFM');
return true;
} else {
print(response.body);
print(' CFM error');
// on failure do sth
return false;
}
}
It seems everything works but other device cannot catch Notification and shows it due to error I put below. Error:
[connection] nw_endpoint_handler_set_adaptive_read_handler [C6.1 216.58.212.42:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for read_timeout failed
[connection] nw_endpoint_handler_set_adaptive_write_handler [C6.1 216.58.212.42:443 ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for write_timeout failed
Libraries:
firebase_core: ^1.0.3
firebase_auth: ^1.0.2
cloud_firestore: ^1.0.4
firebase_storage: ^8.0.1
firebase_messaging: ^10.0.0
Flutter SDK version:
[✓] Flutter (Channel stable, 2.2.2, on macOS 11.4 20F71 darwin-x64, locale
en-GB)
• Flutter version 2.2.2 at /Users/user/development/flutter
• Framework revision d79295af24 (4 weeks ago), 2021-06-11 08:56:01 -0700
• Engine revision 91c9fc8fe0
• Dart version 2.13.3
I think issue solved,summarized as the following:
Use getToken() instead of getAPNSToken(). Using Firebase Messaging SDK to send messages, only tokens from getToken() will valid.
Device token renews itself if one of the following happens: (1).The app deletes Instance ID (2).The app is restored on a new device (3).The user uninstalls/reinstall the app (4).The user clears app data.
Reference https://firebase.google.com/docs/cloud-messaging/android/client for detail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With