3

I am developing an application in which I receive multiple notifications. I am successfully receiving multiple notifications with different data, but when I use setAutoCancel(true); for remove notification, and I receive multiple notifications it shows only last notification on notification bar.

Here my problem shows multiple notification, but if I remove setAutoCancel(true); method I can successfully receive multiple notifications, but I want to remove notification with multiple notifications.

GcmIntentService.java:

public class GcmIntentService extends IntentService {
    Context context;

    //System.currentTimeMillis();

    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    public static final String TAG = "GCM Demo";

    public GcmIntentService() {
        super("GcmIntentService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        Bundle extras = intent.getExtras();
        String msg = intent.getStringExtra("message");
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);


        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification(RegIdDTO.REG_ID,"Send error: " +
                        extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification(RegIdDTO.REG_ID,"Deleted messages on server: " +
                        extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                //sendNotification("Received: " + extras.toString());
                sendNotification(RegIdDTO.REG_ID,msg);
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
    private void sendNotification(long when,String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);


        Intent myintent = new Intent(this, ReceiveActivity.class);
        myintent.putExtra("message", msg);
        myintent.setData(Uri.parse("content://"+when));

        PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_gcm)
                .setContentTitle("Event tracker")
                .setContentText("Events received");
        Notification notification=mBuilder.build();


        AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

        /* Even if the mode is set to "Sound & Vibration" in the phone, 
         * the status code that getRingerMode() returns is RINGER_MODE_NORMAL.
         */
        switch (am.getRingerMode())
        {
            case AudioManager.RINGER_MODE_VIBRATE:
                mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
                break;
            case AudioManager.RINGER_MODE_NORMAL:
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
                break;
            default:
                mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        }

        mBuilder.setContentIntent(contentIntent);
        mBuilder.setAutoCancel(true);
        mNotificationManager.notify((int) when, mBuilder.build());

    }

    my receive activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);
        Intent intent = getIntent();


        name = (TextView) findViewById(R.id.name);
        deal = (TextView) findViewById(R.id.deal);
        valid = (TextView) findViewById(R.id.valid);
        address = (TextView)findViewById(R.id.address);
        String message = intent.getExtras().getString("message");
        try {
            json = new JSONObject(message);
            String stime = json.getString("name");
            name.setText(stime);

            String slecturename = json.getString("deal");
            deal.setText(slecturename);

            String sroom = json.getString("valid");
            valid.setText(sroom);

            String sfaculty = json.getString("address");
            address.setText(sfaculty);


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
ElefantPhace
  • 3,806
  • 3
  • 20
  • 36
DurgaPrasad Ganti
  • 1,008
  • 4
  • 20
  • 41
  • 2
    what is your problem? remove notification or show's multiple notification ? – Shayan Pourvatan Feb 25 '14 at 05:32
  • here my problem shows multiple notification ,but if i remove setAutoCancel(true); method i can successfully receive multiple notifications, but i want remove notification with multiple notifications – DurgaPrasad Ganti Feb 25 '14 at 05:33
  • for removing notification you can handle with id see http://stackoverflow.com/questions/3595232/android-remove-notification-from-notification-bar – Shayan Pourvatan Feb 25 '14 at 05:38
  • ya wrote like that also public void CancelNotification(Context ctx) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager nMgr = (NotificationManager) ctx .getSystemService(ns); nMgr.cancel(RegIdDTO.REG_ID); } – DurgaPrasad Ganti Feb 25 '14 at 05:42
  • it is also same problem – DurgaPrasad Ganti Feb 25 '14 at 05:42
  • you have just one id for your notification,( `RegIdDTO.REG_ID` ) is that true? if yes so how you show multiple notification? – Shayan Pourvatan Feb 25 '14 at 05:48
  • public class RegIdDTO { public static final int REG_ID= (int) Calendar.getInstance().getTimeInMillis(); } – DurgaPrasad Ganti Feb 25 '14 at 05:50
  • Check this link here is solution you are looking for [remove notification from notification bar ][1] [1]: http://stackoverflow.com/questions/3595232/android-remove-notification-from-notification-bar – Muhammad Usman Ghani Feb 25 '14 at 05:52
  • you need multiple id for showing multiple notification, and with id of them you can remove notification, with your final id you have one id, i don't know you can show multiple notification, because this must be overriding each other – Shayan Pourvatan Feb 25 '14 at 05:57

1 Answers1

2

Use the NotificationManager to cancel your notification. You only need to provide your notification id

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
Namrata
  • 1,683
  • 1
  • 17
  • 28
  • ya i wrote like that but it shows only one notification on notification bar public void CancelNotification(Context ctx) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager nMgr = (NotificationManager) ctx .getSystemService(ns); nMgr.cancel(RegIdDTO.REG_ID); } public class RegIdDTO { public static final int REG_ID= (int) Calendar.getInstance().getTimeInMillis(); } – DurgaPrasad Ganti Feb 25 '14 at 06:02
  • and in receive activity @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); serv=new GcmIntentService(); serv.CancelNotification(getApplicationContext()); } – DurgaPrasad Ganti Feb 25 '14 at 06:04