Red*_*d M 25 android android-notifications android-8.0-oreo
Notification.Builder(context)最近已被弃用,其中包含Android O中的通知频道.
问题:
使用后Notification.Builder(context, StringID)代替Notification.Builder(context)我确实收到了我的Android O设备的通知.
但是,在Android 23(M)上尝试后,我没有收到通知.我调试了我的代码,一旦调试器在Android 23(M)上点击Notification.Builder(context,StringID)后面的行就停止执行.
固定:
为解决此问题,我使用if/else条件在Android O设备和其他其他设备之间进行隔离.
我有以下代码片段:
Notification.Builder notificationBuilder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationBuilder = new Notification.Builder(mContext,
mContext.getResources().getString(R.string.notification_id_channel));
} else {
notificationBuilder = new Notification.Builder(mContext);
}
Run Code Online (Sandbox Code Playgroud)
Android Studio中的Lint显示以下弃用行:
题:
有没有办法摆脱弃用警告线?
Wes*_*ess 39
您的解决方案是使用NotificationCompat.Builder(Context context, String channelId).如果使用此功能,则无需检查API级别,Builder会忽略pre-Oreo设备上的通道ID.
我已经在API 15,22,23和26上进行了测试,它运行良好.
| 归档时间: |
|
| 查看次数: |
19206 次 |
| 最近记录: |