Sending push notifications to a targeted device - android

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 :)

Related

FCM push notifications with Parse Server

This is complicated but I'll try to be as concise as possible. I am using FCM to send push notifications to Android devices. My backend is parse server. I can successfully send pushes from the FCM console to the device. And when I send pushes from parse to https://mybackend/parse/push, it says {"result": "true" } as the response. I have checked the server logs, and it (predictably) says all http post requests to /parse/push were 200 success.
However when sending from parse (using curl or the parse push console), the pushes do NOT show up in FCM console. There is some issue between parse server and FCM then.
In my index.js, I have:
push: {
android: {
senderId: "XXXXX",
apiKey: "YYYYYY"
}
}
I have the keys, and done everything according to here: http://parseplatform.org/docs/parse-server/guide/#push-notifications
what else am I missing? Why can't I get this to actually appear on the device? Any help is greatly appreciated, thanks.
Setting up a Parse Server for an Android app, I remember having to double check this to get it right. The key for me was this: when I logged into Firebase console and selected the app I was working on, and clicked on the gear [or cog or whatever] and then Project Settings, I had to make sure to click on the Cloud Messaging tab and grab the Legacy server key [under project credentials] NOT the web api key that appears on the General tab. Also the sender ID that you want to use appears on that page.
I'm not sure when they will deprecate the Legacy server key, you might try the Server key instead, but definitely make sure you get the info off of the Cloud Messaging tab.
I don't know why FCM (and APNs) don't send a different message when the key (or cert) is not set up right [that would be more developer-friendly at least] and instead just send a 200 but my guess is that they evaluate the key [or cert] after receiving the intended notification and sending a response.

Android : Some Questions to Understand GCM well

I am Trying to fetch and push A notification (To inform the device to connect to the data source because it has been changed/Updated)
I read A lot of tutorials about it , and tried to apply it to help me in my case , but really i faced some problem understanding the GCM HOW IT Works
I've register at https://code.google.com/apis/console and take the SENDER ID AND APIKEY
Now I've Some Questions :
do you register the device to the service every time we start the application or only for the first time ?
do we use GCM for (only notify the device something happens) or to (notify and get the information about whats is changes) ?
why this operation need (Client And Server)
I mean (Client) it's OK Because he'll receive the push notification ,, but (Server What it Means Here) :( ,, because i think the server here will be the Google it self to notify the device
In this Case When We Use A APIKEY
the Idea Is :
I've SQL Server Database on my web-site and have small android application (Dealing With it) with two users
need when any user Add A record to the database notify the other user
Thanks In Advance ,,
Regards :)
1) You register with GCM only when you need to. E.g. When you call
GCMRegistrar.getRegistrationId(appContext);
And it returns a blank string. Typically this happens the first time you launch your app.
2) It depends on what the data you are trying to is and whether or not it will fit within the 4K payload limit. Take a look at the Send-to-Sync vs. Messages with Payload Google Help topic.
3) Your server is the server that sends the message to the GCM servers. The GCM servers then send the message down to the apps. Take a look at this blog post I wrote for a service that I helped create: http://blog.andromo.com/2012/how-does-airbop-push-messaging-work/ It should help explain how this works. In the explanation you can just substitute our servers with your own.
4) You send your API key as part of your message to the GCM servers as detailed here: http://developer.android.com/google/gcm/gcm.html#request
So in the following request:
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
"data" : {
...
},
}
Authorization: key=YOUR_API_KEY

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.

Android C2DM sending issues

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.

Categories

Resources