簡體   English   中英

Xamarin.Forms Android推送通知不會出現

[英]Xamarin.Forms Android Push Notification Doesn't Appear

我正在使用azure hub通知。 我正在使用Xamarin.Forms開發我的應用程序。 對於android我可以在測試時獲得通知命中調試,我可以顯示一個DisplayAlert

但我無法將其顯示為通知。 我搜索並在android oreo之后我應該創建一個通知渠道。

但我不知道該怎么做。 他們說你應該在strings.xml創建一個通知id,但我沒有strings.xml文件。 我不知道怎么做,有人可以幫忙嗎?

internal static readonly string CHANNEL_ID = "cross_channel";
    void CreateNotification(string title, string desc)
    {
        var notificationManager = GetSystemService(Context.NotificationService)
            as NotificationManager;

        var uiIntent = new Intent(this, typeof(MainActivity));
        var pendingIntent = PendingIntent.GetActivity(this, RandomGenerator(), uiIntent, PendingIntentFlags.OneShot);
        var notification = new Notification(Android.Resource.Drawable.ButtonMinus, title)
        {
            Flags = NotificationFlags.AutoCancel
        };
        notification.SetLatestEventInfo(this, title, desc,
            PendingIntent.GetActivity(this, 0, uiIntent, 0));

        if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
        {
            var channel = new NotificationChannel(CHANNEL_ID,
                                      "Cross Notifications",
                                      NotificationImportance.High);


            notificationManager.CreateNotificationChannel(channel);
            string channelId = "Cross Channel";
            var notBuilder = new Notification.Builder(Application.Context, CHANNEL_ID).SetContentTitle(title).SetContentText(desc).SetSmallIcon(Android.Resource.Drawable.StarBigOn).SetAutoCancel(true); 
            notificationManager.Notify(1, notBuilder.Build());

            channel.Description = (desc); 
            notBuilder.SetChannelId(channelId);
            }
            notificationManager.Notify(RandomGenerator(), notBuilder.Build());
    }

MainActivity.cs您可以在onCreate方法中調用此方法:

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(CHANNEL_ID, "FCM Notifications", NotificationImportance.Default)
                      {
                          Description = "Firebase Cloud Messages appear in this channel"
                      };

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

哪里

    internal static readonly string CHANNEL_ID = "my_notification_channel";
    internal static readonly int NOTIFICATION_ID = 100; 

分別是通道ID和通知ID的定義。

在加載XF后的MainActivity的OnCreate中調用:

LoadApplication(new App());
CreateNotificationChannel();

祝好運

在查詢時還原

我在使用Xamarin.Forms正確顯示通知時遇到了一些麻煩。 我假設你已經覆蓋了“OnMessageReceived”事件並且你直接調用了“CreateNotification”? 最后,這是對我有用的代碼:

private void ShowNotification(RemoteMessage msg, IDictionary<string, string> data)
{
    var intent = new Intent();
    intent.AddFlags(ActivityFlags.ClearTop);

    foreach (var key in data.Keys)
        intent.PutExtra(key, data[key]);


    var pendingIntent = PendingIntent.GetActivity(Android.App.Application.Context, 100, intent, PendingIntentFlags.OneShot);

    var notificationBuilder = new NotificationCompat.Builder(Android.App.Application.Context) // Note: Everything I read online said to provide the ChannelID string here, but doing so caused it to not display notifications.
.SetSmallIcon(Resource.Drawable.abc_btn_radio_to_on_mtrl_000) // You can set this to your apps icon
.SetContentTitle(msg.GetNotification().Title)
.SetContentText(msg.GetNotification().Body)
.SetPriority((int)Android.App.NotificationImportance.Max)
.SetDefaults(NotificationCompat.DefaultAll)
.SetContentIntent(pendingIntent) // Even though intent here is empty, you *may* need to include it for the notification to show, I never tried without one.
.SetVisibility((int)NotificationVisibility.Public);


     var notificationManager = NotificationManagerCompat.From(Android.App.Application.Context);
            notificationManager.Notify(100, notificationBuilder.Build());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM