It's been a while since I created a Android app with GCM integrated. Previously in the API console you would specify the server IP as a white list. However on my latest project this option is not there. Instead it asks for the SHA1 from the signing key which I have configured.
However when I try to send a notification to a registered device ID I get
"There was an error authenticating the sender account"
I am using the correct API key so I can't see what is wrong.
Previously in the API console you would specify the server IP as a white list. However on my latest project this option is not there
This section is found when you generate a SERVER API key in your GDC.
"There was an error authenticating the sender account"
Found this on GCM Authentication guide. Make sure these are implemented.
A message request is made of 2 parts: HTTP header and HTTP body.
The HTTP header must contain the following headers:
Authorization: key=YOUR_API_KEY Content-Type: application/json for
JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text.
If Content-Type is omitted, the format is assumed to be plain text.
For example:
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data" : {
...
},
}
Additional Note:
Authentication Error 401 - The sender account used to send a message couldn't be authenticated.
Possible causes are:
-Authorization header missing or with invalid syntax in HTTP request.
-Invalid project number sent as key.
-Key valid but with GCM service disabled.
-Request originated from a server not whitelisted in the Server Key IPs. Check that the token you're sending inside the Authentication
header is the correct API key associated with your project. See
Checking the validity of an API
Key
for details.
Google Cloud Messaging defaults to Firebase Cloud Messaging (FCM) now. Use the Server Key provided in the Firebase Console.
Before that, you may also need to Import your Google Cloud Project into Firebase if you have one.
Import instructions (only follow the "Migrate your console project" section)
Related
I'm following Google Cloud messaging tutorial, I'm not exactly understang phrase:
First, make sure your Server key (not the client API key in googleservices.json) is provided as the value of API_KEY in GcmSender.java.
So in order to run this sample, I need to write a server side? I thought that GcmSender.java already emulates server side of the application? Can someone clarify it?
As #MohanadMohie you should instead use FCM.
First, make sure your Server key (not the client API key in googleservices.json) is provided as the value of API_KEY in GcmSender.java
The GcmSender.java does emulate the function for sending a message. This only states that you should make sure that the API key you use is a valid Server Key.
And as per Server Keys go, there is already a note in the GCM docs saying:
Starting from Sept. 2016 new server key can only be created in the Firebase Console using the Cloud Messaging tab of the Settings panel. Existing projects that need to create a new server key can be imported in the Firebase console without affecting their existing configuration.
Plus, in FCM, you'll be able to make use of the Firebase Console to send push notifications.
I have used gcm demo code for viewing working of different features of gcm.But unfortunately it is showing some error.My client side is gcm quickstart code ,which is working finely,successfully sent notification using command code(gcm sender) .But i have few doubts-
Is it possible to send message/notification from gcm demo code to gcm quickstart?
2.I have used sender id and api key of gcm quickstart for sending notification from gcm demo.
3.Is there any correction required in gcm demo?
As GCM Reference 401 is authentication issue and possible problems are explained below;
The sender account used to send a message couldn't be authenticated.
Possible causes are:
Authorization header missing or with invalid syntax in HTTP request.
Invalid project number sent as key.
Key valid but with GCM service disabled.
Request originated from a server not whitelisted in the Server Key IPs.
Check that the token you're sending inside the Authentication header
is the correct API key associated with your project. See Checking the
validity of an API Key for details.
I am confused on relationship between registration id and tokens. In the tutorial for GCM from Google, we register for a registration id in the beginning. However, we also get a token. Now, in the diagrams, we send the registration id to the targeted server. However, do we also send the token? I know that the token is derived from the registration id. Is the token used as an authentication mechanism between GCM and the app and the server never knows about the token?
If you are looking for a basic knowledge about Google Cloud Messaging, IMO, you can refer to the following:
Basically, you need to do the steps:
Create a new project at Google Developers Console . At this
step, for simplicity, you just need to take note of 2 values: Project Number, which
will be used as SENDER_ID in the client project; and API server key (created at Credentials), which
will be used as API_KEY in the server project.
Create a new simple Android project for server side (with basic source code as my answer in the following links).
Create a new simple Android project for client side (with basic source code as my answer in the following links, I customized from the original source at Google Cloud Messaging - GitHub).
Run the client app, you will get the registration token (means that your device has successfully registered). Then, paste (hard-code) this token at CLIENT_REGISTRATION_TOKEN variable in server app (or write code to send this token to server app).
You can read more at the following questions, one of them you have read before with one of your previous questions:
How to implement a GCM Hello World for Android using Android Studio
Adding Google Cloud Messagin (GCM) for Android - Registration process
For more information:
Key Concepts from Google Cloud Messaging: Overview
Credentials
Sender ID A unique numerical value created when you configure your API project (given as "Project Number" in the Google Developers Console). The sender ID is used in the registration process to identify an app server that is permitted to send messages to the client app.
API Key An API key saved on the app server that gives the app server authorized access to Google services. In HTTP, the API key is
included in the header of POST requests that send messages. In XMPP,
the API key is used in the SASL PLAIN authentication request as a
password to authenticate the connection. You obtain the API key when
you configure your API project.
Registration Token An ID issued by the GCM connection servers to the client app that allows it to receive messages. Note that
registration tokens must be kept secret.
Hope this helps!
GCM now uses the concept of an InstanceID which represents a single install of an app on a device (Android or iOS). Each InstanceID can issue several tokens. These tokens are used to identify the InstanceID and can expire and be refreshed.
On the client device, you initialize an InstanceID, then with that InstanceID you generate a token (registration token). You send that token to your server, which uses the token to send messages to the InstanceID (installed application). If that token is invalidated for any reason like the application is uninstalled or the token is compromised, a new token should be generated and sent to your server.
I am still successfully registering with registration Id method until 11 May 2016
if (checkPlayServices()) {
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);
if (regid.isEmpty()) {
registerInBackground();
}
} else {
Log.i(TAG, "No valid Google Play Services APK found.");
}
APA91bHLUfr71D6K7VTrRH3LGiLFxGNr3qRi3xOB_yNl0fLYsqhlgYXxHzOhQx2WKgqZI3sqxa1ZPORa0-5YBZ1_OFLm9cEg1bTh7wtrpCsHW91MSs2BMIXrHEqyjj2TeoVxnAzA5U8s
I am working on push notifications with Google Cloud Messaging(GCM). I am able to setup server and client side following this tutorial.
The server runs on Apache Tomcat6.0 (localhost) and Android emulator on Google APIs (API level 17). I have a sender id and API key. When run from the emulator, I get a successful device connection message. Sooner after that, upon sending message from server shows the following error.
com.google.android.gcm.server.InvalidRequestException: HTTP Status Code: 401()
com.google.android.gcm.server.Sender.sendNoRetry(Sender.java:211)
com.google.android.gcm.server.Sender.send(Sender.java:125)
com.google.android.gcm.demo.server.SendAllMessagesServlet.doPost(SendAllMessagesServlet.java:83)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
From other posts, I understand wrong API key is the culprit. But in my case, I made sure I am using the right one from the API console.
By the way, when I run the emulator, I saw a message 'sending regId to server'. What is the registration id? It is not same as sender id; looks like an encoded one.
Any break through?
EDIT
The issue is finally solved! Ant was not properly building the WAR file. So API key hardly gets updated. It was hell of a nightmare to spot the bug. Thanks all for the inputs!
I suggest you to read the GCM guide:
GCM guide for Android
The sender ID is a constant that is going to be used in the comunication between GCM and your server. The regsitratiomID (regID) is managed by the GCM service when your phone registers to his service.
A better definition:
http://developer.android.com/google/gcm/gcm.html
Sender ID:
A project number you acquire from the API console, as described in Getting Started. The sender ID is used in the registration process to identify an Android application that is permitted to send messages to the device.
Registration ID:
An ID issued by the GCM servers to the Android application that allows it to receive messages. Once the Android application has the registration ID, it sends it to the 3rd-party application server, which uses it to identify each device that has registered to receive messages for a given Android application. In other words, a registration ID is tied to a particular Android application running on a particular device.
The sender account that you're trying to use to send a message couldn't be authenticated. Possible causes are:
Authorization header missing or with invalid syntax.
Invalid project number sent as key.
Key valid but with GCM service disabled.
Request originated from a server not whitelisted in the Server Key
IPs.
Check that the token you're sending inside the Authorization header is the correct API key associated with your project. You can check the validity of your API key by running the following command:
api_key=YOUR_API_KEY
curl --header "Authorization: key=$api_key" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"ABC\"]}"
If you want to confirm the validity of a registration ID, you can do so by replacing "ABC" with the registration ID.
Happens when the HTTP status code is 401.
The Sender ID is the project number what you have created under Google console.
Registration ID
When your application registers to the GCM it gets the registration ID. To deliver the message this registration ID will be used by the GCM to identify your device + your application.
Your android app should send this registration ID to your server so that when server want to send you a message it can tag it with the registration ID, and hence the GCM can deliver it to the right device & right app.
Diagnose whether the registration ID is successfully shared with the server (Get/Post), unless you won't receive any message.
In developer website
there is a Sender Auth Token in the credentials but how to get it?
Please tell me the steps as it is not clear to me.
as Written in Documentation like below.
Sender Auth Token :An API key that is saved on the 3rd-party application server that gives the application server authorized access to Google services. The API key is included in the header of POST requests that send messages.
you can get API KEY from you from HERE
you can get more informations here
EDITED
GCM SERVER SIDE IMPLEMENTATION IN DRUPAL