Android Oreo中的通知被延迟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Oreo中的通知被延迟相关的知识,希望对你有一定的参考价值。

我正在使用一个在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);
}
答案

这种情况会发生,但对于Oreo,您不需要提供频道和群组

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);
    }

这个完美打击

以上是关于Android Oreo中的通知被延迟的主要内容,如果未能解决你的问题,请参考以下文章

Android oreo - 推送通知崩溃

混合应用推送通知图标未在 Android 8 (Oreo) 上显示

即使应用程序关闭也运行作业(Android Oreo)

自定义通知声音在 Android Oreo 中不起作用

离子推送通知自定义声音无法在 Android Oreo 及更高版本上播放

在android oreo中设备重启后PeriodicWorkRequest不起作用