向终端AWS SNS发送通知时看不到消息

时间:2018-06-21 09:28:09

标签: android amazon-web-services xamarin.forms push-notification amazon-sns

我已按照此处的本教程设置了AWS SNS系统,以将通知发送到已订阅主题的所有端点(我已设置了Firebase Cloud Messenger而不是GCM):

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

应用程序运行时,一切正常。我可以通过AWS SNS控制台发送消息,我将在我的终端节点(物理设备和仿真器)上收到通知,但是当应用程序关闭并且尝试从AWS SNS控制台发送相同的JSON数据时,我只会看到标题在通知中。我要发送的JSON数据是:

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

我有一个带有以下代码的PCL Xamarin Forms项目,用于从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);
    }

然后当我想显示通知时:

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);
        } 
    }

所以我的主要问题是,当应用已关闭/关闭并从SNS发送通知时,如何获取数据包的消息部分?

1 个答案:

答案 0 :(得分:1)

FCM消息有两种类型

通知消息

数据消息

参考-How to handle notification when app in background in Firebase