Notification的使用(一)

一、Notification的基本使用

通知的創(chuàng)建可以放在Activity、Service、BroatcastReceiver里面。

1.創(chuàng)建通知

1.1NotificationManager

首先需要創(chuàng)建一個NotificationManager對象用來管理通知。創(chuàng)建方式Context.getSystemService()方法,參數(shù)傳入Context.NOTIFICATION_SERVICE。

NotificationManagermanager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

1.2Notification

1.2.1由于Android每個版本都會對通知進行部分修改,所以為了統(tǒng)一,我們使用v4包里面的NotificationCompat類,這是一個空的通知:

Notification notification =new ?NotificationCompat.Builder(context).build();

1.2.2最基本設(shè)置的natification:

Notificationnotification=newNotificationCompat.Builder(mContext)

? ? ? ? ? ? ? ? ? ? ? ? .setContentTitle("這是通知標(biāo)題") ?//通知標(biāo)題

? ? ? ? ? ? ? ? ? ? ? ? .setContentText("這是通知內(nèi)容")//通知內(nèi)容

? ? ? ? ? ? ? ? ? ? ? ? .setWhen(System.currentTimeMillis())//通知時間,毫秒數(shù)

? ? ? ? ? ? ? ? ? ? ? ? .setAutoCancel(true)//點擊后自動取消

? ? ? ? ? ? ? ? ? ? ? ? .setSmallIcon(R.mipmap.ic_launcher)//通知的小圖標(biāo)(狀態(tài)欄上顯示,只能使用純alpha通道的圖片)

? ? ? ? ? ? ? ? ? ? ? ? setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))///通知的大圖片,通知欄上顯示

? ? ? ? ? ? ? ? ? ? ? ? .build();//通知創(chuàng)建


通知大圖標(biāo)
通知小圖標(biāo)


1.3發(fā)出通知

利用NotificationManager的notify方法發(fā)出通知,兩個參數(shù),第一個是通知的id,該條通知的唯一標(biāo)識,可以用來取消通知等等。第二個參數(shù)是Notification對象,代碼如下:

manager.notify(1,notification);

2.通知的點擊事件

2.1 獲取PendingIntent對象

要實現(xiàn)的通知的點擊效果需要使用PendingIntent類,調(diào)用靜態(tài)方法可以獲得對象。

PendingIntent? pendingIntent=PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_ONE_SHOT);//跳轉(zhuǎn)activity

PendingIntent pendingIntent=PendingIntent.getService(mContext,0,intent3,PendingIntent.FLAG_ONE_SHOT);跳轉(zhuǎn)服務(wù)

PendingIntent? pendingIntent=PendingIntent.getBroadcast(mContext,0,intent3,PendingIntent.FLAG_ONE_SHOT);//跳轉(zhuǎn)廣播


PendingIntent pendingIntent=PendingIntent.getForegroundService(mContext,0,intent3,PendingIntent.FLAG_ONE_SHOT);//跳轉(zhuǎn)前臺服務(wù)(要求API 26)

由上面可以看出雖然方法不一樣,但是方法的參數(shù)都是一樣的,第一個參數(shù)是上下文Context,第二個參數(shù)是請求碼,隨便填一個int值,第三個參數(shù)是要執(zhí)行的”意圖“Intent,第四個參數(shù)是確定PendingIntent的行為,有4個可選值,PendingIntent.FLAG_ONE_SHOT、PendingIntent.FLAG_NO_CREATE、PendingIntent.FLAG_CANCEL_CURRENT、PendingIntent.FLAG_UPDATE_CURRENT具體含義可以查看相關(guān)文檔,或者從字面理解。

2.2設(shè)置到Notification里面

通過在創(chuàng)建Notification對象的時候添加setContentIntent(),傳入PendingIntent對象


Notificationnotification2=newNotificationCompat.Builder(mContext)

? ? ? ? .setContentTitle("這是通知標(biāo)題")//通知標(biāo)題

? ? ? ? ?.setContentText("這是通知內(nèi)容")//通知內(nèi)容

? ? ? ? ?.setContentIntent(serviceIntent)//點擊后的操作

? ? ? ? ?.setWhen(System.currentTimeMillis())//通知時間,毫秒數(shù)

? ? ? ? ?.setAutoCancel(true)//點擊后自動取消

? ? ? ? ?.setSmallIcon(R.mipmap.ic_launcher)//通知的小圖標(biāo)(狀態(tài)欄上顯示,只能使用純alpha通道的圖片)

? ? ? ? ?.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))///通知的大圖片,通知欄上顯示

? ? ? ? .build();//通知創(chuàng)建

其他步驟照舊就OK了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容