stop gcm to send notification on old registration id - google-cloud-messaging

Suppose that case when the client app has re-registered with the gcm itself but the server is unaware of this and now the server is sending notification to that particular client what I want is the notification should not be sent with the old registration id. How can this be done?

If your server sends a message to an invalid Registration token, it will probably receive a NotRegistered error response. You should handle this error accordingly by removing the corresponding token. As per the documentaion I linked above:
For all these cases, remove this registration token from the app server and stop using it to send messages.

Related

Google GCM server returns Unauthorized Error 401 while subscribing

I get subscription success in app. But I see Google GCM server returns Unauthorized Error 401 in the server logs.
When I push a message from adapter, I get an authorization failure in server logs. But My adapter gives success response Notification sent to set user.
Can somebody tell me what could be the mistake I would be doing. (can this error also occur when my firewall is restricting this call.)
While subscribing i was getting Reason: 401 unable to authenticate with gcm services. something like this. And While pushing a notification from adapter i was getting reason : unable to authenticate for Api key "ASYI-" with GCM. the error messages may not be exact but the message means just like above
I am getting below error while sending notification
Failed to invoke Google GCM push service.
GCM Service invocation failed (reason: Authentication has failed for sender key starting with 'AIzaSyCk')
also getting below error
GCM push token 'ssdsddsdsdsds' is not added to GCM notification key. GCM Service invocation failed (reason: Returned HTTP/1.1 401 Unauthorized when invoking GCM push service to get the notification key.)
GCM Service invocation failed (reason: Returned HTTP/1.1 401 Unauthorized when invoking GCM push service to get the notification key.)
on the Android studio i see below error :
GCMAPIClient$BackgroundRegistrationHandler.run in GCMAPIClient.java:130 :: Failed to register with GCM using Google Play Services. Error:Error :INVALID_SENDER
Finally it worked for me, two things i did it to work.
Took the GCM.jar file from the sample project and placed it in my project . removed the other gcm dependencies
Created a project in FCM Console (https://console.firebase.google.com/)
and took the google-services.json and placed it in my android studio project under app. Took the web API key from FCM Console and placed it in the mobilefirst app project.(There are two keys now one for app and other for web).
I had to create a new project in the fcm console and new keys..

Any way to get more details on GCM failure?

I'm currently working on a push notification API that will work with several apps at once, handling notifications and reducing programming time for future apps. It's already partially working, as I'm able to register and receive notifications on Android devices.
Eventually, one of our apps is gonna send broadcast notifications to registered users. But some tokens might be expired, which will lead to a GCM failure. I already tested, and it seems that sending an array of tokens to GCM with a single http call is working really well, as devices with valid tokens got their notifications.
What I wasn't able to find searching GCM documentation was a way to get more details in case of failure. For example, when I send a notification to two users, one with a valid token and the other with an invalid one, I got this result :
{
"multicast_id":7625209716676388798,
"success":1,
"failure":1,
"canonical_ids":0,
"results":[
{"error":"InvalidRegistration"},
{"message_id":"0:1466511379030431%c4718df8f9fd7ecd"}
]
}
We can see that one of the messages failed to send, but what I'm looking for is a way to get more details, ideally the token that leads to a failure, so I can remove it from my database.
Any way to achieve that ? Using the message_id maybe ? Or is there any solution for me to find invalid tokens stored in my database so I can clear them ? I might have missed something in the documentation, even a link to it would be useful.
Based from this documentation, the GCM server will respond to your server with some information about the token you used to try to send the push notification.
According also to this link, if the app server fails to complete its part of the registration handshake, the client app should retry sending registration token to the server or delete the registration token. Wiping old tokens from the GCM servers can be done with ÌnstanceID.deleteToken().
Check these links:
How to remove gcm id from server which is not used
GCM get invalid tokens when sending to multiple devices at once

Server got InternalServerError, but device receive the notification

I implemented a GCM server to send notification, as well as a GCM Android client to receive notification.
Most of the time, they were working good. But some times the server got InternalServerError response, like that:
{"multicast_id":7727132596694632507,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InternalServerError"}]}
When got this reponse,
1. some times the device can not receive notification.
2. but some times the device can receive notification(event InternalServerError responsed).
I know should retry send when got InternalServerError. But that may cause duplicated notification sending for the situation 2.
How to resolve that?

How to tell if GCM token is valid, without sending a notification?

Is there a way to tell when a GCM token is no longer valid, without actually attempting to send a notification and receive an error?
Thanks.
You can do this by using the dry run feature provided by the google.
When sending request for a notification from your web server to GCM servers you can add dry_run key to be true
This parameter allows developers to test a request without actually sending a message. The default value is false, and it must be a JSON boolean.
If the Notification key is invalid it would give the corresponding http response of the request

Trigger.io doesn't expose the user's agreement to notifications in iOS

I am building a trigger.io app (ios+Android) with notifications through parse.com.
Notifications are working, but I am unable to test for whether the user has agreed to notifications before trying to subscribe to a channel with forge.parse.push.subscribe. This is generally only a problem on first run of the app.
forge.parse.installationInfo will return an info.id, even if that user hasn't agreed to notifications, so it can't be used to test for agreement:
forge.parse.push.subscribe throws an error if the user hasn't agreed to notifications:
{ "message":"Subscribing to Parse channel failed: Error Domain=Parse Code=115
\\"The operation couldn’t be completed. (Parse error 115.)
\\" UserInfo=0x165ddbc0 {code=115,
error=There is no device token stored yet.}","type":"UNEXPECTED_FAILURE"}'
I could set a timeout to try subscribing again later in case the user has since accepted notifications, but I'm not happy with having to send multiple REST requests to parse.com to see if the user has agreed to notifications.
forge.parse.registerForNotifications just delays the registration process, but doesn't throw an error if the user doesn't agree to notifications.
Trigger.io displays a console event when the user accepts:
Initializing Parse and subscribing to default channel.
Can this also expose an event to forge to be accessed in the js when the user agrees to notifications?
Or should forge.parse.installationInfo throw an error if the user doesn't have notifications on?