java - 通知恢复应用程序而不是重新启动

标签 java android notifications resume

我已经阅读了关于这个问题的多个问题和答案,但是我无法让其中任何一个为我工作。

我收到一条通知,在单击时我希望将应用程序置于最前面并恢复,而不是关闭并重新启动。

这是我的通知码

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("example")
            .setContentText("example");
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainActivity.class);
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(MainActivity.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);
    int mId = 0;
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());

在我的 list 文件中有

        android:launchMode="singleTop"

谁能看出哪里出了问题?我没有收到任何错误,尽管通知拒绝恢复应用程序而是重新启动它。

最佳答案

使用这个修复:

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Sound Asleep")
            .setContentText("Click to play and stop sounds");
    // Creates an explicit intent for an Activity in your app
    final Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.

    // Adds the back stack for the Intent (but not the Intent itself)
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int mId = 0;
    // mId allows you to update the notification later on.
    mNotificationManager.notify(mId, mBuilder.build());
}

在 list 中

android:launchMode="singleTop"

关于java - 通知恢复应用程序而不是重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21995497/

相关文章:

java - 让 Okhttp3 绑定(bind)到特定的本地地址

JavaFX 表格 View 颜色

android - 插入android日历事件后返回android中的上一个 Activity

android - Firebase 发送多个推送通知而不是堆叠或替换

notifications - Chef 'notifies :action, ' 资源', :before' work at all as it should?

java - 如何从不同的方法打印多个变量?

java - 在 spring 中从 <property> 转换为 p 命名空间时出现问题

iphone - 如何为 2D 游戏中的角色创建平滑路径

android - 通知启动多个 Activity 实例

android - 使用服务的传入短信通知