簡體   English   中英

API 27中未顯示本地通知

[英]Local Notifications are not showing in API 27

通知在Api 26及以下版本上可正常使用,但不適用於API 27。

這是我用於創建通知頻道的代碼:

private void CreateNotificationChannel()
{
    try
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            return;
        }

        var notificationManager = (NotificationManager)GetSystemService(NotificationService);
        NotificationChannel mChannel = notifManager.GetNotificationChannel("1");
        if (mChannel == null)
        {
            mChannel = new NotificationChannel("1", "Chat Application", Android.App.NotificationImportance.High);
            mChannel.EnableVibration(true);
            mChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });
            notifManager.CreateNotificationChannel(mChannel);
        }
    }
    catch (Exception exception)
    {
        LoggingManager.Error(exception);
    }

}

我的通知服務是:

var activity = Forms.Context as Activity;

Intent intent = new Intent(activity, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
Random random = new Random();
int pushCount = random.Next(9999 - 1000) + 1000; //for multiplepushnotifications

intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(activity, pushCount, intent, PendingIntentFlags.Immutable);

// Instantiate the builder and set notification elements:
NotificationCompat.Builder builder = new NotificationCompat.Builder(Forms.Context,"1")
                                                           .SetContentTitle(messageTitle)
                                                           .SetDefaults(1|2)
                                                           .SetContentText(Message)
                                                           .SetContentIntent(pendingIntent)
                                                           .SetAutoCancel(true)
                                                           .SetChannelId("1")
                                                           .SetPriority(1);
builder.SetSmallIcon(Resource.Drawable.icon);

// Build the notification:
Notification notification = builder.Build();

// Get the notification manager:
NotificationManager notificationManager = Forms.Context.GetSystemService(Context.NotificationService) as NotificationManager;

// Publish the notification:
notificationManager.Notify(5, notification);

請幫幫我或給我一些建議,我該如何解決此問題。

您可以嘗試使用notificationManager.Notify(new Random()。Next(),notification);嗎?

而不是notificationManager.Notify(5,notification);

我覺得您的頻道創建有問題,您可以在下面檢查我的工作代碼段。

 var mChannel = new NotificationChannel(CHANNEL_ID, "Chat Application", Android.App.NotificationImportance.High)
                  {
                      Description = "Firebase Cloud Messages appear in this channel"
                  };
        mChannel.EnableVibration(true);
        mChannel.SetVibrationPattern(new long[] { 100, 200, 300, 400, 500, 400, 300, 200, 400 });           

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

暫無
暫無

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

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