4

I wanna open activity when user clicks a notification. I know this question is duplicated but couldn`t find a solution here is what i did

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");

Intent resultIntent = 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 resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);

mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = 
    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());

Here is documentation i followed. Does anyone have any idea? why ResultActivity couldn't open?

nAkhmedov
  • 3,522
  • 4
  • 37
  • 72

5 Answers5

5

Refer this reference link and get solution.

Start activity once notification clicked

Open application after clicking on Notification

Clicking on Notification is not starting intended activity?

All this contains the accepted solutions for Open Activity on click of notification.

So, Please refer it properly and get result with solution of your issue occured now.

Thanks for this Question's and Block owner and Accepted Answer giver. because It is useful for many developers who face issue like this.

Community
  • 1
  • 1
Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
3

Here is my final solution:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(YourService.this)
            .setContentTitle(getResources().getText(R.string.app_name))
            .setContentText(getServiceStateDescription(HomeBridgeService.this))
            .setSmallIcon(iconId)
            .setWhen(System.currentTimeMillis());

    Intent nIntent = getPreviousIntent();
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack
    stackBuilder.addParentStack(MainActivity_.class);
    stackBuilder.addNextIntent(nIntent);
    PendingIntent pendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    notificationBuilder.setContentIntent(pendingIntent);

    startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, notificationBuilder.build());


private Intent getPreviousIntent() {
    Intent newIntent = null;
    final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        final List<ActivityManager.AppTask> recentTaskInfos = activityManager.getAppTasks();
        if (!recentTaskInfos.isEmpty()) {
            for (ActivityManager.AppTask appTaskTaskInfo: recentTaskInfos) {
                if (appTaskTaskInfo.getTaskInfo().baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
                    newIntent = appTaskTaskInfo.getTaskInfo().baseIntent;
                    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
            }
        }
    } else {
        final List<ActivityManager.RecentTaskInfo> recentTaskInfos = activityManager.getRecentTasks(1024, 0);
        if (!recentTaskInfos.isEmpty()) {
            for (ActivityManager.RecentTaskInfo recentTaskInfo: recentTaskInfos) {
                if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
                    newIntent = recentTaskInfo.baseIntent;
                    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
            }
        }
    }
    if (newIntent == null) newIntent = new Intent();
    return newIntent;
}
SBotirov
  • 13,872
  • 7
  • 59
  • 81
nAkhmedov
  • 3,522
  • 4
  • 37
  • 72
2

here is my code...it works this take you to another activity note.class

package com.x.notifix;

import android.app.TaskStackBuilder;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;



public class MainActivity extends AppCompatActivity {

    NotificationCompat.Builder notifix;
    private static final int xid = 12345;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notifix = new  NotificationCompat.Builder(this);
        notifix.setAutoCancel(true);

    }



    public void click (View view){
        notifix.setSmallIcon(R.drawable.x);
        notifix.setTicker(" hollup!! ");
        notifix.setWhen(System.currentTimeMillis());
        notifix.setContentTitle(" X-Noted !!");
        notifix.setContentText( " this is where you got Xed ..");

        Intent i = new Intent(this, note.class);

        PendingIntent pendingIntent = PendingIntent.getActivity(this , 1 , i , PendingIntent.FLAG_UPDATE_CURRENT);
        notifix.setContentIntent(pendingIntent);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(xid, notifix.build());

    }

}
X-Black...
  • 1,376
  • 2
  • 20
  • 28
1

In your code,

Intent resultIntent = new Intent(this, ResultActivity.class);//Here mention the class which you want to open.

And in pending intent add as FLAG_ACTIVITY_NEW_TASK

PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    Intent.FLAG_ACTIVITY_NEW_TASK
);
Kishore Kumar
  • 616
  • 5
  • 3
  • Thank u for yr response, but it doesn't help, I think Intent.FLAG_ACTIVITY_NEW_TASK needs smth flag again. – nAkhmedov Jul 15 '15 at 11:05
  • Just check this:http://developer.android.com/reference/android/content/Intent.html it may help u for regarding activity flags.. – Kishore Kumar Jul 15 '15 at 11:09
0

With kotlin,

val channelId = "My_Channel_id"
val channelName = "My Channel"

val resultIntent = Intent(this, MainActivity::class.java)
val pendingIntent = TaskStackBuilder.create(this).run {
    addNextIntentWithParentStack(resultIntent)
    getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
}

var notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("My notification")
    .setContentText("Much longer text that cannot fit one line...")
    .setStyle(NotificationCompat.BigTextStyle()
    .bigText("Much longer text that cannot fit one line..."))
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
    .setContentIntent(pendingIntent)

with(NotificationManagerCompat.from(this)){
    notify(1,  notificationBuilder.build())
}

This creates a notification which on click opens the MainActivity.

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111