简体   繁体   English

Appcelerator / Titanium:获取Android凭据以推送通知

[英]Appcelerator/ Titanium: Getting Android credentials to push notifications

I want to be able to send push notifications using Titanium on Android. 我希望能够在Android上使用Titanium发送推送通知。 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: 这是我所做的:

  1. I went to the Google API manager and enabled "Google Cloud Messaging". 我去了Google API管理器并启用了“ Google Cloud Messaging”。
  2. Under Credentials > OAuthConsentScreen, I added an email, product name and urls 在“凭据”>“ OAuthConsentScreen”下,我添加了电子邮件,产品名称和网址
  3. Under Credentials > Create credentials > OAuthClientID > Web Application, I gave it a name and put some URL's under Authorized JavaScript origins like localhost. 在“凭证”>“创建凭证”>“ OAuthClientID”>“ Web应用程序”下,给它命名,并将一些URL放在“授权JavaScript”来源(如localhost)下。 Does this need to be something specific as it's not mentioned on the docs. 这是否需要具体说明,因为文档中未提及。
  4. Google then gives me a Client ID and a Client Secret. Google然后给我一个客户ID和一个客户机密。 This is where the docs go out of date . 这是文档过时的地方

    • The Client ID is of the form: 123456789012-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com 客户端ID的格式为: 123456789012-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com
    • The client secret is of the form: XXXXXXXXXXXXXXXXXXXXXXXX 客户机密的格式为: XXXXXXXXXXXXXXXXXXXXXXXX
  5. I then navigated to the push notifications section in the Appcelerator dashboard and clicked "Android Cloud Messaging". 然后,我导航到Appcelerator仪表板中的推送通知部分,然后单击“ 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 在“ GCM API密钥”字段中输入服务器密钥,在“ GCM发送者ID”字段中输入GCM发送者ID

However the fields are now different on both sides. 但是,现在双方的领域都不同。 So instead I copied the following from Google to Appcelerator: 因此,我将以下内容从Google复制到了Appcelerator:

  • Google's Client Secret to Appcelerator's Server key Google对Appcelerator服务器密钥的客户端机密
  • Google's Client ID to Appcelerator's Server ID Google的客户端ID到Appcelerator的服务器ID

/ /

  1. I then subscribed my Andriod device to a channel (which worked fine) using the suggested CloudPush module 然后,我使用建议的CloudPush模块将我的Andriod设备订阅了一个频道(工作正常)

  2. However when it comes to sending a notification, from the Appcelerator Dashboard, the word "Failure" appears next to my Android device. 但是,当要从Appcelerator仪表板发送通知时,Android设备旁边会出现“失败”字样。

推送通知时出错

The full error message when highlighting the "?" 突出显示“?”时的完整错误消息 icon is as follows: 图标如下:

Exception Type: GCM; 异常类型:GCM; Error Code: 3103; 错误代码:3103; Error Message: RegistrationId(s) is null or empty; 错误消息:RegistrationId为空或为空; 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: 我在http://docs.appcelerator.com/arrowdb/latest/#!/guide/疑难解答中查找了此错误,它的意思是:

The GCM client provided a null or empty registration ID. GCM客户端提供了一个空或空的注册ID。 This error is uncommon if you are using the Modules.CloudPush module. 如果使用的是Modules.CloudPush模块,此错误很少见。

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 模块.CloudPush

Thanks! 谢谢!

UPDATE UPDATE

After quite a few suggestions I created another set of credentials by going to "Create Credentials" > "API Key". 经过大量建议后,我通过转到“创建凭据”>“ API密钥”创建了另一组凭据。 This gave a new key of the form XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX . 这给出了一个新的密钥,格式为XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX I then used: 然后,我使用了:

  • Google's API Key for Appcelerator's Server Key Google的Appcelerator服务器密钥的API密钥
  • Google's Project Number (which is of the form 123456789012 ), for Appcelerator's Server ID Google的项目编号(格式为123456789012 ),用于Appcelerator的服务器ID

The exact same error still appears however. 但是仍然出现完全相同的错误。

UPDATE 2 更新2

After reading another Stack Overflow answer ( Appcelerator titanium Android push notification GCM failure? ), I decided to link the app to Firebase. 在阅读了另一个Stack Overflow答案( Appcelerator Titanium Android推送通知GCM失败? )之后,我决定将应用程序链接到Firebase。 On the Friebase dashboard I went to "Cog" > "Project settings" > "Cloud Messaging". 在Friebase仪表板上,我转到“ Cog”>“项目设置”>“ Cloud Messaging”。

It showed me the sender ID (which is the exactly the same as the project number) and a different server key. 它显示了发件人ID(与项目编号完全相同)和不同的服务器密钥。

When I used these credentials, I get a slightly different error when sending the push notification: 使用这些凭据时,发送推送通知时会出现一个略有不同的错误:

Exception Type: GCM; 异常类型:GCM; Error Code: 3006; 错误代码:3006; Error Message: The sender_id contained in the registration_id does not match the sender_id used to register with the GCM servers 错误消息:registration_id中包含的sender_id与用于向GCM服务器注册的sender_id不匹配

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 . 我设法使其运行的方法是将应用程序链接到Firebase From there I went to "Cog/ Settings" > "Cloud Messaging ". 从那里我去了“齿轮/设置”>“云消息传递 ”。

This shows your "Sender ID" (which is the same as your project number). 这将显示您的“发件人ID” (与您的项目编号相同)。 For me this was a 12 digit number. 对我来说,这是一个12位数字。

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; 异常类型:GCM; Error Code: 3006; 错误代码:3006; Error Message: The sender_id contained in the registration_id does not match the sender_id used to register with the GCM servers 错误消息:registration_id中包含的sender_id与用于向GCM服务器注册的sender_id不匹配

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) ". 将您的应用程序链接到Firebase会在您的Google Console API凭据页面中自动创建很多API密钥,例如“ ____ key(由Google Service自动创建) ”。

The "Legacy server key" seems to match the one called: Server key (auto created by Google Service) . “旧版服务器密钥”似乎与以下名称相匹配: 服务器密钥(由Google Service自动创建)

Note that just creating an API key yourself does not seem to work. 请注意,仅自己创建一个API密钥似乎无效。 It has to be the API key associated with Firebase. 它必须是与Firebase关联的API密钥。

When I did this I created an API Key, not an OAuth client ID. 完成此操作后,我创建了一个API密钥,而不是OAuth客户端ID。 Same screens you had, but choose API Key from the Create credentials dropdown. 与您拥有的屏幕相同,但是从“创建凭据”下拉列表中选择“ API密钥”。

Here's a snip from my notes: 这是我的笔记中的一个片段:

Create a Google API Project 创建一个Google API项目

The first step is creating a Google API project and enabling its GCM service. 第一步是创建一个Google API项目并启用其GCM服务。 You also need to obtain a GCM sender ID and create a Google API server key. 您还需要获取GCM发送者ID并创建Google API服务器密钥。

create/edit a project from here : https://console.developers.google.com/project 从此处创建/编辑项目: 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 创建完毕后,点击左上方的汉堡菜单,然后将鼠标悬停在API管理器上,然后选择凭据

On the Credentials tab Click Create Credentials and choose API Key 在“凭据”选项卡上,单击“创建凭据”,然后选择“ API密钥”

copy it and paste it into the Arrow dashboard 复制并将其粘贴到Arrow仪表板中

Click hamburger menu - IAM & Admin -> Settings copy the project number - paste that into Arrow dashboard 单击汉堡菜单-IAM和管理->设置复制项目编号-将其粘贴到Arrow仪表板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM