-1

I had set a notification in my app. Its working fine. If i click the notification in statusbar it takes to my app.
Now i need to set some work if the notification is clicked where can i set this? Is there any method that implicitly got invoked when a notification is clicked?
Also i want to remove that notification if it is clicked,how to do so?

this is my code

notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
Intent inty=getIntent();
note = new Notification(R.drawable.icon, "New E-mail", System.currentTimeMillis());  
PendingIntent intent = PendingIntent.getActivity(MainActivity.this, 0, inty, 0);  
note.setLatestEventInfo(MainActivity.this, "New E-mail", "You have one unread message.", intent);
notifManager.notify(R.string.search_hint, note); 
NewUser
  • 3,729
  • 10
  • 57
  • 79
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64

2 Answers2

1

You could add some extra data to the intent and then in your activity look for it in the onCreate and onNewIntent methods.

For example:

inty.putExtra("came from notification", true);

You can then read that out via the intent passed to onNewIntent or in onCreate by using getIntent().

intent.getBooleanExtra("came from notification", false);
ian.shaun.thomas
  • 3,468
  • 25
  • 40
1

Try to call broadcastReceiver it may helpful for your requirement,

Intent notificationIntent = new Intent(this, dummy_activity.class);
notificationIntent.setAction("android.intent.action.MAIN");
notificationIntent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                                notificationIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT | 
                                Notification.FLAG_AUTO_CANCEL);

// Now, once this dummy activity starts send a broad cast to your parent activity and finish the pending activity
//remember you need to register your broadcast action here to receive.
BroadcastReceiver call_method = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action_name = intent.getAction();
        if (action_name.equals("call_method")) {
            // call your method here and do what ever you want.
        }
    }
};
registerReceiver(call_method, new IntentFilter("call_method"));
Ziem
  • 6,579
  • 8
  • 53
  • 86
No_Rulz
  • 2,679
  • 1
  • 20
  • 33