Android点击通知不会打开附加的Activity

use*_*337 5 android push-notification android-activity android-pendingintent

当我点击状态栏中的通知时,我想打开一个活动.我在StackOverflow上看到了这个问题,但这些答案都不适用于我.此问题仅在Lollipop设备上出现,最佳重现方式是:1.启动应用程序.2.重新启动应用程序.3.接收推送通知.4.单击通知并尝试3-4推送通知.

以下是我的代码:我也尝试了以下内容:

  1. 添加Intent.ACTION_MAIN
  2. Intent.CATEGORY_LAUNCHER的意图
  3. PendingIntent.FLAG_UPDATE_CURRENT.
  4. 我的AndroidManifest.xml中的android:exported ="true"

但没有任何作用.我只在Lollipop设备上看到这个问题.请注意我间歇地看到这个问题.这是与缓存的意图有关还是未交付的内容.请帮忙.

PendingIntent pendingIntent = getPendingIntent(context, payload);
Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_stat)
            .setContentTitle(res.getString(R.string.app_name))
            .setContentText(payload.getMessage())
            .setTicker(payload.getMessage())
            .setContentIntent(pendingIntent)
            .setSound(soundUri);

private PendingIntent getPendingIntent(Context context, NotificationPayload payload) {
    int requestID = (int) System.currentTimeMillis();
    Intent intent = new Intent(context, DeepLinkActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage(BuildConfig.APPLICATION_ID);
    intent.putExtra(NotificationHelper.KEY_NOTIFICATION_PAYLOAD, payload);
    return PendingIntent.getActivity(context, requestID, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);
}
Run Code Online (Sandbox Code Playgroud)

and*_*lic 0

您是否在通知 ​​setContentIntent 方法中设置了待处理意图?

试试这个,它适用于所有 api 版本

        Intent intent = new Intent(this, TestActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.logo)
                .setContentTitle(getResources().getString(R.string.notification_title))
                .setContentIntent(pendingIntent);

  NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
Run Code Online (Sandbox Code Playgroud)