在不同时间显示两种不同的状态通知

时间:2012-09-24 05:00:46

标签: android sqlite service notifications notificationmanager

好的,所以我查看了StackOverflow上的所有主题,我找不到与我的情况相关的主题,所以我希望你们可以帮助我。 :)

我有一个应用需要针对不同时间设置的不同提醒显示多个(2个或更多个)状态通知。例如:提醒1设置为上午9:40,因此会在上午9:40弹出通知。提醒2设置为上午10:10,因此另一个不同的状态通知会在上午10点10分下面上午9:40弹出通知。

当提醒消失时,我的应用程序中没有其他通知时,我的代码可以正常工作。但是,如果有现有通知(让我们称之为提醒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();
}

}

有谁知道我在这里做错了什么?我已经试图把这个问题搞砸了好几天了,这让我疯了。我只需要两个或更多提醒,当他们有不同的名字并在不同的时间出现时,彼此共存。同样,当我的应用只发出一个通知时,一切都很完美。只有当我收到通知抽屉中的一个通知并且另一个试图进入时,它才起作用。我猜测我的notifID出现了问题,但是我不知道它可能是什么。谢谢你的帮助!

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

将uniqueID声明为static,并且每次触发intent时都必须增加uniqueID ..

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

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

尝试这个。

答案 2 :(得分: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);
        }

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