4mahmoud
4mahmoud

Reputation: 853

Android: Detect status bar notification click

When my application fires a notification i want a method to be called after the user clicks the notification

This's how i create a notification :

    Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    alarmIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    Bundle bundle = new Bundle();
    PendingIntent pendingIntent;
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Date date = new Date();

    bundle.putString("name", "name");
    bundle.putString("body", "body");
    alarmIntent.putExtras(bundle);

    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 10000, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    manager.setExact(AlarmManager.RTC_WAKEUP, date.getTime()+5000, pendingIntent); // fire after 5 seconds of launch

So how to edit my code to achieve that .. thank you

Upvotes: 1

Views: 4583

Answers (1)

Attaullah
Attaullah

Reputation: 4021

try this code for notification

for calling

    notificationshow(this);

method definition

    notificationshow(Context c) {
        /*********** Create notification ***********/
        Intent intent = new Intent(this, act_run_ifclicknotifctn.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        Notification n = new Notification.Builder(this)
            .setContentTitle("New Notification text title")
            .setContentText("New content text")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent)
            .setAutoCancel(true)
            /*.addAction(R.drawable.ic_launcher, "Call", pIntent)
            .addAction(R.drawable.ic_launcher, "More", pIntent)
            .addAction(R.drawable.ic_launcher, "And more", pIntent)*/
            .build();

        NotificationManager NotiMgr = (NotificationManager) this
            .getSystemService(NOTIFICATION_SERVICE);

        NotiMgr.notify(0, n);
    }

Upvotes: 1

Related Questions