1

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?

Poodle Galluzzo
  • 79
  • 1
  • 10

1 Answers1

1

There are two types of FCM messages

Notification message

Data message

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

Dev
  • 181
  • 12
  • Thanks for that. It looks like my solution was to send both a notification and data message in the same packet. – Poodle Galluzzo Jun 23 '18 at 03:43
  • @PoodleGalluzzo I think you need to send only data node, Inside data node all the data should be there. – Dev Jun 24 '18 at 08:43
  • It seems that If I try to only send the data then when the app is in focus I will get the notification. If I add the notification node to the data packet it will show when the app is open but in the background. But now I cant seem to receive the notification if the app process is terminated. – Poodle Galluzzo Jun 29 '18 at 10:07
  • @PoodleGalluzzo send only data node if you want to get the notification in both states. Don't put notification tag inside data payload. Make sure your app autostart should be on if you want to get the notification in the background. – Dev Jun 29 '18 at 12:39