Poodle Galluzzo
Poodle Galluzzo

Reputation: 79

Can't see message when sending notification to endpoint AWS SNS

I have set up a AWS SNS system to send notifications to all endpoints that have subscribed to a topic following this tutorial here (I have set up a Firebase Cloud Messenger not GCM):

https://docs.aws.amazon.com/mobile/sdkforxamarin/developerguide/sns.html

Everything works as expected when the app is running. I can send a message through the AWS SNS console and I will receive the notification on my endpoint (physical device and emulator) however when the app is closed and I try to send the same JSON data from the AWS SNS console I only see the Title in the notification. The JSON data that I am sending is:

{ "default": "Testing", "sqs": "Testing", "GCM": "{ \"notification\": { \"message\": \"Testing\" } }" }

I have a PCL Xamarin Forms project with the following code to handle the notification when the notification is received from Firebase:

    private void HandleMessage(Intent intent)
    {
        string message = string.Empty;
        Bundle extras = intent.Extras;

        if (!string.IsNullOrEmpty(extras.GetString("message")))
        {
            message = extras.GetString("message");
        }
        else
        {
            message = extras.GetString("default");
        }

        AndroidUtils.ShowNotification(this, "Test", message);
    }

Then when I want to display the notification:

public static void ShowNotification(Context context, string contentTitle,
            string contentText)
    {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, Constants.CHANNEL_ID);
        builder.SetAutoCancel(true);
        builder.SetSmallIcon(Resource.Mipmap.icon_round);
        builder.SetContentText(contentText);
        builder.SetContentTitle(contentTitle);
        builder.SetPriority(NotificationCompat.PriorityDefault);
        Notification notification = builder.Build();

        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            NotificationChannel notificationChannel = new NotificationChannel(Constants.CHANNEL_ID, Constants.ANDROID_CHANNEL_NAME, NotificationImportance.Default);
            NotificationManager notificationManager = context.GetSystemService(Context.NotificationService) as NotificationManager;
            notificationManager.CreateNotificationChannel(notificationChannel);

            const int notificationID = 0;
            notificationManager.Notify(notificationID, notification);
        }
        else
        {
            NotificationManager notificationManager = context.GetSystemService(Context.NotificationService) as NotificationManager;
            const int notificationID = 0;

            notificationManager.Notify(notificationID, notification);
        } 
    }

So my main question here is how do I get the message part of the data packet when the app has been shut down/closed and I send a notification from SNS?

Upvotes: 1

Views: 1218

Answers (1)

Dev
Dev

Reputation: 191

There are two types of FCM messages

Notification message

Data message

Reference -How to handle notification when app in background in Firebase

Upvotes: 1

Related Questions