1

내 앱에는 매주 목요일마다 리트 윗하고있는 Alarm이 있고 AlarmManger를 사용하고 있으며 모든 이전 버전의 Android에서 잘 작동하지만 Android 8.0 (oreo) 알람이 작동하지 않습니다. 아래에 알람 설정에 사용하는 클래스가 있습니다. 내가 검색 한 내용에서 알람을 명시 적으로 설정해야하지만, 방법을 이해하지 못합니다.Android 8.0 (Oreo)의 AlarmManager 및 알림

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

귀하의 알람이 제대로 발사 될 것이다. 그러나 Oreo/API26에서 .SetDefaults(NotificationDefaults.All은 더 이상 사용되지 않으며 자동 실패가 발생하고 알림은 표시되지 않습니다.

모든 종소리와 휘슬로 알림을 올바르게 표시하려면 알림 채널을 설정해야합니다. 이제

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 builder = 
    new Notification.Builder(context) 
     .SetContentTitle(title) 
~~~~ 
manager.Notify(1331, notification); 

오레오/API26 (+)에 대한 API 검사와 앱에 대한 알림 채널을 설립 할 수는 알림 빌더를 교체 할 경우

& 코드가 통지 설정에서 앱에 할당 된 채널이있어 사용자가 선택할 수있는 경우 사용자가 맞춤 설정할 수 있습니다.

enter image description here

+0

안녕하십니까, 도움을 주셔서 감사합니다. 현재 작동하지만 알림의 아이콘이 보이지 않습니다. android 8.0에서 정상입니까? 사용자가 알림을 클릭했는지 알 수 있습니까? – Phill

+1

@Phill 아이콘 ('SetSmallIcon')이 보여 져야합니다, Oreo에서 않습니다. 알림 ('backIntent')의'Intent'를 설정하고 ('NewTask'를 통해)'MainActivity'의 새로운 인스턴스를 시작합니다, 당신이 원하는 것인지 확실하지 않습니다. 문제가 해결 된 후이 대답을 수락하고 필요한 경우 사용자가 알림을 클릭하는 것과 관련된 다른 질문을 게시하십시오. – SushiHangover

+0

제가 할 수있는 것은, 드로어 블 폴더의 아이콘 크기가 뭐죠, 제 크기는 48x48입니다. – Phill