【问题标题】:not able to use setChannelId in android 5.0 and above无法在 android 5.0 及更高版本中使用 setChannelId
【发布时间】:2018-10-04 11:43:30
【问题描述】:

我正在尝试设置一个 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();

我得到的错误是

无法解析方法“setChannelId”

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

调用需要 API 级别 26(当前最低为 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 的代码

【问题讨论】:

  • 频道 ID 不是必须的来指定 API 低于 26。所以,不必担心。只需遵循相同的方法,它就会正常工作。参考NotificationChannel的文档。

标签: android push-notification android-notifications


【解决方案1】:

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

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());
}

【讨论】:

    【解决方案2】:

    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 类来支持旧版本,而无需自己检查版本。

    NotificationCompat.Builder(context, SHEALTH_NOTIFICATION_CHANNEL_ID)
    

    【讨论】:

    • 不存在android.support.v7.app.Notification.Builder应该是android.support.v7.app.NotificationCompact.Builder
    【解决方案3】:

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

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

    此兼容类将为奥利奥及以上设备添加通知通道,而对于牛轧糖及以下设备将忽略它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 1970-01-01
      • 2023-01-17
      相关资源
      最近更新 更多