两个具有两个不同 ID 的通知,但第二个替换为第一个
two notification with two different id's but 2nd replace to the 1st one
我想用不同的 id
设置 2 个不同的或更多 notification
,但第二个替换第一个。我有一个接收器 class 扩展 broadcastReciever
:
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);
}
}
在我的 activity 中,我创建了具有唯一 id
的 notifications
并将它们传递给 reciver.Each noteID
是唯一的,我从 DataBase
。当用户点击按钮设置通知时,方法 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
。有人可以帮助我吗?
任何帮助将不胜感激
您还必须在未决意向中传递 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);
我想用不同的 id
设置 2 个不同的或更多 notification
,但第二个替换第一个。我有一个接收器 class 扩展 broadcastReciever
:
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);
}
}
在我的 activity 中,我创建了具有唯一 id
的 notifications
并将它们传递给 reciver.Each noteID
是唯一的,我从 DataBase
。当用户点击按钮设置通知时,方法 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
。有人可以帮助我吗?
任何帮助将不胜感激
您还必须在未决意向中传递 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);