【问题标题】:Issue with Notifications in Oreo奥利奥通知问题
【发布时间】:2018-07-03 02:51:30
【问题描述】:

Google 在 Oreo 中创建了一个巨大的 breaking change - 他们停止了通知在所有应用程序中的工作。我现在正在处理更改我所有的应用程序,但我遇到了几个问题。 . .

现在会发布通知,当用户触摸通知时,它会正确触发应用程序。 但是,(1) 通知不会消失。要摆脱它,用户必须删除它。另外 (2) 在发布多个通知后,我的应用图标上的点仍停留在 One。

我正在使用 Bipin Pandey 发布的以下通知助手类:

public class NotificationHelper {

    private Context mContext;
    private NotificationManager mNotificationManager;
    private NotificationCompat.Builder mBuilder;
    public static final String NOTIFICATION_CHANNEL_ID = "10001";

    public NotificationHelper(Context context) {
        mContext = context;
    }

    /**
     * Create and push the notification
     */
    public void createNotification(String title, String message)
    {
        /**Creates an explicit intent for an Activity in your app**/
        Intent resultIntent = new Intent(mContext , MainActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
                0 /* Request code */, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder.setSmallIcon(R.drawable.ticon);
        mBuilder.setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(false)
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setContentIntent(resultPendingIntent);

        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
        {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }
        assert mNotificationManager != null;
        mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
    }


}

下面的代码执行这个帮助类

NotificationHelper myNotify = new NotificationHelper(context);
myNotify.createNotification("New Message",mesText);

【问题讨论】:

    标签: android notifications android-8.0-oreo


    【解决方案1】:

    您设置了setAutoCancel(false),因此当您触发通知时,通知不会自动取消。如果您想点击通知以删除通知(以及与通知关联的点),请删除该行。

    【讨论】:

    • 感谢您的快速回答。你知道为什么在创建多个通知时我的点总是显示“1”吗?
    • 该数字只是您在通知抽屉中看到的通知计数。从您的代码中,您的 notify() 调用始终使用相同的 id,因此每个通知只是替换以前的通知,而不是创建多个单独的通知。
    【解决方案2】:

    您将 setAutoCancel 属性设置为 false。因此,当用户触摸它时,通知不会被关闭。这适用于您想要自己处理删除通知的情况。

    从 NotificationBuilder 中删除该行,您的逻辑应该会按预期工作。

    方法说明:

    public Notification.Builder setAutoCancel (boolean autoCancel)

    当用户触摸它时自动关闭此通知。

    在此处阅读有关自动取消通知的更多信息:

    https://developer.android.com/reference/android/app/Notification.Builder.html#setAutoCancel(boolean)

    【讨论】: