好的,所以我查看了 StackOverflow 上的所有线程,但找不到与我的情况相关的线程,所以我希望你们能在这里帮助我。:)
我有一个应用程序需要为在不同时间设置的不同提醒显示多个(2 个或更多)状态通知。例如:提醒 1 设置为上午 9:40,因此会在上午 9:40 弹出通知。提醒 2 设置在上午 10:10,因此在上午 9:40 已经弹出的通知下方会在上午 10:10 弹出另一个不同的状态通知。
当提醒消失时,我的应用程序没有其他通知时,我的代码有效。但是,如果存在现有通知(我们称之为提醒 1)并且用户没有关闭它,则新通知(我们称之为提醒 2)永远不会出现,并且提醒 1 只是将其时间戳更新为提醒 2 的时间本来应该关掉的。基本上,我的代码不适用于应该在不同时间发出并彼此共存的多个单独的提醒。
这是我在通知中设置提醒名称时的代码:
/* Create a unique notifID that increments itself by 1 every time a new reminder is saved. */
int notifID = 0;
notifID = notifID + 1;
Toast toast = Toast.makeText(context, context.getString(R.string.reminder_saved), Toast.LENGTH_LONG);
toast.show();
c.set(mYear, mMonth, mDay); //Set the notification date.
c.set(Calendar.HOUR_OF_DAY, pHour); //Set the notification hour.
c.set(Calendar.MINUTE, pMinute); //Set the notification minute.
c.set(Calendar.SECOND, 0); //Set the notification second (always 0).
//Use AlarmManager to trigger the notification/alarm.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//Get the current date and time.
Calendar calendar = Calendar.getInstance();
//Set the time for the notification to occur.
calendar.set(Calendar.YEAR, mYear);
calendar.set(Calendar.MONTH, mMonth);
calendar.set(Calendar.DAY_OF_MONTH, mDay);
calendar.set(Calendar.HOUR_OF_DAY, pHour);
calendar.set(Calendar.MINUTE, pMinute);
calendar.set(Calendar.SECOND, 0);
//PendingIntent to launch activity when the alarm triggers.
Intent i = new Intent();
/* Assign a notification ID to each notification. Because it is incremented by 1 for each new notification (see lines 1 and 2), there shouldn't be any duplicates. */
i.putExtra("NotifID", notifID);
/* Get the name of reminder from the textbox and pass it into the intent. */
i.putExtra("Reminder_Name", editRemindMeTo.getText().toString());
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0, i, 0);
//Set the alarm to trigger.
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent);
Intent intent = new Intent(this, ViewLocalReminders.class);
startActivity(intent);
这是我的实际通知代码(它位于扩展 Activity 的单独文件中):
public class DisplayReminderNotification extends SherlockActivity {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the notification ID from the saved reminder (see previous code block).
int notifID = getIntent().getExtras().getInt("NotifID");
//PendingIntent stores the Activity that should be launched when the user taps the notification.
Intent i = new Intent();
i.putExtra("NotifID", notifID);
PendingIntent detailsIntent =
PendingIntent.getActivity(this, 0, i, 0);
//Get the name of the reminder from the previous activity (see previous code block)
String reminderName = getIntent().getExtras().getString("Reminder_Name");
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.flag_red_large,
"App Name",
System.currentTimeMillis());
CharSequence from = "App Name";
//Print the name of the reminder as the notification body.
CharSequence message = reminderName;
notif.setLatestEventInfo(this, from, message, detailsIntent);
//Pause for 100ms, vibrate for 250ms, pause for 100ms, and vibrate for 500ms.
notif.defaults |= Notification.DEFAULT_SOUND;
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notifID, notif);
//Destroy the activity/notification.
finish();
}
}
有谁知道我在这里做错了什么?几天来我一直试图弄清楚这个问题,这让我发疯了。当他们有不同的名字并在不同的时间响起时,我只需要 2 个或更多的提醒来共存。同样,当我的应用程序只发出一个通知时,一切都运行良好。仅当我在通知抽屉中收到一个未触及的通知并且另一个尝试进入时,它才起作用。我猜我的 notifID 有问题,但我不知道它可能是什么。感谢你的帮助!