wholee1
wholee1

Reputation: 1501

Android how to intent activity when notification is pressed

How can I intent the activity after clicking the image on notification bar? How can I make intent? Thanks.

public class AlarmService extends BroadcastReceiver {
NotificationManager nm;

@Override
public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "Check your fridge";
    CharSequence message = "It's time to eat!";
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(), 0);
    Notification notif = new Notification(R.drawable.fridge_icon3,
            "Keep Fridge", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    notif.defaults |= Notification.DEFAULT_SOUND;
    notif.flags |= Notification.FLAG_AUTO_CANCEL;
    nm.notify(1, notif);
}
}

Upvotes: 0

Views: 1165

Answers (1)

Ifor
Ifor

Reputation: 2835

The code you have posted already has an intent in it e.g. you have new Intent() in the contentIntent. It is not much use as you have not filled it in with anything. You need somthing like this.

Intent notificationIntent = new Intent(this, MyAvtivity.class);
// modify the intent as nessasary here.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

User Guide here

Upvotes: 1

Related Questions