【问题标题】:Can't see message when sending notification to endpoint AWS SNS向终端节点 AWS SNS 发送通知时看不到消息
【发布时间】:2018-11-30 13:58:41
【问题描述】:

我已经设置了一个 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 发送通知时,我如何获取数据包的消息部分?

【问题讨论】:

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


    【解决方案1】:

    有两种类型的 FCM 消息

    通知消息

    数据信息

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

    【讨论】:

    • 谢谢。看来我的解决方案是在同一个数据包中发送通知和数据消息。
    • @PoodleGalluzzo 我认为您只需要发送数据节点,数据节点内部的所有数据都应该在那里。
    • 似乎如果我尝试只发送数据,那么当应用程序处于焦点时我会收到通知。如果我将通知节点添加到数据包中,它将在应用程序打开但在后台显示时显示。但是现在如果应用进程终止,我似乎无法收到通知。
    • @PoodleGalluzzo 如果您想在两种状态下都获得通知,则仅发送数据节点。不要将通知标签放在数据负载中。如果您想在后台收到通知,请确保您的应用程序自动启动应该处于打开状态。
    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多