FCM 与 AWS SNS

我正在使用我的机器人项目的 AWS资源,我计划添加推送通知服务我的项目与 AWS SNS。有几个问题困扰我很多。我没有发现任何有关这些问题,除了一个或两个,但不清楚的解释。

1.AWS支持 FCM吗?SNSGCM一起工作。但谷歌建议使用 FCM 而不是 GCM。我没有发现 AWS支持 FCM

即使在发送推送通知之后,AWS还会将消息(或数据)存储到它们的数据库中吗?

3. 在 SNS 应用平台中尝试将 FCM api 密钥放入,为什么会显示出无效的参数?

68815 次浏览

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.

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)

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

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

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

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

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

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

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

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.

Just an additional note to Nathan Dunn's Answer: to add sound use the following JSON message

{
"GCM": "{ \"notification\": { \"text\": \"test message\",\"sound\":\"default\" } }"
}

Now you can go to your firebase console (https://console.firebase.google.com/) select your project, click the gear icon and choose project settings, then click on the cloud messaging tab...

You'll see the legacy Server Key which is the GCM API Key and you'll have the option to generate new Server Keys which are the FCM versions

SNS will accept both versions but their menu option is still categorizing it under GCM

Here is picture for your reference:

enter image description here

Note that you can "accidentally" remove your Server Keys but the Legacy server key is not deletable. Also, if you click the add server key button, you'll get a new server key BELOW the first one, WITH NO WARNING! ...Nice job Google ;)

I tried to use solution with notification payload instead of data, but I did not receive push notifications on the mobile device. I found this tutorial https://youtu.be/iBTFLu30dSg with English subtitles of how to use FCM with AWS SNS step by step and example of how to send push notifications from AWS console and implement it on php with aws php sdk. It helped me a lot.

One more additional note to Nathan Dunn's great answer. How to send data with the notification from SNS to Firebase.

We need to add data to the Json (inside the notification):

{
"default": “any value",
"GCM": "{ \"notification\": { \"body\": \”message body\”, \”title\”: \”message title \”, \"sound\":\"default\" } , \"data\" : {\”key\" : \”value\", \”key2\" : \”value\” } }”
}

In your FirebaseMessagingService implementation (Xamarin example)

public override void OnMessageReceived(RemoteMessage message)
{


try
{


var body = message?.GetNotification()?.Body;
var title = message?.GetNotification()?.Title;
var tag = message?.GetNotification()?.Tag;
var sound = message?.GetNotification()?.Sound;


var data = message?.Data
foreach (string key in data.Keys)
{
// get your data values here
}


}
catch (Exception e)
{
}
}

It took me a while to figure out how to send the notification with the right payload (publish to topic). So I will put it here.

private void PublishToTopic(string topicArn)
{
AmazonSimpleNotificationServiceClient snsClient =
new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.EUWest1);
PublishRequest publishRequest = new PublishRequest();
publishRequest.TopicArn = topicArn;
publishRequest.MessageStructure = "json";
string payload = "\\\"data\\\":{\\\"text\\\":\\\"Test \\\"}";
publishRequest.Message = "{\"default\": \"default\",\"GCM\":\"{" + payload + "}\"}";
PublishResponse publishResult = snsClient.Publish(publishRequest);
}

To answer the questions:

  1. AWS SNS does support FCM.
  2. No AWS does not store messages after sending push notifications.

For a detailed tutorial on setting up FCM with SNS please read this article.

Amazon does support FCM as all previous code has been migrated from GCM to FCM. Below article explains in detail.

Article Published by Amazon