安卓通知
通知是程序在後臺運行時候顯示在系統欄的內容,可以使用在Service,Activity,content provider中
基本使用
(1)構建NotificationManager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
getSystemService()方法是Activity裏面的方法,非Activity使用就需要傳遞一下上下文了
(2)構建通知
Notification notification = new NotificationCompat.Builder(this, id)
.setContentTitle("this is title")
.setContentText("this is text")
.setSmallIcon(R.mipmap.ic_launcher_round)
.build();
setContentTitle() setContentText() setSmallIcon()這三個是一個通知必備的,分別是標題正文以及通知在頂部冒出來的圖標,通知中跟圖片有關的傳入的都是一個mipmap,圖片不是的時候需要轉換
BitmapFactory.decodeResource(getResources(),R.drawable.hh)
(3)解析 NotificationCompat.Builder
這個方法之前只有一個參數,後來被棄用了,但是網上好多人博客一大抄,還沒更新。這個方法現在兩個參數,第一個是context,第二個是channelId,如果channelId沒構建一定運行不起來
那麼構建channelId呢?
String id ="channel_1";//channel的id
String description = "123";//channel的描述信息
int importance = NotificationManager.IMPORTANCE_LOW;//channel的重要性
NotificationChannel channel = new NotificationChannel(id, "123", importance);//生成channel
notificationManager.createNotificationChannel(channel);
這些完成後講id傳入就可以了,直接傳遞”channel_1“也可以
(4)最後就是發通知過程
notificationManager.notify(1, notification); 第一個參數是通知的id,是唯一的,當想要點擊後有通知消失的效果一種實現方式就是將這個id捉到,然後notificationManager.cancel(1);
(5)通知的點擊效果
上面的實現完通知是不能點擊的,我們要點擊效果需要構建一個PendingIntent,他和Intent有相似的地方,也可以用來啓動活動,啓動服務,發送廣播等,相當於一個延緩版本的Intent。
Intent intent=new Intent(MainActivity.this,NewActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
四個參數,第一個context,第二個一般用不到傳入0,第三個是點擊通知後的行爲,可以是一個構建的Intent,第四個有幾個可選值,一般傳入0就可以
然後在build()之前傳入.setContentIntent(pendingIntent);
(6)取消通知,可以在build()前面採用.setAutoCancel(true),還有一種方法就是
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(1);
Demo
新建一個Activity,在佈局文件中設置一個按鈕,用來控制發送通知。同時創建一個NewActivity,用來支持跳轉
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.send_message);
button.setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_message:
//channnelId的構建
String id ="channel_1";//channel的id
String description = "123";//channel的描述信息
int importance = NotificationManager.IMPORTANCE_LOW;//channel的重要性
NotificationChannel channel = new NotificationChannel(id, "123", importance);//生成channel
//構建PendingIntent
Intent intent=new Intent(MainActivity.this,NewActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, "channel_1")
.setContentTitle("this is title")
.setContentText("this is text")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.hh))
.setWhen(System.currentTimeMillis())//通知發生的時間點 馬上
.setContentIntent(pendingIntent)//支持通知欄點擊操作,並進行相應動作
.setAutoCancel(true)//可以在點擊後自動消失
//.setSound(Uri.fromFile(new File("/System/media/audio/ringtones")))//播放通知聲音
//.setDefaults(NotificationCompat.DEFAULT_ALL)//將所有相關效果都設置爲默認的
//.setVibrate(new long[]{0,1000,1000,1000})//震動
//.setLights(Color.GREEN,1000,1000)//燈閃爍時間,我這個使用不報錯,但是也沒看到效果,還有聲音和震動都沒看到
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.hh)))//大圖風格
.setStyle(new NotificationCompat.BigTextStyle().bigText("人生若只如初見,何事秋風悲畫扇,等閒變卻故人心,卻道故人心易變,驪山風雨清宵半,淚雨霖鈴終不怨,何如薄倖錦衣郎,比翼連枝當日願"))
//.setPriority(NotificationCompat.PRIORITY_MAX)
.build();
notificationManager.notify(1, notification);
break;
default:
break;
}
}
}