0

I am developing an android app in which i want to show a new message in the Notification Bar with each new notification. I am able to show notificaitions at specific time but i want to show each notification with a differnent Message.

Below i am pasting smaple code that shows two notifications at the specified time. I have used the Alarm manager class to show the notifications . MainActivity

Calendar calendar = Calendar.getInstance();

     calendar.setTime(new Date(formattedDate1));
     calendar.set(Calendar.MONTH, 9);
     calendar.set(Calendar.YEAR, 2013);
     calendar.set(Calendar.DAY_OF_MONTH, 25);


    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 11);
    calendar.set(Calendar.SECOND, 0);       

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
    myIntent.putExtra("myIntent", "Notification1");
    myIntent.setType("intent1");
    pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 0,
            myIntent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
            pendingIntent1);

    Calendar calendar_new = Calendar.getInstance();

    // calendar_new.setTime(new Date(, month, day))

    calendar_new.set(Calendar.HOUR_OF_DAY, 13);
    calendar_new.set(Calendar.MINUTE, 12);
    calendar_new.set(Calendar.SECOND, 0);

    Intent myIntentnew = new Intent(MainActivity.this, MyReceiver.class);
    myIntentnew.setType("intent2");
    myIntentnew.putExtra("myIntentnew", "Notification2");
    pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 1,
            myIntentnew, 0);

    alarmManager.set(AlarmManager.RTC, calendar_new.getTimeInMillis(),
            pendingIntent2);


} // end onCreate

Notification Service

private NotificationManager mManager;
Notification notification;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext()
            .getSystemService(
                    this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),
            MainActivity.class);

    notification = new Notification(R.drawable.ic_launcher,
            "This is a test message!", System.currentTimeMillis());


    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.setLatestEventInfo(this.getApplicationContext(),
            "We succeded", "hi!", pendingNotificationIntent);

    mManager.notify(0, notification);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

    mManager.cancelAll();
}

If anybody could help me with this . Thanks in advance

Satyen Udeshi
  • 3,223
  • 1
  • 19
  • 26

2 Answers2

1

change

 mManager.notify(0, notification); 

to

  mManager.notify(count , notification);

where count increase every time by 1 .

   mManager.notify(0, notification) 

replace message every time

   Intent serviceIntent = new Intent(YourService.class.getName())
   serviceIntent.putExtra("UserID", "123456");
   context.startService(serviceIntent);

When the service is started its onStartCommand() method will be called so in this method you can retrieve the value (UserID) from the intent object for example

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

String userID = intent.getStringExtra("UserID");

return START_STICKY;
  }
Shivang Trivedi
  • 2,182
  • 1
  • 20
  • 26
  • I did try your solution but its not showing new message and as an update i passing static date values in here and static message but once i am able to show new message i will fetch the new message from database . – Satyen Udeshi Oct 25 '13 at 12:17
  • I tried out your solution it is showing the first message but for the next time interval it gives an null pointer Exception. Any Idea about that.?? I am passing my custom messages with intent in MainActivity, the code for which i have shown above – Satyen Udeshi Oct 25 '13 at 12:23
  • can u get data from onStartCommand() ? override this in your service http://stackoverflow.com/questions/3293243/android-pass-data-from-activity-to-service-using-an-intent – Shivang Trivedi Oct 25 '13 at 12:23
  • yes i am able to get data from onStartCommand(), i have also solved the NullPointerException but still i am not able to show custom message in the second notification – Satyen Udeshi Oct 25 '13 at 12:28
1

You can change this mManager.notify(0, notification); to mManager.notify((int) when, notification);

where long when = System.currentTimeMillis();

Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69