简体   繁体   English

单击通知时启动活动

[英]Launch Activity when notification is clicked

I want to open an Activity when I click on the notification from the Status bar. 当我从状态栏中单击通知时,我想打开一个活动。 I have seen this answered on StackOverflow but none of these answers work for me. 我已经在StackOverflow上看到了这个答案,但是这些答案都不适合我。 This is my code: 这是我的代码:

    notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setProgress(100, 0, false);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.drawable.ic_action_file_cloud_upload);
    notificationBuilder.setContentTitle(getString(R.string.notification_upload_title));

    //when this notification is clicked and the upload is running, open the upload fragment
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

    // set intent so it does not start a new activity
    PendingIntent intent = PendingIntent.getActivity(this, 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    notificationBuilder.setContentIntent(intent);

    this.startForeground(NOTIFICATION_ID_UPLOADING_PROGRESS, notificationBuilder.build());

Manifest.xml 的Manifest.xml

<activity
        android:name="com.android.app.MainActivity_"
        android:label="@string/app_short_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Finally I have realized that I have to write this: 终于我意识到我必须写这个:

Intent notificationIntent = new Intent(this, MainActivity_.class);

instead of 代替

Intent notificationIntent = new Intent(this, MainActivity.class);

Thanks a lot for all your answers! 非常感谢您的所有回答!

I don't think that you need all this flags and configurations just to open an activity from the PendingIntent. 我认为您不需要所有这些标志和配置就可以从PendingIntent中打开活动。 You certainly don't need 你当然不需要

notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent.FLAG_ONE_SHOT

Remove them and try again. 删除它们,然后重试。

Furthermore: is the package name of the MainActivity as intended ( com.android.app.MainActivity )? 此外:MainActivity的包名称是否符合预期( com.android.app.MainActivity )?

try below solution hope it works for you 请尝试以下解决方案,希望它对您有用

Intent intent = new Intent(this,YourActivity....class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent blogIntent = PendingIntent.getActivity(this, INT CONSTANTS, intent,
                PendingIntent.FLAG_ONE_SHOT);

From Notification 从通知

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
.
.
.
notificationBuilder.setContentIntent(blogIntent);


Notification notification = notificationBuilder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(Constants.NOTIFICATION_NEW_BLOG, notification);

You have to add custom receiver in manifest 您必须在清单中添加自定义接收方

       <receiver
            android:name=".IntentReceiver"
            android:exported="false">
            <intent-filter>

                <!-- add notification action here  -->

            </intent-filter>
        </receiver>

In Intent receiver class override on recieve method 在Intent接收器类中,在recipe方法上重写

public class IntentReceiver extends BroadcastReceiver{
@Override
    public void onReceive(Context context, Intent intent) {
        //call your activity here
    }
} 
Use this code

/* Invoking the default notification service */
        NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(this);

        mBuilder.setContentTitle("Test");
        mBuilder.setContentText(text2);
        mBuilder.setSmallIcon(R.drawable.icon);

   /* Creates an explicit intent for an Activity in your app */
        Intent resultIntent = new Intent(this, NotificationListActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(NotificationListActivity.class);

   /* Adds the Intent that starts the Activity to the top of the stack */
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

   /* notificationID allows you to update the notification later on. */
        mNotificationManager.notify(9999, mBuilder.build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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