取消Xamarin.forms(Android)中的警报

时间:2018-06-28 09:34:05

标签: android xamarin xamarin.forms android-notifications

我已经阅读了很多有关取消通知/警报的信息,但无法正常工作。我设置了一个测试,在其中创建警报并立即将其删除。但是它一直在射击。

用于创建/删除警报的代码(随着NotificationId的增加,调用了14次):

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);
    }
}

对此有任何帮助吗?

谢谢

1 个答案:

答案 0 :(得分:0)

使用此代码: alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+ 60 * 1000,alarmIntent); 代替 alarmManager.Set(AlarmType.RtcWakeup,(long)datetime.ToUniversalTime()。Subtract(new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc))。TotalMilliseconds,endingIntent);