两个不同ID的通知但第二个替换为第一个

时间:2016-07-27 11:39:27

标签: android broadcastreceiver android-notifications

我想设置2个不同的notification idbroadcastReciever,但第二个替换第一个public class NotificationPublisher extends BroadcastReceiver { public static String NOTIFICATION_ID = "notification-id"; public static String NOTIFICATION = "notification"; @Override public void onReceive(Context context, Intent intent) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = intent.getParcelableExtra(NOTIFICATION); int id = intent.getIntExtra(NOTIFICATION_ID, 0); Log.i("ShowNotifyID:", "" + id); notificationManager.notify(id, notification); } } 。我有一个延伸notifications的reciver类:

id

在我的活动中,我使用唯一的noteID创建DataBase并将其传递给reciver。每个onClickSet()都是唯一的,我会从public class ReminderActivity extends Activity { int noteID; String noteText; DatePicker datePicker; TimePicker timePicker; NoteManagerHelper db; int isReminder; NoteItem item; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.reminder_activity); datePicker = (DatePicker) findViewById(R.id.datePicker1); timePicker = (TimePicker) findViewById(R.id.timePicker1); Intent reminderIntent = getIntent(); noteText = reminderIntent.getStringExtra("noteText"); noteID = reminderIntent.getIntExtra("noteID", 0); db = new NoteManagerHelper(this); isReminder = db.getIsReminder(noteID); if (isReminder == 1) { setShowingTimeInDatePicker(); } } private void setShowingTimeInDatePicker() { NoteItem item = db.getAnItem(noteID); datePicker.updateDate(item.reminderYear, item.reminderMonth, item.reminderday); Log.i("DATEPICKER:" + item.reminderYear, "" + item.reminderMonth + "-" + item.reminderday); timePicker.setCurrentHour(item.reminderHour); timePicker.setCurrentMinute(item.reminderMinute); } public void onClickSet(View v) {//onClick of the set button //if isreminder is not 0 that means this note already have a notifaction in receiver, we need to cancel it first and start a new one if (isReminder == 1) { removeReminderFromService(); } Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH, datePicker.getMonth()); calendar.set(Calendar.YEAR, datePicker.getYear()); calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth()); calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour()); calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute()); calendar.set(Calendar.SECOND, 0); long delay = calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis(); item = new NoteItem(noteID, noteText); item.isReminder = 1; item.reminderYear = datePicker.getYear(); item.reminderMonth = datePicker.getMonth(); item.reminderday = datePicker.getDayOfMonth(); item.reminderHour = timePicker.getCurrentHour(); item.reminderMinute = timePicker.getCurrentMinute(); db.setReminder(item); scheduleNotification(getMyNotification(noteText), delay); finish(); } public void onClickRemoveReminder(View v) { if (isReminder == 1) { removeReminderFromService(); db.setIsReminderToDB(noteID, 0); isReminder = 0; Toast.makeText(ReminderActivity.this, "Reminder removed successfully!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(ReminderActivity.this, "No reminder set yet!", Toast.LENGTH_SHORT).show(); } } public void removeReminderFromService() { NotificationManager notificationManager = (NotificationManager) (ReminderActivity.this).getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(noteID); Log.i("RemoveNotifyID:", "" + noteID); } public void scheduleNotification(Notification notification, long delay) { Intent notificationIntent = new Intent(this, NotificationPublisher.class); notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, noteID); notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); long futureInMillis = SystemClock.elapsedRealtime() + delay; Toast.makeText(this, "" + futureInMillis, Toast.LENGTH_SHORT).show(); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); } private Notification getMyNotification(String content) { Notification.Builder builder = new Notification.Builder(this); builder.setContentTitle("Rnote"); builder.setAutoCancel(false); builder.setContentText(content); builder.setSmallIcon(R.drawable.ic_launcher); return builder.getNotification(); } } 中检索它。当用户点击时在按钮上设置通知,将调用方法id's

GridFieldDetailForm
我确实读过类似的问题。但解决方案主要是使用不同的doSave。任何人都可以帮助我吗? 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您还必须在待处理的意图中传递 notifiaction_id

传递notification_id

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notification_id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

而不是挂起意图中的0

 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);