Я много читал об отмене уведомлений / сигналов тревоги, но не могу заставить его работать. Я настроил тест, в котором я создаю будильник и немедленно удаляю его. Но он продолжает стрелять.
Код для создания / удаления сигналов тревоги (вызывается 14 раз при увеличении параметраtificationId):
public void CreateNotification(string title, string text, DateTime datetime, int notificationId)
{
var alarmIntent = new Intent(Forms.Context, typeof(NotificationAlarmReceiver));
alarmIntent.SetAction("com.tender.droid.notification");
alarmIntent.PutExtra("text", text);
alarmIntent.PutExtra("title", title);
alarmIntent.PutExtra("notificationId", notificationId);
alarmIntent.PutExtra("millisecond", datetime.Millisecond);
var pendingIntent = PendingIntent.GetBroadcast(Forms.Context, notificationId, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService);
alarmManager.Set(AlarmType.RtcWakeup, (long)datetime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds, pendingIntent);
DeleteNotification(notificationId);
}
public void DeleteNotification(int notificationId)
{
var notificationManager = Android.App.Application.Context.GetSystemService(Context.NotificationService) as NotificationManager;
notificationManager.Cancel(notificationId);
var alarmIntent = new Intent(Forms.Context, typeof(NotificationAlarmReceiver));
var pendingIntent = PendingIntent.GetBroadcast(Forms.Context, notificationId, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService);
pendingIntent.Cancel();
alarmManager.Cancel(pendingIntent);
}
Код для класса NotificationAlarmReceiver:
[BroadcastReceiver(Enabled = true, Process = ":remote")]
[IntentFilter(new[] { "com.tender.droid.notification" })]
public class NotificationAlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var text = intent.GetStringExtra("text");
var title = intent.GetStringExtra("title");
var notificationId = intent.GetStringExtra("notificationId");
var notIntent = new Intent(context, typeof(SplashScreen));
var contentIntent = PendingIntent.GetActivity(context, 0, notIntent, PendingIntentFlags.UpdateCurrent);
var manager = NotificationManagerCompat.From(context);
var style = new NotificationCompat.BigTextStyle();
style.BigText(text);
//Generate a notification with just short text and small icon, set notification elements:
var builder = new NotificationCompat.Builder(context)
.SetContentIntent(contentIntent)
.SetSmallIcon(Resource.Drawable.android_notification)
.SetContentTitle(title)
.SetContentText(text)
.SetStyle(style)
.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis())
.SetAutoCancel(true)
.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification));
// Instantiate the builder
var notification = builder.Build();
manager.Notify(Convert.ToInt32(notificationId), notification);
}
}
Любая помощь в этом?
Спасибо