无法在 android 5.0 及更高版本中使用 setChannelId

not able to use setChannelId in android 5.0 and above

我正在尝试设置一个 setChannelId 并在通知本身中添加一个操作来停止同步数据

以下是我的代码

        return new android.support.v4.app.NotificationCompat.Builder(context)
                .addAction(R.drawable.icon, "STOP SYNC", stopServiceIntent)
                .setChannelId(SHEALTH_NOTIFICATION_CHANNEL_ID)
                .setContentTitle(r.getString(R.string.shealth_notification_title))
                .setContentText(r.getString(R.string.shealth_notification_text))
                .setSmallIcon(R.mipmap.ic_launcher)
                .build();

我得到的错误是

cannot resolve method "setChannelId"

如果我更改代码仅使用 Notification 而不是 NotificationCompat,那么我会收到以下错误

Call requires API Level 26 ( current is min 21)

 return new android.support.v7.app.Notification.Builder(context)
                    .addAction(R.drawable.icon, "STOP SYNC", stopServiceIntent)
                    .setChannelId(SHEALTH_NOTIFICATION_CHANNEL_ID)
                    .setContentTitle(r.getString(R.string.shealth_notification_title))
                    .setContentText(r.getString(R.string.shealth_notification_text))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();

但我想让我的应用在 Android 5.0 及更高版本上 运行 因此我无法将 min sdk 设置为 API 级别 26

以下是我在build.gradle

中对sdk相关的设置
  1. minSdkVersion 21
  2. targetSdkVersion 28

我不会展示我如何能够编写支持 5.0 及更高版本的带有频道 ID 的代码

试试这个,对我来说效果很好。

public void showNotification() {
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationId = 1;
    String channelId = "channel-01";
    String channelName = "Channel Name";
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(channelId, channelName, importance);
        assert notificationManager != null;
        notificationManager.createNotificationChannel(mChannel);
    }
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Test Notofication")
            .setContentText("Testing Okay");

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    Intent intent = new Intent(this, MainActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    assert notificationManager != null;
    notificationManager.notify(notificationId, mBuilder.build());
}

如果您支持旧的 Android 版本,您应该使用 NotificationCompat class 来构建通知:

return new NotificationCompat.Builder(context, SHEALTH_NOTIFICATION_CHANNEL_ID)
    // set every other field

此兼容 class 将为 Oreo 及以上设备添加通知通道,并为 Nougat 及以下设备忽略它。

26 API ( Android O ) 以下的版本无需设置 ChannelId。如果你想支持这两种情况,你可以在运行时检查你的版本。

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    return new android.support.v7.app.Notification.Builder(context)
                            .addAction(R.drawable.icon, "STOP SYNC", stopServiceIntent)
                            .setChannelId(SHEALTH_NOTIFICATION_CHANNEL_ID)
                            .setContentTitle(r.getString(R.string.shealth_notification_title))
                            .setContentText(r.getString(R.string.shealth_notification_text))
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .build();
                }
                else {
                    return new android.support.v7.app.Notification.Builder(context)
                            .addAction(R.drawable.icon, "STOP SYNC", stopServiceIntent)
                            .setContentTitle(r.getString(R.string.shealth_notification_title))
                            .setContentText(r.getString(R.string.shealth_notification_text))
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .build();
                }

或者您可以使用支持库中的 NotificationCompat class 来支持旧版本,而无需自己检查版本。

NotificationCompat.Builder(context, SHEALTH_NOTIFICATION_CHANNEL_ID)