带有 AWS SNS 的 FCM [英] FCM with AWS SNS

本文介绍了带有 AWS SNS 的 FCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 AWS 资源用于我的 android 项目,我计划使用 AWS SNS 为我的项目添加推送通知服务.有几个问题困扰着我.我没有发现任何关于这些的问题,只有一两个但解释不清楚.

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.AWS 是否支持FCM?SNSGCM 一起使用.但是 Google 建议使用 FCM 而不是 GCM.我没有找到支持 FCMAWS.

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.AWS 是否在发送推送通知后仍将消息(或数据)存储到他们的数据库中?

2.Do AWS store messages (or data) into their databases even after sending push notifications?

3.我试过把FCM api key放到SNS应用平台,为什么显示参数无效?

3.I tried putting FCM api key in SNS application platform, it is showing invalid parameters why?

推荐答案

FCM 向后兼容 GCM.在 AWS 上设置 FCM 的步骤与 GCM 设置程序 和(至少目前)FCM 在服务器端配置方面与 GCM 和 SNS 透明地工作.

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.

但是,如果您向 Android 设备发送 data 有效负载,除非您实施 客户端服务 扩展了 FirebaseMessagingService.AWS 控制台中的默认 JSON 消息生成器发送 data 消息,除非实现上述服务,否则您的应用程序将忽略这些消息.为了在初始测试中解决这个问题,您可以提供一个自定义的 notification 负载,该负载将由您的设备接收(只要您的应用不在前台)

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)

Google 提供了 GCM-FCM 迁移说明但是,您需要进行的更改主要在应用方面.

There are GCM-FCM migration instructions provided by Google however the changes you need to make are predominantly on the App side.

使用 SNS 在您的应用上测试 GCM/FCM 需要遵循的步骤是:

The steps you need to follow to test GCM/FCM on your app with SNS are:

  1. 在 SNS 中创建平台应用,选择 Google Cloud Messaging (GCM) 作为推送通知平台,并提供您的 API 密钥 字段中的noreferrer">服务器 API 密钥.
  2. 选择平台应用程序并点击创建平台端点按钮.
  3. 提供您的应用生成的 InstanceID(设备令牌).您必须扩展 FirebaseInstanceIDService 并覆盖 onTokenRefresh 方法才能在您的 Android 应用程序中看到它.一旦您完成此操作,卸载并重新安装您的应用,并且您的令牌应在首次启动时打印到 Android Studio 中的调试控制台.
  4. 点击添加端点按钮.
  5. 点击您的平台应用程序的 ARN 链接.
  6. 为您的设备选择新创建的端点,然后点击发布到端点按钮.
  7. 选择 JSON 消息格式,然后点击 JSON 消息生成器按钮.
  8. 输入测试消息并点击生成 JSON 按钮
  9. 现在是陷阱部分".
  1. 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.
  2. Select the Platform Application and click the Create platform endpoint button.
  3. 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.
  4. Click the Add endpoint button.
  5. Click on the ARN link for your platform application.
  6. Select the newly created Endpoint for your device and click the Publish to endpoint button.
  7. Select the JSON Message Format, and click the JSON message generator button.
  8. Enter a test message and click the Generate JSON button
  9. Now comes the "gotcha part".

由 SNS 生成的消息将采用以下形式:

The message that is generated by SNS will be of the form:

{
"GCM": "{ "data": { "message": "test message" } }"
}

正如我们之前提到的,如果没有实现接收它们的服务,data 有效载荷将被忽略.我们希望在不编写太多代码的情况下进行测试,因此我们应该发送一个 notification 负载.为此,只需将 JSON 消息更改为读取:

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" } }"
}

(有关 FCM 消息的 JSON 格式的更多信息,请参阅 FCM文档.)

(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.

您当然可以通过 Amazon SNS API 以编程方式完成所有这些工作,但是所有示例似乎都使用 data 有效负载,因此您需要牢记这一点并生成适合您使用的有效负载案例.

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.

这篇关于带有 AWS SNS 的 FCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆