Firebase Cloud Messaging send message to iid imported Apns token - ios

I'm writing an application which should send push notifications from the server to android and iOS devices. I try to use FCM for this purpose.
iOS application used APNS so I can obtain apns subscription tokens on server side. Then I try to batch import these tokens, I get back some kind of registration tokens with length of 174 symbols instead of usual 152 used in Firebase.
When I use IID method GET https://iid.googleapis.com/iid/info/{token}, it successfully returns information about given token, but when I try to send message directly using this token by using HTTP v1 API POST https://fcm.googleapis.com/v1/projects/{0}/messages:send method with target field "token":"f5MpeWlqg0E:APA91bHGpRR3_ygt10....CdI3Rtsodf8XMIQNlflF23mQ" and ios specific payload, I get an error:
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
},
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.token",
"description": "Invalid registration token"
}
]
}
]
}
}
Then, if I try to send same message to the same token again, I get another error:
{
"error": {
"code": 404,
"message": "Requested entity was not found.",
"status": "NOT_FOUND",
"details": [
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "UNREGISTERED"
}
]
}
}
And IID method returs 404 too, meaning that Firebase removed my token from system.
The question is why do I get "wrong" token from IID that I cannot address and how can I send direct notifications to iOS phones.

Ok, the problem was in iid settings: when I tried to retrieve FCM token from APNS token I set "sandbox" to false, but used sandbox certificate, so the token was invalid.

Related

Sending data message to iOS device results in FirebaseMessagingException: Request contains an invalid argument

When we are sending a FCM data message with the Firebase Admin SDK to an iOS device we are getting the following exception.
com.google.firebase.messaging.FirebaseMessagingException: Request contains an invalid argument.
We are using com.google.firebase:firebase-admin:6.7.0 and the message is build like this.
Message.builder()
.setApnsConfig(ApnsConfig.builder()
.putHeader("apns-priority", "10")
.setAps(Aps.builder()
.setContentAvailable(true)
.build())
.build())
.putAllData(data)
.setToken(token)
.build();
I don't think that there is something wrong with the token itself, because sending a push notification to this token works.
Also the message size shouldn't be an issue, because it is basically just an id in it.
I'm not sure if it happens on all iOS devices.
Am i missing something on building the data message?
Update
I checked the details included at the exception and there it looks like something is wrong with the token.
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
},
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.token",
"description": "Invalid registration token"
}
]
}
]
}
}
But when i send a data message to the same token using postman, there is no error and the response looks like this.
{
"multicast_id": 7455611938954436367,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1614145427166557%c5757de0f9fd7ecd"
}
]
}
I think i figured it out.
My problem was that i was setting the apns-priority to "10". According to the documentation, notifications with a priority of "10" have to be delivered immediatly.
If i put my device into flight mode for example, and sending my data push it failes with the mentioned firebase exception about an invalid token. Which is very missleading in my opinion.
If i set the apns-priority to "5" everything works fine.
It may happen if you try to use a push notification with only data (silent push notification) which do not required the user to have granted permission to receive on iOS, but you set some iOS specific parameters in the request.
In this case, the error does not occur if the user has granted permission to receive push notifications or you don't specify any iOS specific parameter.

Firebase Cloud Messaging Token error but up to date token

when I send push from device A to device B . Push fails with error:
404 {
"error": {
"code": 404,
"message": "Requested entity was not found.",
"status": "NOT_FOUND",
"details": [
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "UNREGISTERED"
}
]
}
}
According to documentation this error occurs when FCM Token is not valid. However in delegate method didReceiveRegistrationToken I receive the same token which I use in the push, so FCM token is up to date. Or maybe I'm missing something. Any help will be very appreciated.

Using Postman with Firebase Cloud Messaging to send a push notification to a specific iOS device

I'm trying to send a push notification to a specific iOS device using Postman as a trigger to send a request to Firebase Cloud Messaging.
As a response I get an error saying that I have an invalid APNS credential (InvalidApnsCredential). What am I doing wrong?
The url I'm sending the request to is:
https://fcm.googleapis.com/fcm/send
The request headers look like this:
Content-Type: application/json
Authorization: key=[SERVER_KEY]
[SERVER_KEY] is the server key I took from the Firebase console project settings.
The body of the request:
{
"to": "[REFRESH_TOKEN]",
"notification": {
"body": "Test body",
"title": "Test title",
"content_available": true,
"priority": "high"
}
}
Where [REFRESH_TOKEN] is the refresh token I extract from the FIRInstanceID:
NSString *token = [[FIRInstanceID instanceID] token];
The full actual response that is returned:
{
"multicast_id": XXXXXXXXXXXXXXXXXXX,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidApnsCredential"
}
]
}
The multicast_id is some number I just covered up.
I issued fresh production and development certificates and added them to the Firebase console, still the same error. Also, tried with APNs Authentication Key, but to no avail.

Cannot delete comment on Youtube video via API

I'm trying to delete followiing comment z12mdhjgxrfgtpf3d231jrz5vo3jdj0lo on this video but the response code is 400. I'm using token of the channel's admin with https://www.googleapis.com/auth/youtube.force-ssl scope as it required by documentation.
The request is:
DELETE https://www.googleapis.com/youtube/v3/comments?id=z12mdhjgxrfgtpf3d231jrz5vo3jdj0lo&alt=json
{
"query_params": {
"id": "z12mdhjgxrfgtpf3d231jrz5vo3jdj0lo",
"alt": "json"
}
}
The response:
{
"error": {
"errors": [
{
"domain": "youtube.comment",
"reason": "processingFailure",
"message": "The API server failed to successfully process the request. While this can be a transient error, it usually indicates that the requests input is invalid.",
"locationType": "parameter",
"location": "id"
}
],
"code": 400,
"message": "The API server failed to successfully process the request. While this can be a transient error, it usually indicates that the requests input is invalid."
}
}
What is wrong with the request?
That's token debug info:
{
issued_to: "493985796851.apps.googleusercontent.com",
audience: "493985796851.apps.googleusercontent.com",
user_id: "109704411339866376304",
scope: "https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtube.upload",
expires_in: 2452,
access_type: "offline"
}
The reason you're not able to delete the comment is that you're using Comments.delete to remove comment which was written by other users . Comments.delete only deletes your own comments in your video.
To delete comment of other users, use Comments.setModerationStatus and set moderationStatus to "rejected".
So just repeat what you did but this time using Comments.setModerationStatus. There's a Try-it in the link for testing purposes.

Live Connect API for fetching Client ID

In the Facebook API, there is a request that provides details about the application that a particular OAuth access token is for. For example, I could send
http://graph.facebook.com/app?access_token=ABC...123
and it would return information about the application that the access token was generated for. It has a very similar one to get information about the user the access token was generated for.
http://graph.facebook.com/me?access_token=ABC...123
My question pertains to the parallel calls in Microsoft's Live Connect RESTful API. There is a direct equivalent to the /me request that returns the user's unique ID, but I cannot find any documentation for a parallel to the /app request (client in MS terminology).
https://apis.live.net/v5.0/me?access_token=ABC...123
{
"id": "1234abc1a1abc123",
"name": "test test",
"first_name": "test",
"last_name": "test",
"gender": null,
"locale": "en_US"
}
https://apis.live.net/v5.0/app?access_token=ABC...123
{
"error": {
"code": "request_url_invalid",
"message": "The URL contains the path 'app', which isn't supported."
}
}
https://apis.live.net/v5.0/client?access_token=ABC...123
{
"error": {
"code": "request_url_invalid",
"message": "The URL contains the path 'client', which isn't supported."
}
}

Resources