[英]Handle push notification when app is Running
我正在使用 Google Notifications (FCM),我注意到以下代码仅在应用程序处于后台时才有效。 如果我在应用程序运行时收到推送通知,则什么也不会发生。
即使应用程序正在运行,也可以使用NotificationManager
显示通知吗?
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
if (message.Data != null)
{
ShowNotification(message.Data.ToDictionary(t => t.Key, t => t.Value));
}
}
void ShowNotification(IDictionary<string, string> data)
{
var manager = (NotificationManager) GetSystemService(NotificationService);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "M_CH_ID");
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
builder.SetSmallIcon(Resource.Mipmap.Icon)
.SetContentTitle(GetString(Resource.String.appName))
.SetContentText("TOOOOOODO") //TODO
.SetContentIntent(pendingIntent)
.SetWhen(10)
.SetAutoCancel(true);
manager.Notify(0, builder.Build());
}
}
该问题仅发生在 Android Oreo 上。 这是正确的代码:
var manager = (NotificationManager) context.getSystemService(Context.NotificationService);
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
{
var notificationChannel = new NotificationChannel("AAA", "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.description = "Channel description"
notificationChannel.enableLights(true)
notificationChannel.vibrationPattern = longArrayOf(0, 1000, 500, 1000)
notificationChannel.enableVibration(true)
manager.createNotificationChannel(notificationChannel)
}
var builder = new NotificationCompat.Builder(context, "AAA");
var intent = new Intent(context, typeof(LoginActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent);
builder.SetSmallIcon(Resource.Mipmap.Icon)
.SetContentTitle(title)
.SetContentText(body)
.SetContentIntent(pendingIntent)
.SetAutoCancel(true);
manager.Notify(0, builder.Build());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.