AlarmManager
を使用している通知が特定の日付に設定されているとします。表示される前にボタンの押下でその通知を削除したい場合はどうすればいいですか?私が間違っていない場合、NotificationManager.cancel(id)
メソッドは、現在表示されている通知をキャンセルします。表示される前に削除したい場合はどうすればよいですか? arraylistのアイテムを削除する方法、データベース内の行を削除する方法など。Android - 通知が表示される前に削除またはキャンセルする
例私はid,name
とPerson
オブジェクトを持っており、毎回、私はデータベースにPerson
を追加し、Notification
は、指定した日付に設定されます。ここで
public void setNotification(Calendar calendar,Person person){
AlarmManager alertManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_NOTIFICATION");
intent.addCategory("android.intent.category.DEFAULT");
intent.putExtra("myTag",person);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, person.getId(), intent, PendingIntent.FLAG_ONE_SHOT);
alertManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
マイ警報受信機クラス(はい、私はDAOデザインパターンを実装しています私が持っています後者はシングルトンクラスであることとPersonDaoとPersonDaoImpl):アラームレシーバに設定
public class AlarmReceiver extends BroadcastReceiver {
final static String GROUP_KEY = "GROUP_KEY";
@Override
public void onReceive(Context context, Intent intent) {
//retrieve info:
PersonDAO personDao = PersonDAOImpl.getInstance(context);
Person person = (Person)intent.getSerializableExtra("myTag");
String name = person.getName();
long[] vibrationPattern = {0, 300, 0, 300};
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ViewDetails.class);
stackBuilder.addNextIntent(notificationIntent);
int requestCode = person.getId();
PendingIntent pendingIntent = stackBuilder.getPendingIntent(requestCode, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Deadline due today")
.setContentText("name: " + name)
.setTicker(name+"'s debt is due today")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pi)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestCode, notification);
}
例の日付は2016年9月12日午後3時でした。現在の日付はSeptermber 10 2016です。私がしたいのは、データベース内の人物を削除するときです。通知は削除/キャンセルされます。したがって、2016年9月12日午後3時には警告通知はありません。
「通知は特定の日付に設定されました」 - これは「通知」の機能ではありません。あなたはどのようにこれをしようとしているのかを示す[mcve]を提供したいかもしれません。 – CommonsWare
アラームマネージャを使用@CommonsWare – Andre
@Andreあなたのコードの完全な例を投稿してください。 – mixel