Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android How to start activity when user clicks a notification?

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?

like image 608
nAkhmedov Avatar asked Dec 24 '22 17:12

nAkhmedov


2 Answers

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.

like image 199
Mr.Sandy Avatar answered Jan 05 '23 17:01

Mr.Sandy


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;
}
like image 27
nAkhmedov Avatar answered Jan 05 '23 18:01

nAkhmedov