0

i am developing one application in that i have successfully receive notification but i unable to remove notification i wrote some code it is not working please tell me where i made mistake

my code

GcmIntentService

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, 0,
        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();
    /* NotificationCompat.InboxStyle inboxStyle =
            new NotificationCompat.InboxStyle();


    String[] events = new String[6];
    // Sets a title for the Inbox style big view
    inboxStyle.setBigContentTitle("Event tracker details:");

    // Moves events into the big view
    for (int i=0; i < events.length; i++) {

        inboxStyle.addLine(events[i]);
    }
    // Moves the big view style object into the notification object.
    mBuilder.setStyle(inboxStyle);
*/

    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);
    mNotificationManager.notify((int) when, mBuilder.build());



}
public void CancelNotification(Context ctx, int notId) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) ctx
        .getSystemService(ns);
    nMgr.cancel(notId);

}
}

in receive activity

TextView name;
TextView deal;
TextView valid;
TextView address;
JSONObject json;
GcmIntentService serv;
Context mContext;

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


}

@
Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    serv = new GcmIntentService();
    serv.CancelNotification(getApplicationContext(), (int) Calendar.getInstance().getTimeInMillis());
}
}  
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
DurgaPrasad Ganti
  • 1,008
  • 4
  • 20
  • 41

2 Answers2

2
serv.CancelNotification(getApplicationContext(), (int) Calendar.getInstance().getTimeInMillis());

mNotificationManager.notify((int) when, mBuilder.build());

I am sure in above case (int) when != (int) Calendar.getInstance().getTimeInMillis()

You should use same Ids for showing and cancelling the notification.

Below is the reference link.

http://developer.android.com/reference/android/app/NotificationManager.html#cancel(java.lang.String, int)

Vipul
  • 27,808
  • 7
  • 60
  • 75
  • i wrote in two separate classes public void CancelNotification(Context ctx) { String ns = Context.NOTIFICATION_SERVICE; NotificationManager nMgr = (NotificationManager) ctx .getSystemService(ns); nMgr.cancel((int) Calendar.getInstance().getTimeInMillis()); } – DurgaPrasad Ganti Feb 24 '14 at 12:36
  • in on receive activity under onResume() serv=new GcmIntentService(); serv.CancelNotification(getApplicationContext()); – DurgaPrasad Ganti Feb 24 '14 at 12:37
  • @Durga Create Constant int Id field and use same id in both the places. – Vipul Feb 24 '14 at 12:37
  • how i can pass same id from intentservice class to activity – DurgaPrasad Ganti Feb 24 '14 at 12:39
  • Create a New Helper class Utils.java and declare public static final int id variable there.Then you can access it in any class like Utils.id – Vipul Feb 24 '14 at 12:46
  • ya its working but i have been receiving multiple notificatons but if i use this method it removes privoius notification,only shows one notification – DurgaPrasad Ganti Feb 24 '14 at 13:03
  • i want remove only when i open notification,i hope u understand my problem – DurgaPrasad Ganti Feb 24 '14 at 13:08
0

Dude here serv.CancelNotification(getApplicationContext(), (int) Calendar.getInstance().getTimeInMillis());

this : (int) Calendar.getInstance().getTimeInMillis() should be equal to the Notification ID that you want to cancel or here you send current time in millis which is believe is not the notification id that started the notification you want to close

Try this to start the notification use this : mNotificationManager.notify((int) when, mBuilder.build()); ok

but fine a way to save this when for this notif as its ID

then to cancel it just use the previous saved when to retrieve the notif serv.CancelNotification(getApplicationContext(), (int) when);

murielK
  • 1,000
  • 1
  • 10
  • 21