From b55b76c8c30887c6c60a4257e6e1e88121ce4ccc Mon Sep 17 00:00:00 2001 From: "M. S. Parmar" <45614562+m-s-parmar@users.noreply.github.com> Date: Sat, 14 Dec 2024 13:47:09 +0530 Subject: [PATCH 1/2] Added an example for sending push notification to FCM endpoint using AWS SNS. --- gov2/sns/publish_to_fcm_endpoint.go | 87 +++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 gov2/sns/publish_to_fcm_endpoint.go diff --git a/gov2/sns/publish_to_fcm_endpoint.go b/gov2/sns/publish_to_fcm_endpoint.go new file mode 100644 index 00000000000..dccb329b8a6 --- /dev/null +++ b/gov2/sns/publish_to_fcm_endpoint.go @@ -0,0 +1,87 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/sns" +) + +// This code example shows you how to send a push notification to a Firebase Cloud Messaging (FCM) endpoint +// using AWS SNS SDK. + +func main() { + ctx := context.Background() + + sdkConfig, err := config.LoadDefaultConfig(ctx) + if err != nil { + fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") + fmt.Println(err) + return + } + + snsClient := sns.NewFromConfig(sdkConfig) + + notificationData := map[string]interface{}{ + "notification": map[string]string{ + "title": "Notification Title", + "body": "Sample Body", + }, + "data": map[string]string{ + "sampleData1": "Sample Data", + "sampleData2": "Sample Data", + }, + } + + // We need to Marshal this data into JSON format, then put it as a + // value to GCM key inside another map, and then marshal that + // map to get the required format to be sent to SNS. + + notificationDataJSON, err := json.Marshal(notificationData) + + if err != nil { + fmt.Println(err) + return + } + + message := map[string]interface{}{ + "GCM": string(notificationDataJSON), + } + + messageJSON, err := json.Marshal(message) + + if err != nil { + fmt.Println(err) + return + } + + endpointArn := "user-endpoint-arn" + + _, err = snsClient.Publish(ctx, &sns.PublishInput{ + TargetArn: &endpointArn, + MessageStructure: aws.String("json"), + Message: aws.String(string(messageJSON)), + }) + + if err != nil { + fmt.Println(err) + return + } + + // or if you want to pass the message data directly as a string to avoid the double + // JSON marshalling, you can do that by passing it in the following format: + // + // Message: aws.String( + // "{\"GCM\":\"{\\\"notification\\\":{" + + // "\\\"body\\\":\\\"Body\\\"," + + // "\\\"title\\\":\\\"Title\\\"" + + // "}," + + // "\\\"data\\\":{" + + // "\\\"sampleData1\\\":\\\"Sample Data\\\"," + + // "\\\"sampleData2\\\":\\\"Sample Data\\\"" + + // "}}\"}" + // ), +} From ee0fd02ad5458096c5ec7447992f897ad9dda363 Mon Sep 17 00:00:00 2001 From: "M. S. Parmar" <45614562+m-s-parmar@users.noreply.github.com> Date: Wed, 18 Dec 2024 18:03:19 +0530 Subject: [PATCH 2/2] Updated publish_to_fcm_endpoint.go: Added default key in the message. --- gov2/sns/publish_to_fcm_endpoint.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gov2/sns/publish_to_fcm_endpoint.go b/gov2/sns/publish_to_fcm_endpoint.go index dccb329b8a6..c075ed913b9 100644 --- a/gov2/sns/publish_to_fcm_endpoint.go +++ b/gov2/sns/publish_to_fcm_endpoint.go @@ -48,6 +48,7 @@ func main() { } message := map[string]interface{}{ + "default": "Default Value", "GCM": string(notificationDataJSON), } @@ -75,7 +76,8 @@ func main() { // JSON marshalling, you can do that by passing it in the following format: // // Message: aws.String( - // "{\"GCM\":\"{\\\"notification\\\":{" + + // "{\"default\":\"Default Value\","+ + // "\"GCM\":\"{\\\"notification\\\":{" + // "\\\"body\\\":\\\"Body\\\"," + // "\\\"title\\\":\\\"Title\\\"" + // "}," +