向端点 AWS SNS 发送通知时看不到消息
Can't see message when sending notification to endpoint AWS 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);
}
}
所以我的主要问题是当应用程序关闭时如何获取数据包的消息部分down/closed并且我从 SNS 发送通知?
有两种类型的 FCM 消息
通知消息
数据消息
参考-
我已经设置了一个 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);
}
}
所以我的主要问题是当应用程序关闭时如何获取数据包的消息部分down/closed并且我从 SNS 发送通知?
有两种类型的 FCM 消息
通知消息
数据消息
参考-