1

好的,所以我查看了 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 有问题,但我不知道它可能是什么。感谢你的帮助!

4

3 回答 3

2

好的,所以我终于想通了!我的解决方案可能不适用于每个人的特定应用程序类型,但如果您遇到同样的问题,它应该可以为你们指明正确的方向。我从数据库中检索每个提醒的数据,因此我只需将通知 ID 设置为数据库中的主键。每个提醒都有自己唯一的主键,因此使用该键也会给我一个唯一的通知 ID!:D

非常感谢所有帮助我指出正确方向的人。:)

于 2012-09-24T06:39:32.073 回答
0

将 uniqueID 声明为静态,并且每次触发意图时都必须增加 uniqueID..

在您的情况下,notifID 应该在触发意图后增加..

Intent intent = new Intent(this, ViewLocalReminders.class);
startActivity(intent);
notifID++

试试这个。

于 2012-09-24T05:13:02.797 回答
0
public void onCreate() {





                // TODO Auto-generated method stub
                 notifyServiceReceiver = new NotifyServiceReceiver();
                 super.onCreate();
                 }

                 @Override
                 public int onStartCommand(Intent intent, int flags, int startId) {

                 // TODO Auto-generated method stub




                MY_NOTIFICATION_ID = intent.getExtras().getInt("UNIQUE");



                 IntentFilter intentFilter = new IntentFilter();
                 intentFilter.addAction(ACTION);
                 registerReceiver(notifyServiceReceiver, intentFilter);

                 // Send Notification
                 notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                 myNotification = new Notification(R.drawable.app_icon,"Notification!",System.currentTimeMillis());
                 Context context = getApplicationContext();
                 String notificationTitle = "My App";
                 String notificationText = ""+TEXT;
                 Intent myIntent = new Intent(getApplicationContext(), NotesOnRollAppActivity.class);
                 PendingIntent pendingIntent= PendingIntent.getActivity(getBaseContext(),MY_NOTIFICATION_ID, myIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
                // myNotification.defaults |= Notification.DEFAULT_SOUND;
                /* myNotification.sound = Uri.parse("android.resource://com.project.temp"+R.raw.femaleaud);*/



                     myNotification.sound = Uri.parse("android.resource://com.project.temp/"+ R.raw.femaleaud_converted);   



                 myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
                 myNotification.setLatestEventInfo(context, notificationTitle,notificationText,pendingIntent);
                 notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

                 return super.onStartCommand(intent, flags, startId);
        }

这是我使用的工作代码。希望你能得到它。。

于 2012-09-24T05:42:41.620 回答