Android C2DM sending issues - android

I am having trouble testing out push notifications using the C2DM service.
I am using Lars' example until "3.4. Register your application" to set up my android test project. I successfully get the Registration ID from the device.
Then I successfully get the Auth token via my server using the following curl:
curl https://www.google.com/accounts/ClientLogin -d Email=theEmailYouWhitelisted -d Passwd=pass****word -d accountType=HOSTED_OR_GOOGLE -d source=Google-cURL-Example -d service=ac2dm
So now I have the two main components to push. So for the purpose of testing I issue another curl to send a message (I am going to implement the auth token generation, and sending of message in php once I get it functional).
I use:
curl --header "Authorization: GoogleLogin auth=**authFromRegistrationAbove**" "https://android.apis.google.com/c2dm/send" -d registration_id=**phoneRegistrationId(reciever)** -d "data.message=HelloPush" -d collapse_key=something -k
Response:
Error=NotRegistered
This is where I am, and confused. I successfully got my Auth Token using my white listed email / pw registered and accepted into the C2DM program via my client server. Then compiled Lars' project to generate my devices registration id, but once they are used together I get the not registered error above, and no push notification.
Ideas? Comment if you need anymore information and I'll update the post. Thanks.

If you follow me example you should maintain your registered C2DM user (Email address) in the Text Field in RegisterActivity.This must be the same as in the CURL thing. Also make sure you copy the authentication string correctly.

Did you see check this previous question already? This problem is also frequently discussed on the android-c2dm mailing list. Here's a search for NotRegistered.
A common problem is that you are not using the same sender ID on the application as the one you used to generate the auth token. Note that in the C2DM signup form, there are two email addresses you provide: one is just to notify you when you're accepted, and the other is the "Role Account". The latter is also known as the sender ID and is what must be used on the phone application and for the account used to generate the auth token.

Related

Can I register a new app to existing GCM server?

The app I am currently working on must implement push service. We have an alternative app that is developed using JavaScript and such and is developed in parallel (different package name, etc). That app already has push implemented and a server working.
I was given the server URL and a senderID which i used like this (based on guides):
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(ServerRequests.senderId,
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Then i used the token to register on the server as instructed.
The problem is I am not receiving any messages. Is there something I missed? Will it work without having to make modifications (highly unlikely) on the server ?
Check is api-key is valid using the method from here:
https://developers.google.com/cloud-messaging/http
Checking the validity of an API key
If you receive authentication errors when sending messages, check the validity of your API key. For example, on Android, run the following command:
# api_key=YOUR_API_KEY
# curl --header "Authorization: key=$api_key" \
--header Content-Type:"application/json" \
https://gcm-http.googleapis.com/gcm/send \
-d "{\"registration_ids\":[\"ABC\"]}"
If you receive a 401 HTTP status code, your API key is not valid. Otherwise you should see something like this:
{"multicast_id":6782339717028231855,"success":0,"failure":1,
"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
If you want to confirm the validity of a registration token, you can do so by replacing "ABC" with the registration token.

Sending push notifications to a targeted device

I have a mobile app (both iOS and Android) created for an online shop. This particular version of the app is for the sellers only. There is a section in it that function as a helpdesk. Basically buyers can open tickets, send messages to sellers etc. So what I want to do is every time when a buyer does that, the seller should receive a push notification on his device.
The database that keeps records of buyers, sellers and everything else runs on a separate server. I can have some sort of a cron job to periodically check for new cases opened by buyers. I'm hoping to use Parse to handle push notifications.
Now where I'm a stuck at is how to associate my database server with Parse.
My current plan is something like this.
The app is launched and the seller is logged in for the first time. Parse SDK registers the device with its servers. Alongside this, using a separate web service, I was hoping to save the unique ID generated by Parse (Upon research, I found the suitable unique ID is called Installation ID) and the logged in seller's ID in my database as well.
When the cron job finds an open case, it retrieves the seller's ID that it should go to. Along with it, that Installation ID.
My server sends out a HTTP request to post a push notification to the device with that Installation ID using Parse's REST API.
One issue. They haven't specified a way to pass an Installation ID in their docs.
I want to know if this is possible? Or if there is a better way to go about this?
Pretty sure that is possible. First you can get the installationId via the PFInstallation:
PFInstallation.currentInstallation().installationId // swift
or
[PFInstallation currentInstallation].installationId // Objective-C
Dont know about android but probably similar
ParseInstallation.getCurrentInstallation().getInstallationId() // Java
https://parse.com/docs/osx/api/Classes/PFInstallation.html https://parse.com/docs/android/api/com/parse/ParseInstallation.html
Send the push
And then you actually have to send the push: Take a look at the Parse REST guide under "using advanced targeting".
You can use a curl query like the following:
curl -X POST -H "X-Parse-Application-Id: %appId%" -H "X-Parse-REST-API-Key: %api key%" -H "Content-Type: application/json" -d '{ "where": { "installationId": "%installationId%" }, "data": { "alert": "The Giants scored a run! The score is now 2-2." } }' https://api.parse.com/1/push
The important part here is the { "where": { "installationId": "%installationId%" } which sends the push message to only the Installations that match the query - exactly the one installation with the given id.
I just tried and got a push message only on specific device :)

Android GCM send and MismatchSenderId

The odd thing is that notifications were working before. So I'm not sure what happened. I tried reinstalling my app to the device, but nothing changed.
{"multicast_id":xxxxxxxxxxxxxxxxxxx,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
Because this was working at one point, I know I am using the correct keys. I even triple checked based on other SOF threads I came across. What could be the potential problem now? I've looked around for a few days now and I'm still at a loss..
** EDIT **
Here's the POST info when a message is sent out from my server.
Headers
Array
(
[0] => Authorization: key=xxxxxxx-key-for-browser-apps-from-google-apis-console-xxxxxxx
[1] => Content-Type: application/json
)
Fields
Array
(
[registration_ids] => Array
(
[0] => xxxxxxxxx-big-old-id-from-the-device-xxxxxxxx
[data] => Array
(
[message] => hello
)
)
Also
I am using the key associated with Key for browser apps (with referers).
Apparently, when you recompile your app it will sometimes throw off the device id generation to where it won't match a previous id.
What I have to do is a COMPLETE uninstall of the app on my phone and recompile the app again (via eclipse). This seems quite pathetic, but it's the closest to a workable solution I have at the moment..
I think it must be something to do with the way you have set up API access for your project in the Google APIs console, in particular the 'allowed referers'. As the dev docs say
Mismatched Sender A registration ID is tied to a certain group of
senders. When an application registers for GCM usage, it must specify
which senders are allowed to send messages. Make sure you're using one
of those when trying to send messages to the device. If you switch to
a different sender, the existing registration IDs won't work. Happens
when error code is MismatchSenderId.
My project is set up to allow any referer like:
Key for browser apps (with referers)
API key: my secret
Referers: Any referer allowed
Perhaps you have restricted it to a certain IP range, and now you are trying to send the message from outside that range
you did not use right device token, that's why you got the error.
So, please get the new device token (Android mobile's token) and use it
curl -X POST \
-H "Authorization: key= write here api_key" \
-H "Content-Type: application/json" \
-d '{"registration_ids": ["write here reg_id generated by gcm"],
"data": { "message": "Manual push notification from Rajkumar"},
"priority": "high"
}' \
https://android.googleapis.com/gcm/send
MismatchSenderId because with in same device you have logged with different keys. to solve this problem uninstall app and run it againg and update the registration key. and then run the CURL script in your teminal which i post above it will give success message and you will get notification to your device

Android Gcm Not working with ROR

I read basic example for implementing google gcm. I developed small client side android application which successfully gives me registration id from gcm server.. now on server side I am using Ruby on rails hence we use this gem for implementing server side of gcm https://github.com/dondeng/gcm_on_rails. I pass my registration id to to my server.
I successfully ran these commands
device = Gcm::Device.create(:registration_id => "XXXXXXXXXXXXXXXXXXXXXX")
notification = Gcm::Notification.new
notification.device = device
notification.collapse_key = "updates_available"
notification.delay_while_idle = true
notification.data = {:registration_ids => ["RegistrationID"], :data => {:message_text => "Get on cloud nine"}}
notification.save
used to deliver notifications:
$ rake gcm:notifications:deliver
my rake command running without any error. But after completion of all these tasks my device not receiving any message.. I don't know whats going on in background. How to confirm that sent message properly receive by device.. Need Help... Thank you....
Debugging GCM messages can be cumbersome and difficult.
One possible reason is a wrong API key for GCM, which must be defined in the configuration files. If you use the wrong API key, then you might get a success response from the server {:code=>200, :message=>nil}, although no message has been sent. In this case go to the Google API Console and look up the right key.
Another possible reason is a wrong REGISTRATION_ID for the device. if the REGISTRATION_ID is not correct you may get an NotRegistered or InvalidRegistration error, which is not always recognized by the gcm_on_rails plugin (at the moment). Use the following curl command to debug the API through the console (insert your API_KEY and the REGISTRATION_ID of the device).
curl --header "Authorization: key=API_KEY" \
--header Content-Type:"application/json" \
https://android.googleapis.com/gcm/send \
-d "{\"registration_ids\":[\"REGISTRATION_ID\"],
\"data\":{\"message_text\":\"Test, Test\"}}"
A successful / unsuccessful attempt to call the Gcm API should look like this:
{"multicast_id":7961..,"success":1,"failure":0,"canonical_ids":1,
"results":[{"registration_id":"..","message_id":"0:.."}]}
{"multicast_id":7961..,"success":0,"failure":1,"canonical_ids":0,
"results":[{"error":"NotRegistered"}]}
Finally another reason is an invalid key in the day: do not use a reserved word like message_type as a key in the data hash. It will return a status code 200, but it will not deliver any notification. message_type is one of the advanced options (took me more than an hour to find that out).
If you have no error on server-side, maybe you are not handling correctly the event in your Android App.
Follow this guidelines: http://developer.android.com/guide/google/gcm/gs.html#android-app, and write logs for debug.

Why do I get "MismatchSenderId" from GCM server side?

I'm trying to create a push service for my Android app, and I follow Google GCM's documentation and example for this matter:
I can register/unregister my Android app. From my server side, I can see that I have one subscription registered, but when I try to send a message from server to my Android app I always get following error:
Error sending message to device #0: MismatchSenderId
For my Android app, I use SENDER_ID = 200000000001.
And for my server side as Google says I use API_KEY = AIzxxxxxxxxxxxxxxxxXxxXxxXxxxXXXXXxxxxs
I followed this document:
http://developer.android.com/guide/google/gcm/demo.html
I've enabled GCM in my Google API panel too, yet I'll get that annoying error message.
How can I fix this problem?
Did your server use the new registration ID returned by the GCM server to your app? I had this problem, if trying to send a message to registration IDs that are given out by the old C2DM server.
And also double check the Sender ID and API_KEY, they must match or else you will get that MismatchSenderId error. In the Google API Console, look at the URL of your project:
https://code.google.com/apis/console/#project:xxxxxxxxxxx
The xxxxxxxxx is the project ID, which is the sender ID.
And make sure the API Key belongs to 'Key for server apps (with IP locking)'
Mismatch happens when you don't use the numeric ID. Project ID IS NOT SENDER ID!! It took me 9 hours to figure this out. For all the confusion created by google, check the following link to get numeric id.
https://console.cloud.google.com
instead of
https://console.developers.google.com
Hope it helps!!
Update:-
Things have changed again. Now the sender id is with firebase.
Go to https://console.firebase.google.com and select your project. Under settings -> cloud messaging, you can find the sender id.
And it works!
Please run below script in your terminal
curl -X POST \
-H "Authorization: key= write here api_key" \
-H "Content-Type: application/json" \
-d '{
"registration_ids": [
"write here reg_id generated by gcm"
],
"data": {
"message": "Manual push notification from Rajkumar"
},
"priority": "high"
}' \
https://android.googleapis.com/gcm/send
it will give the message if it is succeeded or failed
I encountered the same issue recently and I tried different values for "gcm_sender_id" based on the project ID. However, the "gcm_sender_id" value must be set to the "Project Number".
You can find this value under: Menu > IAM & Admin > Settings.
See screenshot: GCM Project Number
This happens when the Server key and Sender ID parameters HTTP request do not match each other. Basically both server ID and Server key must belong to the same firebase project. Please refer to the below image. In case of mixing these parameters from deferent Firebase projects will cause error MismatchSenderId
InstanceID.getInstance(getApplicationContext()).getToken(authorizedEntity,scope)
authorizedEntity is the project number of the server
Your android app needs to correct 12-digit number id (aka GCM Project Number). If this 12-digit number is incorrect, then you will also get this error.
This 12-digit number is found in your Google Play Console under your specific app, 'Service & API' -> 'LINKED SENDER IDS'
Check google-services.json file in app folder of your Android project. Generate a new one from Firebase console if you are unsure. I got this error in two cases.
I used a test Firebase project with test application (that contained right google-services.json file). Then I tried to send push notification to another application and got this error ('"error": "MismatchSenderId"'). I understood that the second application was bound to another Firebase project with different google-services.json. Because server keys are different, the request should be rewritten.
I changed google-services.json in the application, because I wanted to replace test Firebase project with an actual. I generated right google-services.json file, changed request, but kept receiving this error. On the next day it fixed itself. I suspect Firebase doesn't update synchronously.
To get a server key for the request, open https://console.firebase.google.com and select an appropriate project.
Then paste it in the request.
With the deprecation of GCM and removal of its APIs, it appears that you could see MismatchSenderId if you try to use GCM after May 29, 2019. See the Google GCM and FCM FAQ for more details.
Use sender ID & API Key generated here: http://developers.google.com instead (browse for Google Cloud Messaging first and follow the instruction).
If use for native Android, check your AndroidMaifest.xml file:
<meta-data
android:name="onesignal_google_project_number"
android:value="str:1234567890" />
<!-- its is correct. -->
instead
<meta-data
android:name="onesignal_google_project_number"
android:value="#string/google_project_number" />
Hope it helps!!

Categories

Resources