I've read a lot about canceling notifications/alarms but I can't get it working. I set up a test where I create a alarm and immediately delete it. But it keeps firing.
Code to create / delete alarms (called 14 times with increasing notificationId):
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);
}
Code for class 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);
}
}
Any help on this?
Thanks
Use this code : alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, alarmIntent); instead of alarmManager.Set(AlarmType.RtcWakeup, (long)datetime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds, pendingIntent);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.