【发布时间】:2020-04-21 00:36:44
【问题描述】:
我想知道我可以在哪个类中指定如何处理在应用程序关闭或在后台发送时发送的通知。现在我在本地收到通知,然后触发我的SendLocalNotification 方法以按照我想要的方式构建它,这也是我希望在应用程序关闭或在后台也发生通知的情况。我仍然会收到后台通知,但它们不会触发任何构成其外观的代码。
我做了一些挖掘并阅读了一些关于覆盖处理远程通知的方法的内容,但找不到一个很好的例子,甚至找不到要覆盖的具体方法。
这是我的 FirebaseService 类中的 OnMessageReceived 覆盖(忽略看起来不合适的代码。我一直在乱搞东西):
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);
}
【问题讨论】:
标签: c# android xamarin.forms push-notification firebase-cloud-messaging