I'm trying to make GCM work with my Android application. I have some questions.
In this article in section Step 3: Write Your Application I can't see any Service. How can I start my BroadcastReceiver without a service? Should I start it? In this section there isn't any Service.
In section Writing the Server Code, in the code, there are two strings:
USERNAME = ''
PASSWORD = ''
What values should I use for them?
There is no need for a service. The broadcast receiver is started by Android when a GCM message arrives. In the old implementation there was an intent service that was called by the broadcast receiver, but it's not required anymore.
The user is <your Google API project Id>#gcm.googleapis.com. The password is the API key. However, the code sample you mentioned is for the new XMPP based API. It's simpler to use the HTTP based API which only requires the API key in the headers of the request. The code in the section you mentioned has a link to an HTTP sample.
In the HTTP API you POST an HTTP request the looks like this to https://android.googleapis.com :
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
"data" : {
...
},
}
#Tooto : If you look into the article carefully then you will find in the Manifest.xml the entry of the service class.
<service android:name=".MyIntentService" />
For more details on how to set up the GCM in your android app you can refer to this link : http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
Related
I am using AWS resources for my android project, I am planning to add push notification service for my project with AWS SNS.there are few questions bothering me much. I did not find any questions regarding these, except one or two but with unclear explanations.
1.Does AWS support FCM? SNS work with GCM. But Google recommends to use FCM instead of GCM. I did not find AWS supporting FCM.
2.Do AWS store messages (or data) into their databases even after sending push notifications?
3.I tried putting FCM api key in SNS application platform, it is showing invalid parameters why?
FCM is backwards compatible with GCM. The steps for setting up FCM on AWS are identical to the GCM set up procedure and (at least for the moment) FCM works transparently with GCM and SNS with respect to server-side configuration.
However, if you are sending data payloads to the Android device they will not be processed unless you implement a client side service that extends FirebaseMessagingService. The default JSON message generator in the AWS console sends data messages, which will be ignored by your app unless the aforementioned service is implemented. To get around this for initial testing you can provide a custom notification payload which will be received by your device (as long as your app is not in the foreground)
There are GCM-FCM migration instructions provided by Google however the changes you need to make are predominantly on the App side.
The steps you need to follow to test GCM/FCM on your app with SNS are:
Create a Platform Application in SNS, selecting Google Cloud Messaging (GCM) as the Push Notification Platform, and providing your Server API key in the API key field.
Select the Platform Application and click the Create platform endpoint button.
Provide the InstanceID (Device Token) generated by your app. You must extend the FirebaseInstanceIDService and override the onTokenRefresh method to see this within your Android App. Once you have done this, uninstall and reinstall your app and your token should be printed to the Debug console in Android Studio on first boot.
Click the Add endpoint button.
Click on the ARN link for your platform application.
Select the newly created Endpoint for your device and click the Publish to endpoint button.
Select the JSON Message Format, and click the JSON message generator button.
Enter a test message and click the Generate JSON button
Now comes the "gotcha part".
The message that is generated by SNS will be of the form:
{
"GCM": "{ \"data\": { \"message\": \"test message\" } }"
}
As we mentioned earlier, data payloads will be ignored if no service to receive them has been implemented. We would like to test without writing too much code, so instead we should send a notification payload. To do this, simply change the JSON message to read:
{
"GCM": "{ \"notification\": { \"title\": \"test title\", \"body\": \"test body\" } }"
}
(For more information about the JSON format of an FCM message, see the FCM documentation.)
Once you have done this, make sure your app is not running on the device, and hit the Publish Message button. You should now see a notification pop up on your device.
You can of course do all this programmatically through the Amazon SNS API, however all the examples seem to use the data payload so you need to keep that in mind and generate a payload appropriate to your use case.
Now you can go to your firebase console (https://console.firebase.google.com/) select your project, click the gear icon and choose project settings, then click on the cloud messaging tab...
You'll see the legacy Server Key which is the GCM API Key and you'll have the option to generate new Server Keys which are the FCM versions
SNS will accept both versions but their menu option is still categorizing it under GCM
Here is picture for your reference:
Note that you can "accidentally" remove your Server Keys but the Legacy server key is not deletable. Also, if you click the add server key button, you'll get a new server key BELOW the first one, WITH NO WARNING! ...Nice job Google ;)
One more additional note to Nathan Dunn's great answer.
How to send data with the notification from SNS to Firebase.
We need to add data to the Json (inside the notification):
{
"default": “any value",
"GCM": "{ \"notification\": { \"body\": \”message body\”, \”title\”: \”message title \”, \"sound\":\"default\" } , \"data\" : {\”key\" : \”value\", \”key2\" : \”value\” } }”
}
In your FirebaseMessagingService implementation (Xamarin example)
public override void OnMessageReceived(RemoteMessage message)
{
try
{
var body = message?.GetNotification()?.Body;
var title = message?.GetNotification()?.Title;
var tag = message?.GetNotification()?.Tag;
var sound = message?.GetNotification()?.Sound;
var data = message?.Data
foreach (string key in data.Keys)
{
// get your data values here
}
}
catch (Exception e)
{
}
}
I tried to use solution with notification payload instead of data, but I did not receive push notifications on the mobile device. I found this tutorial https://youtu.be/iBTFLu30dSg with English subtitles of how to use FCM with AWS SNS step by step and example of how to send push notifications from AWS console and implement it on php with aws php sdk. It helped me a lot.
Just an additional note to Nathan Dunn's Answer: to add sound use the following JSON message
{
"GCM": "{ \"notification\": { \"text\": \"test message\",\"sound\":\"default\" } }"
}
It took me a while to figure out how to send the notification with the right payload (publish to topic). So I will put it here.
private void PublishToTopic(string topicArn)
{
AmazonSimpleNotificationServiceClient snsClient =
new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.EUWest1);
PublishRequest publishRequest = new PublishRequest();
publishRequest.TopicArn = topicArn;
publishRequest.MessageStructure = "json";
string payload = "\\\"data\\\":{\\\"text\\\":\\\"Test \\\"}";
publishRequest.Message = "{\"default\": \"default\",\"GCM\":\"{" + payload + "}\"}";
PublishResponse publishResult = snsClient.Publish(publishRequest);
}
Amazon does support FCM as all previous code has been migrated from GCM to FCM. Below article explains in detail.
Article Published by Amazon
To answer the questions:
AWS SNS does support FCM.
No AWS does not store messages after sending push notifications.
For a detailed tutorial on setting up FCM with SNS please read this article.
I am following instructions at :
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-android-push-notification-google-gcm-get-started/#sending-push-notifications
Instructions say:
// If you want to use tags...
// Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/
// regID = hub.register(token, "tag1,tag2").getRegistrationId();
Based on that I did the following:
added regID assignment to my code
My code looks like RegistrationIntentService example via Microsoft
except the line 48
regID = hub.register(token).getRegistrationId();
is something like this
regID = hub.register(token, "tag1").getRegistrationId();
didn't really implement anything in the reference link. Reference link assumes that I have an actual server. Right now I am just testing from Azure trouble shooting test send center. (will set up server latter).
Right now from Azure trouble shooting test send center I can't send a message to 'tag1'. I know my payload is set up right but my tag is not getting registered in RegistrationIntentService.java as suggested in instructions.
Any suggestions?
Your tags are being added correctly. I have a working version with notifications that send upstream messages to tags, and the tags are created in the exact manner you describe in your question.
The problem is Azure. I have yet to find the correct way to send a payload with a tag on the test send server in Azure. The typical GCM format of
"to": "/topics/foo-bar",
"data": {
"message": "This is a GCM Topic Message!",
}
does not work. I believe this is because Azure test send server is doing something in between when it sends the request on to GCM.
When you setup your server, your tags should be working as expected.
I have been trying to send push notifications to a Phonegap app deployed on iOS and Android. iOS works fine, but Android doesn't work when I send push notifications from any of the dev consoles from PubNub, Parse and Amazon SNS.
I did verify that I can send notifications if I use the GCM API, so I am using the correct Sender ID, API key and the device token.
I don't see any errors on PubNub console. On Parse dashboard I see that the push notifications have been sent. No error on Amazon SNS. Yet, no push notifications on the device.
I am out of ideas. Thanks in advance for any helpful advice.
With help from PubNub, figured out what the issue was. The sample on PubNub has the following format
{"pn_gcm": {
"data" : {
"GCMSays" : "hi" }
}
}
But the required format was
{"pn_gcm": {
"data" : {
"message" : "hi" }
}
}
After confirming using PubNub console, I updated my JSON object in the code and it all worked like a charm.
With Parse console, I tried to create a JSON object with similar format, but it didn't work. Haven't tried Amazon SNS.
This tutorial illustrates how to configure Android GCM with PubNub: http://www.pubnub.com/docs/java/android/tutorial/google-push-notification.html
[Parse developer] Parse doesn't provide 1st party support for PhoneGap; their product just works with the JS SDK, which doesn't have native Push support. I describe what is necessary to get Parse to recognize your device for push in an arbitrary language in a previous question. You may however be receiving the push but not creating a notification.
In Android (unlike iOS, WinRT, or WinPhone), push doesn't necessarily imply "notification." The native android SDK from Parse creates this automatically for you. You'll need to create your own BroadcastReceiver and wire it to handle the intents that the Play Store app is sending for pushes. You can find these in our Android Push Tutorial. In your BroadcastReceiver, you'll want to create and register your own Notification object when receiving a push.
For Firebase Cloud Messaging, I had to use this structure to receive a tray notification:
"pn_gcm": {
"notification": {
"title": push_title,
"text": push_message
}
},
hello to all i want to use google gcm for my app .. but now is not working ..
open this url shows warning me warning ..
http://developer.android.com/google/gcm/demo.html
The information in this document has been superseded by GCM Server and GCM Client. Please use the GoogleCloudMessaging API instead of the GCM client helper library. The GCM server helper library is still valid.
m confused littel bit .... can any one provide me an latest sample code please
thank you
Use this link to set up your gcm id:
https://developer.android.com/google/gcm/gs.html
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