通知并不总是显示弹出窗口

时间:2019-08-22 16:02:23

标签: android notifications foreground-service

我正在使用带有通知的前台服务。重要的是我的通知会自动弹出,而不仅仅是在您向下拖动顶部栏时弹出。 大部分时间都在工作。小米手机除外。奇怪的是,它也停止使用Pixel 2 API 29。 我知道如果没有声音或振动,它们不会出现在较旧的API上。也许与此有关。

StatusNotification statusNotification = new StatusNotification(getApplicationContext());
startForeground(1, statusNotification.notification());

...

public class StatusNotification {
    Context context;
    long[] shortVibration;

    StatusNotification(Context context){
        this.context = context;
        shortVibration = new long[] {0L, 100L, 0L};

// just {100L} doesn't vibrate at all
// but it has to be one short vibration and not the default one
// VibrationEffect.createOneShot() is only for Oreo and higher

        createNotificationChannel();
    }

    void createNotificationChannel(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager manager = context.getSystemService(NotificationManager.class);

            NotificationChannel channel2 = new NotificationChannel("channel2", "Channel 2", NotificationManager.IMPORTANCE_HIGH);
            channel2.setDescription("Channel 2");
            channel2.enableVibration(true);
            channel2.setSound(null, null);
            channel2.setVibrationPattern(shortVibration);
            manager.createNotificationChannel(channel2);
        }
    }

    Notification notification(){
        return new NotificationCompat.Builder(context, "channel2")
                .setProgress(100,100, false)
                .setContentTitle("Title")
                .setColor(context.getResources().getColor(R.color.colorPrimary))
                .setSmallIcon(R.drawable.ic)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setVibrate(shortVibration)
                .build();
    }
}

0 个答案:

没有答案