安卓8.0以上版本Notification使用

安卓8.0以上版本Notification发生了很大变化。
今天在学习的时候,按照《第一行代码》中写的一样,但尝试了好几遍,都无法成功,在手机调试了没有反应。接下来,我跟大家分享一下我查找了资料以及修改了代码之后的效果。
1,
首先,是NotificationCompat.Builder的问题。升级版本后,该方法被修改为:
NotificationCompat.Builder(Context context, String channelId)
即需要传递两个参数。
2,
安卓8.0以上版本,创建通知:
import android.os.Build;

String CHANNEL_ID = “my_channel_01”;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { CharSequence name = “my_channel_name”; String description = “my_channel_description”; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); channel.setDescription(description); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); Notification notification = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID)
.setContentTitle(“Chat”) .setContentText(“发送失败!”) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .build(); notificationManager.notify(1,notification);
在这里插入图片描述

==我也是刚学安卓,遇到问题就把解决方法和大家分享,一起进步。有做的不好的,请大家见谅。

参考资料:
https://blog.csdn.net/csdnxufei/article/details/87648856

猜你喜欢

转载自blog.csdn.net/s_t_r_u_g_g_l_e_/article/details/106044662