OnMessageReceived 等同于应用程序关闭/在后台发送的通知
OnMessageReceived equivalent for notifications sent while app is closed / in background
我想知道 class 我可以指定如何处理应用程序关闭或在后台时发送的通知。现在我在本地收到通知,然后触发我的 SendLocalNotification
方法以我想要的方式构建它,这就是我希望在应用程序关闭或在后台时发生的通知。我仍然会收到后台通知,但它们不会触发任何构建其外观的代码。
我进行了一些挖掘并阅读了一些有关重写处理远程通知的方法的内容,但找不到很好的示例,甚至找不到要重写的具体方法。
这是我的 OnMessageReceived 在我的 FirebaseService class 中的覆盖(忽略看起来不合适的代码。我已经一直在乱搞东西):
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
string messageBody = string.Empty;
if (message.GetNotification() != null)
{
switch (message.GetNotification().Title)
{
case "Load Matched":
break;
}
messageBody = message.GetNotification().Body;
}
else
{
messageBody = message.Data.Values.First();
}
try
{
MessagingCenter.Send(messageBody, "Update");
}
catch (Exception e)
{
}
SendLocalNotification(messageBody);
}
这是我的 SendLocalNotification 方法。我希望远程通知也能触发此方法,以便它们看起来相同。
void SendLocalNotification(string body)
{
//Unique request code to avoid PendingIntent collision.
var requestCode = new Random().Next();
// accept intent
var acceptIntent = new Intent(this, typeof(MainActivity));
acceptIntent.SetAction("ACCEPT_ACTION");
var pendingIntentAccept = PendingIntent.GetActivity(this, requestCode, acceptIntent, PendingIntentFlags.OneShot);
// decline intent
var declineIntent = new Intent(this, typeof(MainActivity));
declineIntent.SetAction("DECLINE_ACTION");
var pendingIntentDecline = PendingIntent.GetActivity(this, requestCode, declineIntent, PendingIntentFlags.OneShot);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
var notificationBuilder = new NotificationCompat.Builder(this)
.AddAction(0, "Accept", pendingIntentAccept)
.AddAction(0, "Decline", pendingIntentDecline)
.SetContentTitle("Content Title")
.SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
.SetContentText("content text")
.SetContentInfo("content info")
.SetSubText("sub text")
.SetAutoCancel(true)
.SetShowWhen(true)
.SetContentIntent(pendingIntentAccept)
.SetContentIntent(pendingIntentDecline);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
编辑:这是我用来发送通知的代码。我的印象是拥有 data
标签意味着正在发送数据通知,然后 OnMessageReceived
.
会接收到该通知
public void SendNotificationByTag(string tag, string notificationBody = "", string notificationTitle = "")
{
var url = $"{_baseUrl}/messages/?api-version=2015-01";
var client = new RestClient($"{url}");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("ServiceBusNotification-Format", "gcm");
request.AddHeader("ServiceBusNotification-Tags", $"{tag}");
request.AddHeader("Authorization", $"{NotificationHelper.CreateSasToken(url, "DefaultFullSharedAccessSignature", $"{_configuration["DefaultFullSharedAccessSignature"]}")}");
request.AddParameter("application/json", $"{{\"data\":\n\t{{\n\t\t\"gcm.notification.body\":\"{notificationBody}\", \n\t\t\"gcm.notification.title\":\"{notificationTitle}\",\n\t}}\n}}", ParameterType.RequestBody);
client.Execute(request);
}
Firebase 云消息传递支持两个主要message types:
Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
Data messages, which are handled by the client app.
听起来你正在发送通知消息。当您的应用未激活时,这些由 OS/SDK 自动处理并显示在系统托盘中,无需调用您的代码。
如果您总是希望您的 OnMessageReceived
被呼叫,您应该只发送数据消息。
另见:
所以看起来罪魁祸首是在请求中包含 gcm.notification.title
和 gcm.notification.body
。即使它在数据标签内,它看起来仍然作为通知消息传递。当我用常规 title 和 body 标签替换这 2 个键时,我开始在应用程序处于后台时点击我的 OnMessageReceived
。
我想知道 class 我可以指定如何处理应用程序关闭或在后台时发送的通知。现在我在本地收到通知,然后触发我的 SendLocalNotification
方法以我想要的方式构建它,这就是我希望在应用程序关闭或在后台时发生的通知。我仍然会收到后台通知,但它们不会触发任何构建其外观的代码。
我进行了一些挖掘并阅读了一些有关重写处理远程通知的方法的内容,但找不到很好的示例,甚至找不到要重写的具体方法。
这是我的 OnMessageReceived 在我的 FirebaseService class 中的覆盖(忽略看起来不合适的代码。我已经一直在乱搞东西):
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
string messageBody = string.Empty;
if (message.GetNotification() != null)
{
switch (message.GetNotification().Title)
{
case "Load Matched":
break;
}
messageBody = message.GetNotification().Body;
}
else
{
messageBody = message.Data.Values.First();
}
try
{
MessagingCenter.Send(messageBody, "Update");
}
catch (Exception e)
{
}
SendLocalNotification(messageBody);
}
这是我的 SendLocalNotification 方法。我希望远程通知也能触发此方法,以便它们看起来相同。
void SendLocalNotification(string body)
{
//Unique request code to avoid PendingIntent collision.
var requestCode = new Random().Next();
// accept intent
var acceptIntent = new Intent(this, typeof(MainActivity));
acceptIntent.SetAction("ACCEPT_ACTION");
var pendingIntentAccept = PendingIntent.GetActivity(this, requestCode, acceptIntent, PendingIntentFlags.OneShot);
// decline intent
var declineIntent = new Intent(this, typeof(MainActivity));
declineIntent.SetAction("DECLINE_ACTION");
var pendingIntentDecline = PendingIntent.GetActivity(this, requestCode, declineIntent, PendingIntentFlags.OneShot);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
var notificationBuilder = new NotificationCompat.Builder(this)
.AddAction(0, "Accept", pendingIntentAccept)
.AddAction(0, "Decline", pendingIntentDecline)
.SetContentTitle("Content Title")
.SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
.SetContentText("content text")
.SetContentInfo("content info")
.SetSubText("sub text")
.SetAutoCancel(true)
.SetShowWhen(true)
.SetContentIntent(pendingIntentAccept)
.SetContentIntent(pendingIntentDecline);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
编辑:这是我用来发送通知的代码。我的印象是拥有 data
标签意味着正在发送数据通知,然后 OnMessageReceived
.
public void SendNotificationByTag(string tag, string notificationBody = "", string notificationTitle = "")
{
var url = $"{_baseUrl}/messages/?api-version=2015-01";
var client = new RestClient($"{url}");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("ServiceBusNotification-Format", "gcm");
request.AddHeader("ServiceBusNotification-Tags", $"{tag}");
request.AddHeader("Authorization", $"{NotificationHelper.CreateSasToken(url, "DefaultFullSharedAccessSignature", $"{_configuration["DefaultFullSharedAccessSignature"]}")}");
request.AddParameter("application/json", $"{{\"data\":\n\t{{\n\t\t\"gcm.notification.body\":\"{notificationBody}\", \n\t\t\"gcm.notification.title\":\"{notificationTitle}\",\n\t}}\n}}", ParameterType.RequestBody);
client.Execute(request);
}
Firebase 云消息传递支持两个主要message types:
Notification messages, sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
Data messages, which are handled by the client app.
听起来你正在发送通知消息。当您的应用未激活时,这些由 OS/SDK 自动处理并显示在系统托盘中,无需调用您的代码。
如果您总是希望您的 OnMessageReceived
被呼叫,您应该只发送数据消息。
另见:
所以看起来罪魁祸首是在请求中包含 gcm.notification.title
和 gcm.notification.body
。即使它在数据标签内,它看起来仍然作为通知消息传递。当我用常规 title 和 body 标签替换这 2 个键时,我开始在应用程序处于后台时点击我的 OnMessageReceived
。