前台服务能否调用不持续的通知(对于 api > 26)

Can a foreground service call a not ongoing notification (for api > 26)

即使我为那些 api 使用频道,我也有一个前台服务,它似乎无法在 api > 26 (oreo) 上发送不持续的通知。 在下面的 api 上,通知显示正确。 下面是我发送通知的代码:

    private void notification(String content) {
    Random random = new Random();
    int id = random.nextInt(1000)+1;

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
        NotificationChannel chan = new NotificationChannel(STANDARD_NOTIFICATION_CHANNEL_ID, standardChannelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, STANDARD_NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(false)
                .setSmallIcon(android.R.drawable.btn_dropdown)
                .setContentTitle(content)
                .setPriority(NotificationManager.IMPORTANCE_DEFAULT)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        // TODO we need to see how to send not ongoing notifications in our case with API > 26
        manager.notify(id,notification);
    }
    else {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, STANDARD_NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(false)
                .setSmallIcon(R.drawable.ic_launcher_fury)
                .setContentTitle(content)
                .build();
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(id,notification);
    }
}

唯一正确显示的通知是向用户显示服务在前台的持续通知。我这样称呼它:

    @Override
public void onCreate() {
    super.onCreate();

    Random random = new Random();
    int id = random.nextInt(1000);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startMyOwnForeground();
    }
    else {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, FOREGROUND_NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.drawable.ic_launcher_fury)
                .setContentTitle(getResources().getString(R.string.app_is_in_background))
                .build();
        startForeground(id, notification);
    }
}

@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground()
{
    Random random = new Random();
    int id = random.nextInt(1000);

    NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, FOREGROUND_NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(android.R.drawable.btn_dropdown)
            .setContentTitle(getResources().getString(R.string.app_is_in_background))
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(id, notification);
}

你有什么线索吗?

我猜你的问题出在这里:

NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, NotificationManager.IMPORTANCE_NONE);

根据 NotificationManager.IMPORTANCE_NONE

的 JavaDoc
/**
 * A notification with no importance: does not show in the shade.
 */

因此,请尝试使用不同的重要性级别,例如:NotificationManager.IMPORTANCE_DEFAULT

NotificationChannel chan = new NotificationChannel(FOREGROUND_NOTIFICATION_CHANNEL_ID, foregroundChannelName, NotificationManager.IMPORTANCE_DEFAULT);