Xamarin 计划通知在android 9.0中不起作用

Xamarin 计划通知在android 9.0中不起作用,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,每当我在本地通知代码中使用Alarm Manager时,它在Android 8.0及以下版本中都可以正常工作。但是 它不适用于Android 9.0版 报警管理器代码:: var intent = CreateIntent(id); var localNotification = new LocalNotification(); localNotification.Title = title; localNotification.Body

每当我在本地通知代码中使用Alarm Manager时,它在Android 8.0及以下版本中都可以正常工作。但是 它不适用于Android 9.0版

报警管理器代码::

    var intent = CreateIntent(id);
        var localNotification = new LocalNotification();
        localNotification.Title = title;
        localNotification.Body = body;
        localNotification.Id = id;
        localNotification.NotifyTime = notifyTime;

        if (_notificationIconId != 0)
        {
            localNotification.IconId = _notificationIconId;
        }
        else
        {
            localNotification.IconId = Resource.Drawable.notification_bg;
        }

        var serializedNotification = SerializeNotification(localNotification);
        intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification);

        Random generator = new Random();
        _randomNumber = generator.Next(100000, 999999).ToString("D6");

        var pendingIntent = PendingIntent.GetBroadcast(Application.Context, Convert.ToInt32(_randomNumber), intent, PendingIntentFlags.Immutable);
        var alarmManager = GetAlarmManager();
        alarmManager.Set(AlarmType.RtcWakeup, totalMilliSeconds, pendingIntent);
PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, pendingIntentId, intent, PendingIntentFlags.Immutable);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(Application.Context, channelId)

            .SetVibrate(new long[] { 0, 100 })
            .SetPriority(1)
            .SetContentIntent(pendingIntent)
            .SetContentTitle(title)               
            .SetContentText(body)
            .SetWhen(totalMilliSeconds)               
            .SetSmallIcon(Resource.Drawable.notification_bg)
            .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);

Notification notification = builder.Build();
        manager.Notify(channelId, messageId, notification);
而且,当我使用ChannelID与Notification Manager实现相同的代码时。所以,通知就出现在 Android 9.0及以下版本。 但是,如果我想设置一个特定的传递时间来显示通知,那么它在这里不起作用。它能立即交付

通知管理器代码::

    var intent = CreateIntent(id);
        var localNotification = new LocalNotification();
        localNotification.Title = title;
        localNotification.Body = body;
        localNotification.Id = id;
        localNotification.NotifyTime = notifyTime;

        if (_notificationIconId != 0)
        {
            localNotification.IconId = _notificationIconId;
        }
        else
        {
            localNotification.IconId = Resource.Drawable.notification_bg;
        }

        var serializedNotification = SerializeNotification(localNotification);
        intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification);

        Random generator = new Random();
        _randomNumber = generator.Next(100000, 999999).ToString("D6");

        var pendingIntent = PendingIntent.GetBroadcast(Application.Context, Convert.ToInt32(_randomNumber), intent, PendingIntentFlags.Immutable);
        var alarmManager = GetAlarmManager();
        alarmManager.Set(AlarmType.RtcWakeup, totalMilliSeconds, pendingIntent);
PendingIntent pendingIntent = PendingIntent.GetActivity(Application.Context, pendingIntentId, intent, PendingIntentFlags.Immutable);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(Application.Context, channelId)

            .SetVibrate(new long[] { 0, 100 })
            .SetPriority(1)
            .SetContentIntent(pendingIntent)
            .SetContentTitle(title)               
            .SetContentText(body)
            .SetWhen(totalMilliSeconds)               
            .SetSmallIcon(Resource.Drawable.notification_bg)
            .SetDefaults((int)NotificationDefaults.Sound | (int)NotificationDefaults.Vibrate);

Notification notification = builder.Build();
        manager.Notify(channelId, messageId, notification);

那么,有没有一种方法可以让我在Android 9.0及以下版本中实现这一点呢?

根据Android文档,自Android 6.0(API级别23)以来,计划报警要求一直没有改变。您似乎没有以正确的方式实现报警,正如您所见,有很多不同之处,您应该使用WorkManager进行计划:根据Android文档,自Android 6.0(API级别23)以来,计划报警要求没有改变。您似乎没有以正确的方式实施报警,正如您所看到的,有很多不同之处,您应该使用WorkManager进行计划: