Notification 使用详解
生活随笔
收集整理的這篇文章主要介紹了
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() //設置時間,默認顯示
具體的可以看下圖 ,有些手機可能做了修改,顯示的效果大致類似,不怎么全部一樣
?
?開始的寫代碼
1?
//創建通知欄管理工具 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 使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 实现 下拉筛选的效果
- 下一篇: 求一个放下的个性签名!