How can i open Activity when notification click
Asked Answered
H

2

5

I need use notification with click event, i have notification method but this method don't open my activity.

My code:

 private void sendNotification(String msg) {

        NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this)
        .setContentTitle("EXX")
        .setSmallIcon(R.drawable.ic_launcher)
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg)          
        .setOngoing(true);          
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}    

is this possible,

thanks.

Hallmark answered 3/2, 2017 at 14:24 Comment(2)
Possible duplicate of Open application after clicking on NotificationDenten
Not a duplicate. OP is just missing the setContentIntent()Whipstall
H
7

Yes, it is possible.

Change Your method, like that;

private void sendNotification(String msg) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this)
            .setContentTitle("EXX")
            .setSmallIcon(R.drawable.ic_launcher)
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText(msg))
            .addAction(getNotificationIcon(), "Action Button", pIntent)
            .setContentIntent(pIntent)
            .setContentText(msg)
            .setOngoing(true);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

and add your mainactivity

    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

this code working.

Hairstreak answered 3/2, 2017 at 14:27 Comment(2)
oh thanks ! this code working on me ! i love you bro <3Hallmark
Please be aware that addAction(getNotificationIcon(), "Action Button", pIntent) is deprecatedCoaction
M
7

Hey @John it's very simple

you just have to do

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

... // Because clicking the notification opens a new ("special") activity, there's // no need to create an artificial back stack.

PendingIntent pendingIntent = TaskStackBuilder.create(getApp())
            .addNextIntent(intent)
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

And set pending Intent in mBuilder

private void sendNotification(String msg) {
        NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this)
        .setContentTitle("EXX")
        .setSmallIcon(R.drawable.ic_launcher)
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg)          
        .setOngoing(true);    
        .setContentIntent(pendingIntent)
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
} 

and you done :)

Magnificat answered 3/2, 2017 at 14:34 Comment(1)
In place of getApp() could use MainActivity.class or the specific activity name to specify the name of your activity.Twocolor

© 2022 - 2024 — McMap. All rights reserved.