私の割り当ての一部として一定時間が経過した後に受信されたがまだ対話されていない通知を削除したいとします。。つまり、この時間が経過しても通知が通知トレイに残っていると、アプリケーションは自動的に通知を削除します。通知を受信した場合Xamarin Androidが一定時間後にバックグラウンド通知をキャンセルする
void SendNotification(RemoteMessage remotemessage)
{
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
long[] pattern = { 100, 100, 100, 100 };
var notificationBuilder = new Notification.Builder(this)
.SetVibrate(pattern)
.SetSmallIcon(Resource.Drawable.mhu2)
.SetContentTitle(remotemessage.GetNotification().Title)
.SetContentText(remotemessage.GetNotification().Body)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager)GetSystemService(Context.NotificationService);
int id = 0;
notificationManager.Notify(id, notificationBuilder.Build());
Handler handler = new Handler(Looper.MainLooper);
long delayInMilliseconds = 5000;
handler.PostDelayed(new Runnable(() => notificationManager.Cancel(id)), delayInMilliseconds);
}
、それは自動的に5秒(デバッグ目的)の後に削除されます:私は、次のコードを適用するよう
は、フォアグラウンドの通知については、これは、問題ではありませんでした。しかし、皆知っているように、アプリの状態によって通知は同じように処理されません。このコードはフォアグラウンドアプリでは機能しますが、アプリがバックグラウンドであるか、または強制終了されても実行されません。したがって、ユーザーがアプリを開いていないときやバックグラウンドで通知を受け取ったときは、通知は削除されません。
OnDestroy/OnStop/OnPause状態をオーバーライドするときにコードを実行することで、これを調べて部分的な解決策を見出そうとしましたが、アプリが開かれていないときに通知を削除することはできません。
ご意見をいただければ幸いです!