Android 8.0(奥利奥)中的AlarmManager和通知

时间:2017-12-11 10:33:10

标签: xamarin xamarin.forms xamarin.android alarmmanager android-8.0-oreo

在我的应用程序中,我有一个警报,每个星期四重复,我使用AlarmManger,一切都在所有以前的Android版本中正常工作,但现在与Android 8.0(奥利奥),警报器没有发射,下面是我用来设置闹钟的类。 从我搜索到的我需要明确设置警报,但我 不明白怎么做。

MainActivity:

try
        {
            Intent alarmIntent = new Intent(this, typeof(AlarmReceiver));
            PendingIntent pending = PendingIntent.GetBroadcast(this, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
            AlarmManager alarmManager = GetSystemService(AlarmService).JavaCast<AlarmManager>();

            if (Settings.AccessAlarm == 0)
            {
                alarmManager.SetRepeating(AlarmType.RtcWakeup, BootReceiver.FirstReminder(), BootReceiver.reminderInterval, pending);
                PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarmIntent, 0);
                Settings.AccessAlarm = 1;
            }
        }
        catch (Exception e)
        {
            Settings.AccessAlarm = 0;
        }

Bootreceiver.cs:

[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
public class BootReceiver : BroadcastReceiver
{
    //the interval currently every one minute
    //to set it to dayly change the value to 24 * 60 * 60 * 1000
    public static long reminderInterval = AlarmManager.IntervalDay * 7;
    //public static long reminderInterval = 3 * 1000;

    public static long FirstReminder()
    {
        System.Random rnd = new System.Random();

        int minutenumber = rnd.Next(20, 40);

        Java.Util.Calendar calendar = Java.Util.Calendar.Instance;
        calendar.Set(Java.Util.CalendarField.DayOfWeek, Calendar.Thursday);
        calendar.Set(Java.Util.CalendarField.HourOfDay, 10);
        calendar.Set(Java.Util.CalendarField.Minute, minutenumber);
        return calendar.TimeInMillis;

    }

    public override void OnReceive(Context context, Intent intent)
    {
        try
        {
            Console.WriteLine("BootReceiver: OnReceive");
            var alarmIntent = new Intent(context, typeof(AlarmReceiver));
            var pending = PendingIntent.GetBroadcast(context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
            AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
            alarmManager.SetRepeating(AlarmType.RtcWakeup, FirstReminder(), reminderInterval, pending);

            PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, 0);
            Settings.AccessAlarm = 1;
        }
        catch (Exception e)
        {
            Settings.AccessAlarm = 0;
        }

    }
}

AlarmReceiver:

[BroadcastReceiver]
public class AlarmReceiver : BroadcastReceiver
{
    private int z = 0;
    private int i;


    public override void OnReceive(Context context, Intent intent)
    {
        try
        {
                Settings.AlarmNotification = 1;

                if (System.DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
                {
                    Settings.AlarmCount =0;
                }

                var title = "Test";
                var message = "Something";

                Intent backIntent = new Intent(context, typeof(MainActivity));
                backIntent.SetFlags(ActivityFlags.NewTask);



                var resultIntent = new Intent(context, typeof(MainActivity));



                PendingIntent pending = PendingIntent.GetActivities(context, 0,
                    new Intent[] { backIntent, resultIntent },
                    PendingIntentFlags.OneShot);

                var builder =
                    new Notification.Builder(context)
                        .SetContentTitle(title)
                        .SetContentText(message)
                        .SetAutoCancel(true)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetDefaults(NotificationDefaults.All);

                builder.SetContentIntent(pending);
                var notification = builder.Build();
                var manager = NotificationManager.FromContext(context);
                manager.Notify(1331, notification);

        }
        catch (Exception)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:3)

当您使用显式意图时,您的警报将正确触发。但在Oreo / API26 .SetDefaults(NotificationDefaults.All已过时,将导致无声失败,并且您的通知将不会显示。

因此,您需要设置一个通知渠道,以便正确显示您的所有通知和口哨声明。

如果您更换通知构建器&amp;通知代码:

var builder =
    new Notification.Builder(context)
        .SetContentTitle(title)
~~~~
manager.Notify(1331, notification);

通过对Oreo / API26(+)的API检查,您可以为您的应用建立通知渠道:

using (var notificationManager = NotificationManager.FromContext(context))
{
    Notification notification;
    if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.O)
    {
        notification = new Notification.Builder(context)
                                    .SetContentTitle(title)
                                    .SetContentText(message)
                                    .SetAutoCancel(true)
                                    .SetSmallIcon(Resource.Drawable.icon)
                                    .SetDefaults(NotificationDefaults.All)
                                    .SetContentIntent(pending)
                                    .Build();
    }
    else
    {
        // Setup a NotificationChannel, Go crazy and make it public, urgent with lights, vibrations & sound.
        var myUrgentChannel = context.PackageName;
        const string channelName = "SushiHangover Urgent";

        NotificationChannel channel;
        channel = notificationManager.GetNotificationChannel(myUrgentChannel);
        if (channel == null)
        {
            channel = new NotificationChannel(myUrgentChannel, channelName, NotificationImportance.High);
            channel.EnableVibration(true);
            channel.EnableLights(true);
            channel.SetSound(
                RingtoneManager.GetDefaultUri(RingtoneType.Notification),
                new AudioAttributes.Builder().SetUsage(AudioUsageKind.Notification).Build()
            );
            channel.LockscreenVisibility = NotificationVisibility.Public;
            notificationManager.CreateNotificationChannel(channel);
        }
        channel?.Dispose();

        notification = new Notification.Builder(context)
                                    .SetChannelId(myUrgentChannel)
                                    .SetContentTitle(title)
                                    .SetContentText(message)
                                    .SetAutoCancel(true)
                                    .SetSmallIcon(Resource.Drawable.icon)
                                    .SetContentIntent(pending)
                                    .Build();
    }
    notificationManager.Notify(1331, notification);
    notification.Dispose();
}

现在,在设置中,您的应用会为其分配一个频道,用户可以自行选择:

enter image description here