【发布时间】: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 相关的设置
- minSdkVersion 21
- targetSdkVersion 28
我没有说明如何编写支持 5.0 及更高版本的具有通道 ID 的代码
【问题讨论】:
-
频道 ID 不是必须的来指定 API 低于 26。所以,不必担心。只需遵循相同的方法,它就会正常工作。参考NotificationChannel的文档。
标签: android push-notification android-notifications