简体   繁体   中英

Handle push notification when app is Running

I'm using the Google Notifications (FCM) and I noticed that the following code works only if the app is in background . If I receive a push notification when the app is running, nothing happen.

It is possible to show a Notification with NotificationManager even if the app is running?

[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());
    }
}

The problem happens only on Android Oreo. This is the right code:

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM