Android 8.0(Oreo) 中的 AlarmManager 和通知 [英] AlarmManager and Notifications in Android 8.0(Oreo)
问题描述
在我的应用程序中,我有一个每周四重复的闹钟,我正在使用 AlarmManger,在所有以前版本的 android 中一切正常,但现在使用 android 8.0(oreo),闹钟没有触发,下面是我用来设置闹钟的课程.从我搜索的内容来看,我需要明确设置闹钟,但我不明白怎么办.
In my app i have a alarm that is repeting every thursday, i am using AlarmManger, everything is working fine in all previous versions of android, but now with android 8.0(oreo), the alarm Isn't firing, below is the classes that i use to set my alarm. From what i searched i need to set the alarm explicitly, but I dont understand how.
主要活动:
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:
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;
}
}
}
报警接收器:
[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)
{
}
}
}
推荐答案
当您使用显式意图时,您的警报将正确触发.但在 Oreo/API26 中 .SetDefaults(NotificationDefaults.All
已过时,将导致静默失败,您的通知将不会显示.
Your alarms would be firing correctly as you are using explicit intents. But in Oreo/API26 .SetDefaults(NotificationDefaults.All
is obsolete and will cause a silent failure and your notifications will not be displayed.
因此,您需要设置一个通知渠道,以正确显示您的通知以及所有花里胡哨的内容.
So you need to setup a notification channel to properly display your notification with all the bells and whistles.
如果您更换通知生成器 &通知代码:
If you replace your notification builder & notify code:
var builder =
new Notification.Builder(context)
.SetContentTitle(title)
~~~~
manager.Notify(1331, notification);
通过对 Oreo/API26(+) 的 API 检查,您可以为您的应用建立通知渠道:
With an API check for Oreo/API26(+) you can established an notification channel for your app:
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();
}
现在在设置中,您的应用分配了一个频道,用户可以选择自定义频道:
Now in settings, your app has a channel assigned to it that the user can customize if they so choose:
这篇关于Android 8.0(Oreo) 中的 AlarmManager 和通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!