问题
I want to be able to send push notifications using Titanium on Android. However I get a back-end error whenever I try this, which I believe is to do with invalid credentials. I tried following the docs but they seem out of date.
Here's what I did:
- I went to the Google API manager and enabled "Google Cloud Messaging".
- Under Credentials > OAuthConsentScreen, I added an email, product name and urls
- Under Credentials > Create credentials > OAuthClientID > Web Application, I gave it a name and put some URL's under Authorized JavaScript origins like localhost. Does this need to be something specific as it's not mentioned on the docs.
Google then gives me a Client ID and a Client Secret. This is where the docs go out of date.
- The Client ID is of the form:
123456789012-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com
- The client secret is of the form:
XXXXXXXXXXXXXXXXXXXXXXXX
- The Client ID is of the form:
I then navigated to the push notifications section in the Appcelerator dashboard and clicked "Android Cloud Messaging". NOTE that according to the docs I should:
Enter the server key in the GCM API Key field and the GCM sender ID in the GCM Sender ID field
However the fields are now different on both sides. So instead I copied the following from Google to Appcelerator:
- Google's Client Secret to Appcelerator's Server key
- Google's Client ID to Appcelerator's Server ID
/
I then subscribed my Andriod device to a channel (which worked fine) using the suggested
CloudPush
moduleHowever when it comes to sending a notification, from the Appcelerator Dashboard, the word "Failure" appears next to my Android device.
The full error message when highlighting the "?" icon is as follows:
Exception Type: GCM; Error Code: 3103; Error Message: RegistrationId(s) is null or empty; Catched Exception: argument cannot be null
I looked this error up on http://docs.appcelerator.com/arrowdb/latest/#!/guide/troubleshooting and all it says is:
The GCM client provided a null or empty registration ID. This error is uncommon if you are using the Modules.CloudPush module.
Which isn't helpful.
What am I doing wrong? I think I am putting in the credentials wrong, but not sure how.
The docs I was using is as follows:
Configuring Push Services
Subscribing to push notifications
Modules.CloudPush
Thanks!
UPDATE
After quite a few suggestions I created another set of credentials by going to "Create Credentials" > "API Key". This gave a new key of the form XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
. I then used:
- Google's API Key for Appcelerator's Server Key
- Google's Project Number (which is of the form
123456789012
), for Appcelerator's Server ID
The exact same error still appears however.
UPDATE 2
After reading another Stack Overflow answer (Appcelerator titanium Android push notification GCM failure?), I decided to link the app to Firebase. On the Friebase dashboard I went to "Cog" > "Project settings" > "Cloud Messaging".
It showed me the sender ID (which is the exactly the same as the project number) and a different server key.
When I used these credentials, I get a slightly different error when sending the push notification:
Exception Type: GCM; Error Code: 3006; Error Message: The sender_id contained in the registration_id does not match the sender_id used to register with the GCM servers
回答1:
I finally managed to get this to work. Basically the docs are heavily out of date.
The way I managed to get it to work is by linking the app to Firebase. From there I went to "Cog/ Settings" > "Cloud Messaging".
This shows your "Sender ID" (which is the same as your project number). For me this was a 12 digit number.
The page also shows a Server key and a Legacy server key. It does not seem to matter which one you use.
If you still receive the following error when pushing a notification:
Exception Type: GCM; Error Code: 3006; Error Message: The sender_id contained in the registration_id does not match the sender_id used to register with the GCM servers
simply uninstall the app and rebuild it.
SIDENOTE:
Linking your app to Firebase creates quite a few API keys automatically in your Google Console API credentials page named like "____ key (auto created by Google Service)".
The "Legacy server key" seems to match the one called: Server key (auto created by Google Service).
Note that just creating an API key yourself does not seem to work. It has to be the API key associated with Firebase.
回答2:
When I did this I created an API Key, not an OAuth client ID. Same screens you had, but choose API Key from the Create credentials dropdown.
Here's a snip from my notes:
Create a Google API Project
The first step is creating a Google API project and enabling its GCM service. You also need to obtain a GCM sender ID and create a Google API server key.
create/edit a project from here : https://console.developers.google.com/project
After it is created, click the hamburger menu in the upper left, and Mouse over API Manager and choose Credentials
On the Credentials tab Click Create Credentials and choose API Key
copy it and paste it into the Arrow dashboard
Click hamburger menu - IAM & Admin -> Settings copy the project number - paste that into Arrow dashboard
来源:https://stackoverflow.com/questions/43757524/appcelerator-titanium-getting-android-credentials-to-push-notifications