Android Oreo中的通知被延迟

时间:2017-12-19 06:49:40

标签: android notifications

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

1 个答案:

答案 0 :(得分:1)

这种情况会发生,但对于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);
    }

这个完美点击