【问题标题】:Notifications are being delayed in Android OreoAndroid Oreo 中的通知被延迟
【发布时间】:2018-06-01 13:27:40
【问题描述】:

我正在开发一个在 Marshmallow 中运行良好的提醒应用程序,但我刚刚购买了带有 Android Oreo 的 Xperia XZ1 紧凑型,通知被延迟甚至根本不显示。当提醒的时间过去了,手机什么也不做,但如果我解锁屏幕,通知就会突然出现。根据应用程序的日志,我的广播接收器工作并及时收到警报,然后成功生成通知。但是系统延迟显示通知,我不知道原因。我的应用程序已获得所有通知权限,配置为从省电模式中忽略等等。这是显示通知的代码。正如我所说,这段代码总是会及时执行,但通知的声音会随机延迟几分钟,或者直到我手动打开屏幕。

有没有办法解决这个问题?

private void makeNotification(Long rowId, String title, String body) {
    android.app.NotificationManager mgr = (android.app.NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
    Intent notificationIntent;

    long notificationId;
    PendingIntent pi;
    if (rowId == null) {
        notificationId = 0;
        notificationIntent = new Intent(context, ReminderListActivity.class);
        pi = PendingIntent.getActivity(context, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    } else {
        notificationId = rowId;
        notificationIntent = new Intent(context, ReminderEditActivity.class);
        notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId);
        long rowIdPrimitive = rowId;
        pi = PendingIntent.getActivity(context, (int)rowIdPrimitive, notificationIntent,
                PendingIntent.FLAG_ONE_SHOT);
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(android.R.drawable.stat_sys_warning)
            .setContentTitle(title)
            .setContentText(body)
            .setContentIntent(pi)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(body));

    Notification note = mBuilder.build();

    note.defaults |= Notification.DEFAULT_SOUND;
    note.flags |= Notification.FLAG_AUTO_CANCEL;

    // An issue could occur if user ever enters over 2,147,483,647 tasks. (Max int value).
    // I highly doubt this will ever happen. But is good to note.
    int id = (int)((long)notificationId);
    mgr.notify(id, note);
    NotificationManager.setNotified(context, id);
}

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    会出现这种情况,但是对于奥利奥,不需要提供频道和群组

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (LOG_DEBUG) Log.v(TAG, " : version : >=O ");
    
    
            NotificationChannel mChannel = new NotificationChannel
                    (NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    
            mChannel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.CYAN);
            mChannel.enableVibration(true);
            notificationManager.createNotificationChannel(mChannel);
    
    
            NotificationChannelGroup mGroup = new NotificationChannelGroup(NOTIFICATION_GROUP_ID, NOTIFICATION_GROUP_NAME);
            notificationManager.createNotificationChannelGroup(mGroup);
    
    
            NotificationCompat.Builder builder = new NotificationCompat.Builder
                    (context, NOTIFICATION_CHANNEL_ID)
    
                    .setContentTitle(context.getString(R.string.notification_title))
                    .setContentText(context.getString(R.string.notification_subtext))
                    .setSmallIcon(R.drawable.ic_pawprint)
                    .setAutoCancel(true)
                    .setDeleteIntent(piDismiss)
                    //.setContentIntent(pIpanel)
                    .setCustomContentView(collapsedView)
                    .setCustomBigContentView(expandedView)
                    .setStyle(new NotificationCompat.DecoratedCustomViewStyle());
    
            notification = builder.build();
        }
    
        if (notificationManager != null) {
            notificationManager.notify(NOTIFICATION_ID, notification);
        }
    

    这个很完美

    【讨论】:

    • 谢谢,我会测试一下。但我认为渠道不是那么必要。我有另一个人(github.com/jonasbleyl/recurrence/blob/master/app/src/main/java/…)的另一个提醒应用程序,他不使用渠道,他发出通知的方式和我一样,它的应用程序可以工作而我的不行。
    • 如果我没记错的话,你必须这样做,可能在 O 中崩溃,如果有帮助,请运行上面的代码
    • 我找不到 NotificationCompat.Builder(Context, int) 方法。我一定是缺少一些支持库,但我找不到它。
    • 使用 appcompat 26.1.0
    • 这非常适合奥利奥。如果有人需要应用程序与 Oreo 和旧版本兼容,唯一要做的就是在 Oreo 中使用此代码,如果应用程序处于旧版本,则使用 NotificationCompat.Builder 的旧构造函数工作,接受单个参数的那个,上下文。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2015-10-11
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多