Вопрос

Okay, so I've looked at all the threads on StackOverflow, and I couldn't find one that pertains to my situation, so I'm hoping you guys can help me out here. :)

I've got an app that needs to show multiple (2 or probably more) status notifications for different reminders that have been set at different times. Eg: Reminder 1 is set for 9:40AM so a notification pops up at 9:40AM. Reminder 2 is set at 10:10AM, so another different status notification pops up at 10:10AM below the notification that already popped up at 9:40AM.

My code works when there are no other notifications from my app when a reminder goes off. However, if there's an existing notification (let's call it Reminder 1) and the user hasn't dismissed it, the new notification (let's call it Reminder 2) never shows up, and Reminder 1 simply updates its timestamp to the time that Reminder 2 was supposed to go off. Basically, my code isn't working for multiple, separate reminders that are supposed to go off at different times and coexist with one another.

Here's the code from when I set the name of the reminder in the notification:

    /* 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);

Here's my actual notification code (It's in a separate file that extends 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();
}

}

Does anyone know what I'm doing wrong here? I've been trying to figure this one out for days and it's been driving me nuts. I just need 2 or more reminders to coexist which each other when they've got different names and go off at different times. Again, everything works perfectly when there's only one notification that comes up from my app. It doesn't work only when I've got one notification thats sitting untouched in the notification drawer and another one tries to come in. I'm guessing there's something wrong with my notifID, but I don't what it could be. Thanks for all your help!

Это было полезно?

Решение

OKAY, so I finally figured this out! My solution might not apply to everyone's specific app type, but it should hopefully give you guys a pointer in the right direction, if you're having the same problem. I'm retrieving data from a database for each reminder, so I simply set the notification ID to the primary key in the database. Each reminder has its own unique primary key, so using that key gives me a unique notification ID as well! :D

Thanks a lot to everyone who helped point me in the right direction here. :)

Другие советы

declare uniqueID as static and each and every time you fire intent you have to increment uniqueID..

in your case notifID should be increment after firing intent..

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

TRY THIS.

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);
        }

This is the working code dat i have used. Hope you ll get it..

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top