欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 人文社科 > 生活经验 >内容正文

生活经验

Notification 使用详解

发布时间:2023/11/27 生活经验 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Notification 使用详解 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

录制了一个gif 图大家看看效果

由于手机厂商修改问题,这个显示可能存在差役,但是这个提示框都是会显示的,运行是在android 7 8 ,9 三个版本运行的都没有问题 下面开始介绍它的使用

Notification  简单的总结:通知会在不使用应用程序时提供有关事件的简短及时信息 

1 添加支持库 这个一般创建工程的时候都会自带的有,可以忽略这一步

implementation "com.android.support:support-compat:28.0.0"

2 创建一个基本的通知

 需要提前了解的内容

1 setSmallIcon(R.mipmap.ic_launcher) //小图标 ,可能部分手机不显示比如小米手机,但是小米手机把小图片改为内容一起的图片了

2 .setContentTitle() //标题

3 setContentText()//内容

4 setPriority()// 通知的优先级

5 setWhen() //设置时间,默认显示

具体的可以看下图 ,有些手机可能做了修改,显示的效果大致类似,不怎么全部一样

 

 开始的写代码

//创建通知栏管理工具
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2

考虑到android 8.0的原因,我们需要判断下,8.0 我们需要添加渠道 

   //如果是8.0 创建一个渠道
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){NotificationChannel mChannel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);notificationManager.createNotificationChannel(mChannel);
}

3 创建内容

 Notification mBuilder = new NotificationCompat.Builder(MainActivity.this, PUSH_CHANNEL_ID).setSmallIcon(R.mipmap.ic_launcher) //小图片 ,有些手机可能不显示,.setContentTitle("精选内容") //标题.setContentText("点击查看更多详细的内容呢") //内容.setPriority(NotificationCompat.PRIORITY_DEFAULT)  //通知优先级,PRIORITY_DEFAULT为默认写与不写一样.setContentIntent(pendingIntent).build();//通知显示出来notificationManager.notify(PUSH_NOTIFICATION_ID, mBuilder);
setContentIntent 这个属性跳转的时候 用到的,跳转呢,点击通知跳转呢使用的 PendingIntent

全部代码如下

  //创建通知栏管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//如果是8.0 创建一个渠道if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){NotificationChannel mChannel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);notificationManager.createNotificationChannel(mChannel);}//点击跳转Intent intent = new Intent(MainActivity.this,SecondeActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);Notification mBuilder = new NotificationCompat.Builder(MainActivity.this, PUSH_CHANNEL_ID).setSmallIcon(R.mipmap.ic_launcher) //小图片 ,有些手机可能不显示,.setContentTitle("精选内容") //标题.setContentText("点击查看更多详细的内容呢") //内容.setPriority(NotificationCompat.PRIORITY_DEFAULT)  //通知优先级,PRIORITY_DEFAULT为默认写与不写一样.setContentIntent(pendingIntent).build();//通知显示出来notificationManager.notify(PUSH_NOTIFICATION_ID, mBuilder);

这是一个基本的 显示 ,

 

可折叠的通知代码

 //创建通知栏管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//如果是8.0 创建一个渠道if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel mChannel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);notificationManager.createNotificationChannel(mChannel);}//点击跳转Intent intent = new Intent(MainActivity.this, SecondeActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);Notification mBuilder = new NotificationCompat.Builder(MainActivity.this, PUSH_CHANNEL_ID).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("精选内容").setContentText("点击查看更多详细的内容呢").setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent).setStyle(new NotificationCompat.InboxStyle().addLine("第一行内容显示").addLine("第二行内容显示").addLine("第三行内容显示").addLine("第四行内容显示").addLine("第五行内容显示")).build();notificationManager.notify(2, mBuilder);

加载大图片

 //创建通知栏管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//如果是8.0 创建一个渠道if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel mChannel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);notificationManager.createNotificationChannel(mChannel);}//点击跳转Intent intent = new Intent(MainActivity.this, SecondeActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);Notification mBuilder = new NotificationCompat.Builder(MainActivity.this, PUSH_CHANNEL_ID).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("精选内容").setContentText("点击查看更多详细的内容呢").setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent).setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.image))).build();notificationManager.notify(2, mBuilder);

自定义布局

   //创建通知栏管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//如果是8.0 创建一个渠道if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {NotificationChannel mChannel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);notificationManager.createNotificationChannel(mChannel);}//自定义布局RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_remote_layout);Notification mBuilder = new NotificationCompat.Builder(MainActivity.this, PUSH_CHANNEL_ID).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("精选内容").setContentText("点击查看更多详细的内容呢").setPriority(NotificationCompat.PRIORITY_DEFAULT).setContent(remoteViews).build();notificationManager.notify(2, mBuilder);

 

demo 地址参考 

 

总结

以上是生活随笔为你收集整理的Notification 使用详解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。