API 27中未显示本地通知

问题描述 投票:1回答:2

通知适用于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);

请帮帮我或给我一些建议如何解决这个问题。

xamarin.forms push-notification xamarin.android localnotification
2个回答
0
投票

我觉得您的频道创建有问题,下面您可以查看我的工作代码。

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

0
投票

你可以尝试使用notificationManager.Notify(new Random()。Next(),notification);

而不是notificationManager.Notify(5,通知);

© www.soinside.com 2019 - 2024. All rights reserved.