Xamarin Android 8.0(Oreo)中的AlarmManager和通知
在我的应用程序中,我有一个每周四重复的警报,我使用AlarmManger,在所有以前版本的android中,一切都正常工作,但现在使用android 8.0(oreo),警报不会触发,下面是我用来设置警报的类。 根据我搜索的内容,我需要明确设置警报,但我 我不明白怎么做 主要活动:Xamarin Android 8.0(Oreo)中的AlarmManager和通知,xamarin,xamarin.forms,xamarin.android,alarmmanager,android-8.0-oreo,Xamarin,Xamarin.forms,Xamarin.android,Alarmmanager,Android 8.0 Oreo,在我的应用程序中,我有一个每周四重复的警报,我使用AlarmManger,在所有以前版本的android中,一切都正常工作,但现在使用android 8.0(oreo),警报不会触发,下面是我用来设置警报的类。 根据我搜索的内容,我需要明确设置警报,但我 我不明白怎么做 主要活动: try { Intent alarmIntent = new Intent(this, typeof(AlarmReceiver)); PendingI
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;
}
报警接收器:
[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
中,所有的都已过时,将导致无声故障,并且不会显示您的通知
因此,您需要设置一个通知通道,以正确显示您的通知和所有的铃声和口哨
如果替换通知生成器和通知代码:
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();
}
使用(var notificationManager=notificationManager.FromContext(context))
{
通知;
if(Android.OS.Build.VERSION.SdkInt
现在,在“设置”中,您的应用程序分配了一个频道,用户可以自定义该频道(如果他们选择):
您好,非常感谢您的帮助,现在正在工作,但是通知上的图标没有显示,在android 8.0中正常吗?另一件事,可以知道用户是否在通知中单击了吗?@Phill图标(SetSmallIcon
)应该显示,在我的Oreo上显示。您正在设置通知的意图(backIntent
)并启动main活动的新实例(通过NewTask
),不确定这是否是你想要的。接受这个答案,因为它有效,如果需要,发布另一个关于用户点击通知的问题。我会这样做,你在可绘图文件夹中的图标大小是多少,我的是48x48。@Phill我使用所有大小的ldpi
到xxxhdpi
,48x48将是xhdpi
,因此我也会使用大图标:我的代码和你几乎一样,但从Android 8+开始,当设备重新启动并将引导完成发送到应用程序时,应用程序会崩溃,如下所示(我删除了一些文本以缩短内容):RuntimeException:无法启动接收器PushHandlerBroadcastReceiver:IllegalStateException:不允许启动服务意图{act=android.Intent.action.BOOT_COMPLETED cmp=..GcmService(有额外功能)}:应用程序位于后台uid中…
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();
}