简体   繁体   中英

Make notification launch an activity

How do I launch an activity when user clicks my notification? I have tried inserting an intent but It is not doing anything-

// **non cleareble notification**//
    NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification noti = new Notification.Builder(this)
            .setAutoCancel(false)
            .setContentIntent(
                    PendingIntent.getActivity(this, 0, getIntent(),
                            PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("HELLO world")
            .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS")
            .setDefaults(Notification.DEFAULT_ALL).setOngoing(true)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("ticker message")
            .setWhen(System.currentTimeMillis()).build();
    noti.flags |= Notification.FLAG_NO_CLEAR;
    notificationManager.notify(0, noti);
    Intent intent = new Intent(this, NotificationAction.class);

Do not use Notification.Builder , use NotificationCompat.Builder instead.

Intent intent = new Intent(this, NewActivity.class);

PendingIntent pendingIntent =
    PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

And then in your builder:

mBuilder.setContentIntent(pendingIntent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM