日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

生活经验

Notification 使用详解

發(fā)布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Notification 使用详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

錄制了一個gif 圖大家看看效果

由于手機廠商修改問題,這個顯示可能存在差役,但是這個提示框都是會顯示的,運行是在android 7 8 ,9 三個版本運行的都沒有問題 下面開始介紹它的使用

Notification? 簡單的總結:通知會在不使用應用程序時提供有關事件的簡短及時信息?

1 添加支持庫 這個一般創(chuàng)建工程的時候都會自帶的有,可以忽略這一步

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

2 創(chuàng)建一個基本的通知

?需要提前了解的內容

1 setSmallIcon(R.mipmap.ic_launcher) //小圖標 ,可能部分手機不顯示比如小米手機,但是小米手機把小圖片改為內容一起的圖片了

2?.setContentTitle() //標題

3?setContentText()//內容

4?setPriority()// 通知的優(yōu)先級

5?setWhen() //設置時間,默認顯示

具體的可以看下圖 ,有些手機可能做了修改,顯示的效果大致類似,不怎么全部一樣

?

?開始的寫代碼

1?

//創(chuàng)建通知欄管理工具
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2

考慮到android 8.0的原因,我們需要判斷下,8.0 我們需要添加渠道?

   //如果是8.0 創(chuàng)建一個渠道
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 創(chuàng)建內容

 Notification mBuilder = new NotificationCompat.Builder(MainActivity.this, PUSH_CHANNEL_ID).setSmallIcon(R.mipmap.ic_launcher) //小圖片 ,有些手機可能不顯示,.setContentTitle("精選內容") //標題.setContentText("點擊查看更多詳細的內容呢") //內容.setPriority(NotificationCompat.PRIORITY_DEFAULT)  //通知優(yōu)先級,PRIORITY_DEFAULT為默認寫與不寫一樣.setContentIntent(pendingIntent).build();//通知顯示出來notificationManager.notify(PUSH_NOTIFICATION_ID, mBuilder);
setContentIntent 這個屬性跳轉的時候 用到的,跳轉呢,點擊通知跳轉呢使用的 PendingIntent

全部代碼如下

  //創(chuàng)建通知欄管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//如果是8.0 創(chuàng)建一個渠道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)  //通知優(yōu)先級,PRIORITY_DEFAULT為默認寫與不寫一樣.setContentIntent(pendingIntent).build();//通知顯示出來notificationManager.notify(PUSH_NOTIFICATION_ID, mBuilder);

這是一個基本的 顯示 ,

?

可折疊的通知代碼

 //創(chuàng)建通知欄管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//如果是8.0 創(chuàng)建一個渠道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);

加載大圖片

 //創(chuàng)建通知欄管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//如果是8.0 創(chuàng)建一個渠道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);

自定義布局

   //創(chuàng)建通知欄管理工具NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//如果是8.0 創(chuàng)建一個渠道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 使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。