android How to start activity when user clicks a notification?

寵の児 提交于 2020-01-21 00:54:40

问题


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?


回答1:


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.




回答2:


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;
}



回答3:


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());

    }

}



回答4:


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
);


来源:https://stackoverflow.com/questions/31428325/android-how-to-start-activity-when-user-clicks-a-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!