简体   繁体   English

Xamarin Android 通知未在前台显示

[英]Xamarin Android Notification not Showing in Foreground

I am using Azure Hubs to send notifications.我正在使用 Azure 集线器发送通知。 I am able to receive the notification and it displays on the device when I pull down the notifications window.当我下拉通知 window 时,我能够收到通知并显示在设备上。 However, I do not see the notification display at the top of the screen as expected.但是,我没有按预期在屏幕顶部看到通知显示。 I even "locked" the screen and it didn't display.我什至“锁定”了屏幕,但它没有显示。 I got the notification sound and my logs show I received it.我收到了通知声音,我的日志显示我收到了它。

Screen showing received notification显示收到通知的屏幕

My FirebaseMessageService:我的 FirebaseMessageService:

using System;
using System.Linq;
using WindowsAzure.Messaging;
using Android.App;
using Android.Content;
using Android.Support.V4.App;
using Android.Util;
using Firebase.Messaging;
using IDEQ.AQI.Pages;

namespace IDEQ.AQI.Droid
{
    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class FirebaseService : FirebaseMessagingService
    {
        const string Tag = "FirebaseMsgService";

        public override void OnNewToken(string token)
        {
            // NOTE: save token instance locally, or log if desired

            SendRegistrationToServer(token);
        }

        private void SendRegistrationToServer(string token)
        {
            try
            {
                var hub = new NotificationHub(Constants.NotificationHubName, Constants.ListenConnectionString, this);

                // register device with Azure Notification Hub using the token from FCM
                var registration = hub.Register(token, Constants.SubscriptionTags);

                // subscribe to the SubscriptionTags list with a simple template.
                var pnsHandle = registration.PNSHandle;
                var templateReg = hub.RegisterTemplate(pnsHandle, "defaultTemplate", Constants.FCMTemplateBody, Constants.SubscriptionTags);
            }
            catch (Exception e)
            {
                Log.Error(Constants.DebugTag, $"Error registering device: {e.Message}");
            }
        }

        public override void OnMessageReceived(RemoteMessage message)
        {
            base.OnMessageReceived(message);
            string messageBody;


            Log.Info(Tag, "From: " + message.From);

            if (message.GetNotification() != null)
            {
                Log.Info(Tag, "Notification Message Body: " + message.GetNotification().Body);
                messageBody = message.GetNotification().Body;
            }

            // NOTE: test messages sent via the Azure portal will be received here
            else
            {
                messageBody = message.Data.Values.First();
            }

            // convert the incoming message to a local notification
            SendLocalNotification(messageBody);

            // send the incoming message directly to the MainPage
            SendMessageToMainPage(messageBody);
        }

        private void SendLocalNotification(string body)
        {
            try
            {
                var intent = new Intent(this, typeof(MainActivity));
                intent.AddFlags(ActivityFlags.ClearTop);
                intent.PutExtra("message", body);

                var requestCode = new Random().Next();
                var pendingIntent = PendingIntent.GetActivity(this, requestCode, intent, PendingIntentFlags.OneShot);

                var notificationBuilder = new NotificationCompat.Builder(this, Constants.NotificationChannelId)
                    .SetContentTitle("IDEQ Alert")
                    .SetSmallIcon(Resource.Drawable.ic_launcher)
                    .SetContentText(body)
                    .SetAutoCancel(true)
                    .SetShowWhen(false)
                    .SetContentIntent(pendingIntent);

                var notificationManager = NotificationManagerCompat.From(this);
                notificationManager.Notify(0, notificationBuilder.Build());
            }
            catch (Exception e)
            {
                Log.Error(Tag, e.ToString());
            }
        }

        private void SendMessageToMainPage(string body)
        {
            (Xamarin.Forms.Application.Current.MainPage as MainPage)?.AddMessage(body);
        }
    }
}

//My main activity where I create the channel:

private void CreateNotificationChannel()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        // Notification channels are new in API 26 (and not a part of the
        // support library). There is no need to create a notification
        // channel on older versions of Android.
        return;
    }

    var channel = new NotificationChannel(Constants.NotificationChannelId, Constants.NotificationChannelName, NotificationImportance.Default)
    {
        Description = string.Empty
    };

    var notificationManager = (NotificationManager) GetSystemService(NotificationService);
    notificationManager.CreateNotificationChannel(channel);
}

Notification in your system tray will only be displayed if application is in background or turned off.只有当应用程序处于后台或关闭时,系统托盘中的通知才会显示。 If your application is running, your OnMessageRecieved method will get hit, however Android will not display notification in the system try.如果您的应用程序正在运行,您的 OnMessageRecieved 方法将被命中,但是 Android 不会在系统尝试中显示通知。 This is how life cycle of push notification works in Android.这就是推送通知的生命周期在 Android 中的工作方式。 The only way u can display notification in system tray when application in in foreground is when you force Local Notification like you did in SendLocalNotification method.当应用程序在前台时,您可以在系统托盘中显示通知的唯一方法是像在 SendLocalNotification 方法中那样强制执行本地通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 推送通知未显示在 Android 前台 - Push notification not showing in Android foreground FCM 通知未在 android 的前台显示 - FCM Notification are not showing in foreground in android Android 前台服务通知未显示 - Android foreground service notification not showing 更新 Xamarin.Android 中的前台通知文本 - Update Foreground Notification Text in Xamarin.Android Xamarin Android 从前台通知启动应用程序 - Xamarin Android Launch Application from Foreground Notification 在 android 中显示数字而不是消息的前台通知 - Foreground Notification showing numbers instead of message in android Android前台服务通知未显示在状态栏中 - Android foreground service notification not showing in status bar 当app在前台时,GCM 3.0通知有效负载不显示android通知 - GCM 3.0 notification payload not showing android notification when app is in foreground 如果在后台Android Xamarin中,获取通知消息以在前台显示我的应用程序 - Getting a notification message to show my app in foreground if in background Android Xamarin Firebase通知显示在textview中,而应用程序在前景中,但是当我单击通知消息不显示在textview中时 - firebase notification showing in textview while apps in foreground but while i click on notification message not showing in textview android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM