Android 通知中的待定意图不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20881226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:38:11  来源:igfitidea点击:

Pending intent in notification not working

androidandroid-pendingintent

提问by prashantwosti

Below is my block of code which should open NotificationActivitywhen the notification is tapped on. But its not working.

下面是我的代码块,NotificationActivity当点击通知时它应该打开。但它不起作用。

private void setNotification(String notificationMessage) {
    Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(R.drawable.logo)
    .setContentTitle("My Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(notificationMessage))
    .setContentText(notificationMessage).setAutoCancel(true);
    mBuilder.setSound(alarmSound);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

回答by droidx

try this:

尝试这个:

private void setNotification(String notificationMessage) {

//**add this line**
int requestID = (int) System.currentTimeMillis();

Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);

//**add this line**
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

//**edit this line to put requestID as requestCode**
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.logo)
.setContentTitle("My Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setContentText(notificationMessage).setAutoCancel(true);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

回答by pratsJ

You might be using a notification id equal to 0. There is known issue with notification id being 0. If you will use any other id, it should work.

您可能正在使用等于 0 的通知 id。通知 id 为 0 存在已知问题。如果您将使用任何其他 id,它应该可以工作。

回答by Ravi Gadipudi

I added task builder and the below block code worked for me

我添加了任务构建器,下面的块代码对我有用

Intent intent = new Intent(context, HomeActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

回答by Zeero0

If the app is already running then you need to handle it in onNewIntent

如果应用程序已经在运行,那么你需要处理它 onNewIntent

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    //Handle intent here...
}

回答by Rick Royd Aban

Try checking your AndroidManifest.xml, double check if you are navigating to an activity with an intent-filter main action, launcher category

尝试检查您的AndroidManifest.xml,仔细检查您是否正在导航到带有intent-filter main action, launcher category

For example,

例如,

enter image description here

enter image description here

I couldn't make my pending intentto navigate to HomeActivitybut it works when navigating to SplashScreen

我无法pending intent导航到,HomeActivity但它在导航到SplashScreen

Hope this helps.

希望这可以帮助。

回答by live-love

Added the requestid, but the intent was still not opening.

添加了requestid,但意图仍然没有打开。

This is the solution that worked for me:

这是对我有用的解决方案:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Setting those flags to clear the activities below the intent activity.

设置这些标志以清除意图活动下方的活动。