本文介绍了Appcelerator/Titanium:获取 Android 凭据以推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在 Android 上使用 Titanium 发送推送通知.但是,每当我尝试此操作时,都会出现后端错误,我认为这与无效凭据有关.我尝试按照文档进行操作,但它们似乎已过时.

这是我所做的:

  1. 我前往 Google API 管理器并启用了Google Cloud Messaging".
  2. 在凭据 > OAuthConsentScreen 下,我添加了电子邮件、产品名称和网址
  3. 在凭据 > 创建凭据 > OAuthClientID > Web 应用程序下,我给它一个名称并将一些 URL 放在授权的 JavaScript 来源下,如 localhost.这是否需要是特定的,因为文档中没有提到.
  4. 然后 Google 给了我一个客户端 ID 和一个客户端密钥.这是文档过时的地方.

    • 客户 ID 的格式如下:123456789012-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com
    • 客户端密钥的格式为:XXXXXXXXXXXXXXXXXXXXXXXX
  5. 然后我导航到 Appcelerator 仪表板中的推送通知部分并单击Android Cloud Messaging".请注意,根据文档我应该:

在 GCM API Key 字段中输入服务器密钥,并在GCM 发件人 ID 字段

但是现在双方的领域都不同了.因此,我将以下内容从 Google 复制到 Appcelerator:

  • Google 的 Appcelerator 服务器密钥的客户端密钥
  • Google 的客户端 ID 到 Appcelerator 的服务器 ID

/

  1. 然后我使用建议的CloudPush模块

  2. 将我的Andriod设备订阅到一个频道(效果很好)
  3. 但是,当涉及到发送通知时,从 Appcelerator Dashboard 中,我的 Android 设备旁边会出现失败"一词.

突出显示?"时的完整错误消息图标如下:

异常类型:GCM;错误代码:3103;错误信息:RegistrationId(s) 为 null 或为空;捕获异常:参数不能为空

我在 http://docs.appcelerator 上查找了此错误.com/arrowdb/latest/#!/guide/troubleshooting 并且它说的是:

GCM 客户端提供了一个 null 或空的注册 ID.这个错误是如果您使用 Modules.CloudPush 模块,则不常见.

这没有帮助.

我做错了什么?我想我输入的凭据有误,但不确定如何输入.

我使用的文档如下:

配置推送服务

订阅推送通知

Modules.CloudPush

谢谢!

更新

经过大量建议后,我通过转到创建凭据">API 密钥"创建了另一组凭据.这提供了一个 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 形式的新密钥.然后我使用:

  • 用于 Appcelerator 服务器密钥的 Google API 密钥
  • Google 的项目编号(格式为 123456789012),用于 Appcelerator 的服务器 ID

但是仍然出现完全相同的错误.

更新 2

在阅读另一个 Stack Overflow 答案后(Appcelerator Titanium Android push通知 GCM 失败?),我决定将应用链接到 Firebase.在 Friebase 仪表板上,我转到Cog">项目设置">云消息传递".

它向我展示了发件人 ID(与项目编号完全相同)和不同的服务器密钥.

当我使用这些凭据时,我在发送推送通知时收到了一个略有不同的错误:

异常类型:GCM;错误代码:3006;错误消息:sender_id包含在 registration_id 中与用于使用的 sender_id 不匹配在 GCM 服务器上注册

解决方案

我终于设法让它发挥作用.基本上,这些文档已经过时了.

我设法让它工作的方法是将应用链接到 Firebase.从那里我转到Cog/设置">云消息传递".

这会显示您的发件人 ID"(与您的项目编号相同).对我来说,这是一个 12 位数字.

该页面还显示了一个服务器密钥和一个旧服务器密钥.您使用哪一种似乎并不重要.

如果在推送通知时仍然收到以下错误:

异常类型:GCM;错误代码:3006;错误消息:sender_id包含在 registration_id 中与用于使用的 sender_id 不匹配在 GCM 服务器上注册

只需卸载应用程序并重新构建即可.

旁注:

将您的应用链接到 Firebase 会在您的 Google Console API 凭据页面中自动创建多个 API 密钥,名称类似于____ 密钥(由 Google 服务自动创建)".

旧服务器密钥"似乎与以下名称相匹配:服务器密钥(由 Google 服务自动创建).

请注意,仅自己创建 API 密钥似乎不起作用.它必须是与 Firebase 关联的 API 密钥.

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:

  1. I went to the Google API manager and enabled "Google Cloud Messaging".
  2. Under Credentials > OAuthConsentScreen, I added an email, product name and urls
  3. 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.
  4. 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
  5. 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:

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

/

  1. I then subscribed my Andriod device to a channel (which worked fine) using the suggested CloudPush module

  2. However 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:

I looked this error up on http://docs.appcelerator.com/arrowdb/latest/#!/guide/troubleshooting and all it says is:

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:

解决方案

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:

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.

这篇关于Appcelerator/Titanium:获取 Android 凭据以推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:28