如何从服务中推送比Oreo更高的可清除通知?

时间:2019-04-12 08:10:37

标签: android service notifications android-notifications foreground

在Android 8.0 Oreo及更高版本中,如何从服务中发送可清除的提醒? startForeground(id,通知)只是一个进行中的通知。我使用它的方式可以与较旧的API一起使用,但是一旦我开始使用Oreo,它就会消失。\  + NotificationChannel,NotificationManager都是正确的。

        NotificationCompat.Builder builder, builder2;

    NotificationManager nm;
    nm = ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE));

    if (Build.VERSION.SDK_INT >= 26) {
        String CHANNEL_ID = "snwodeer_service_channel";
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "SnowDeer Service Channel",
                NotificationManager.IMPORTANCE_DEFAULT);


        nm.createNotificationChannel(channel);

        builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        builder2 = new NotificationCompat.Builder(this, CHANNEL_ID);
    } else {
        builder = new NotificationCompat.Builder(this);
        builder2 = new NotificationCompat.Builder(this);
    }
    builder.setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("제목")
            .setContentText("내용")
            .setContentIntent(pendingIntent);

    nm.notify(1, builder.build());

0 个答案:

没有答案