我正在尝试添加2个操作我的FCM通知
标记重要
Mark Read
我创建了一个广播接收器,但广播接收器的OnReceive
没有被调用。
这是我使用2个操作创建通知的代码:
[Service, IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FCMListenerService : FirebaseMessagingService
{
private string Tag = "FCM_Listener_Service";
public override void OnMessageReceived(RemoteMessage message)
{
var data = message.Data;
var notification = new FCMNotificationDTO(data);
SendNotification(notification);
}
private void SendNotification(FCMNotificationDTO notification)
{
var intent = new Intent(this, typeof(BaseDrawActivity));
intent.PutExtra("NotificationId", notification.NotificationId);
var openActivityIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);
var defaultSoundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
//important intent
Intent importantIntent = new Intent("com.myapp.myapp.NotificationActionHandler");
importantIntent.SetAction(NotificationActionHandler.MARK_IMPORTANT);
PendingIntent intentImportant = PendingIntent.GetBroadcast(this, 0, importantIntent, PendingIntentFlags.UpdateCurrent);
//read intent
Intent readIntent = new Intent("com.myapp.myapp.NotificationActionHandler");
readIntent.SetAction(NotificationActionHandler.MARK_READ);
PendingIntent intentRead = PendingIntent.GetBroadcast(this, 0, readIntent, PendingIntentFlags.UpdateCurrent);
var notificationBuilder =
new NotificationCompat.Builder(this)
// .SetSmallIcon(Resource.Drawable.Icon)
.SetContentTitle(notification.AlertName)
.SetContentText(notification.Reference)
.SetAutoCancel(true)
.SetSound(defaultSoundUri)
.SetContentIntent(openActivityIntent) // When the notification is clicked open the notification activity
.AddAction(Resource.Drawable.ic_alert_circle_outline, GetText(Resource.String.important), intentImportant)
.AddAction(Resource.Drawable.ic_eye_outline, GetText(Resource.String.mark_read), intentRead)
;
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(notification.NotificationId, notificationBuilder.Build());
}
}
我的BroadCast接收器:
[BroadcastReceiver(Enabled = true, Name = "com.myapp.myapp.NotificationActionHandler")]
public class NotificationActionHandler : BroadcastReceiver
{
public static string MARK_READ = "MARK_READ";
public static string MARK_IMPORTANT = "MARK_IMPORTANT";
public override void OnReceive(Context context, Intent intent)
{
var action = intent.Action;
switch (action)
{
case "MARK_IMPORTANT":
Toast.MakeText(context, "Mark important notification", ToastLength.Short).Show();
break;
case "MARK_READ":
Toast.MakeText(context, "Mark read notification", ToastLength.Short).Show();
break;
}
}
}
构建应用程序后,Manifest包含Broad cast接收器详细信息
<receiver android:enabled="true" android:name="com.myapp.myapp.NotificationActionHandler" />