Notification應(yīng)用

最基本的通知

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
builder.setContentTitle("notification")
       .setContentText("this is content")
       .setSmallIcon(R.mipmap.ic_launcher);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID,buidler.build());

說明

  • 通常會選擇用NotificationCompat來構(gòu)建一個通知,以保證兼容性。
  • 一個通知必須為其設(shè)置上面的三個屬性。但是我們自己通過ReomteViews來自定義通知布局時,可以不用設(shè)置前兩個屬性,但是必須setSmallIcon。
  • NotificationManager有兩個nofify()方法.notify(int id, Notification notification) ,notify(String tag, int id, Notification notification),其中tag和id用來唯一標(biāo)識一個通知。

一般混合通知

下面這個通知是一個長文本通知。包括兩個按鈕,并且可以響應(yīng)用戶點擊事件。內(nèi)容比較雜。

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
Intent sendMessage = new Intent(Intent.ACTION_VIEW,Uri.parse("sms:10086"));
PendingIntent smpd = PendingIntent.getActivity(mContext,0,sendMessage,PendingIntent.FLAG_UPDATE_CURRENT);
// 為通知設(shè)置長文本樣式
android.support.v4.app.NotificationCompat.BigTextStyle style = new android.support.v4.app.NotificationCompat.BigTextStyle();
style.bigText(getResources().getString(R.string.note_text));
style.setSummaryText("English Text");
builder.setContentTitle("notification")
       .setContentText("this is content")
       .setSmallIcon(R.drawable.github)
       .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.github))
       .setTicker("hello")
       .setDefaults(Notification.DEFAULT_ALL)
       .setSound(Uri.parse("android:resource://" + context.getPackageName() + "/" + R.raw.msg))
       .setAutoCancel(true)
       .setContentIntent(pendingIntent)
       .setPriority(Notification.PRIORITY_MAX)
       .setStyle(style)
       .addAction(R.drawable.accept,"send",smpd)
       .addAction(R.drawable.decline,"cancel",null);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID,buidler.build());

說明

  • 通過PendingIntent來包裝Intent,然后調(diào)用builder的setContentIntent方法把PendingIntent傳進去,響應(yīng)用戶的點擊事件。
  • 通過setStyle來設(shè)置通知的樣式。例如可以設(shè)置BigTextStyle,還可以設(shè)值MediaStyle。
  • 通過addAction方法來給通知添加按鈕,并設(shè)值響應(yīng)事件。

帶進度條的通知

final NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
builder.setContentTitle("Downloading")
       .setSmallIcon(R.mipmap.ic_launcher);
new Thread(new Runnable() {
    @Override
    public void run() {
      for (int i =0;i<100;i+=10){
      // 第三個參數(shù)如果設(shè)置為true,則不會顯示精確的進度。
      builder.setProgress(100,i,false);
      manager.notify(NOTIFICATION_ID,builder.build());
      try {
            // 模擬下載
            Thread.sleep(3000);
          } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
  builder.setContentText("Complete")
      // 取消進度條
       .setProgress(0,0,false);
  manager.notify(NOTIFICATION_ID,builder.build());
  }
}).start();

自定義布局通知

自定義布局同時主要是通過ReomteViews來實現(xiàn).

NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext);  
// 為RemotViews設(shè)置一個布局,這個布局需要自己在layout中實現(xiàn)。
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
remoteViews.setTextViewText(R.id.text,"notification");
remoteViews.setImageViewResource(R.id.image,R.drawable.git);
builder.setSmallIcon(R.drawable.github);  
// 將RemoteViews設(shè)置到通知中
builder.setContent(remoteViews);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID,builder.build());

補充

  • RemoteViews是一個可以運行在其他進程中的View。通知是運行在系統(tǒng)進程的。
  • 如果需要為RemoteViews中的子View設(shè)置響應(yīng)事件,可以通過remoteViews.setOnClickPendingIntent方法實現(xiàn)。
  • 上面通知的RemoteViews中有個TextView,如果要為它設(shè)置樣式,可以在xml文件里添加一句android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Title",這樣它的樣式就和系統(tǒng)一致了。

懸掛式通知

通知有五種優(yōu)先級,范圍從 PRIORITY_MIN (-2) 到 PRIORITY_MAX (2),如果未設(shè)置,則優(yōu)先級默認為 PRIORITY_DEFAULT (0)。
通過NotificationCompat.Builder.setPriority()來為通知設(shè)置優(yōu)先級。當(dāng)優(yōu)先級大于默認的時候時,通知都可以在其他應(yīng)用的上面,顯示一個頂部懸掛的通知。

通知的等級

通知的等級有三個

  • VISIBILITY_PRIVATE, 表明當(dāng)前通知只有在沒有鎖屏的時候才會顯示。
  • VISIBILITY_PUBLIC, 任何情況下都可以顯示。
  • VISIBILITY_SECRET, 在沒有鎖屏的情況下才會顯示。

通過NotificationCompat.Builder.setVisibility(VISIBILITY_PUBLIC),可以讓通知在鎖屏界面上顯示。
通過NotificationCompat.Builder.setCategory(Notification.CATEGORY_MESSAGE),可以控制通知的位置。

最后編輯于
?著作權(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)容

  • 原文出處: http://www.androidchina.net/6174.html Notification在...
    木木00閱讀 12,530評論 3 32
  • Notification可以讓我們在獲得消息的時候,在狀態(tài)欄,鎖屏界面來顯示相應(yīng)的信息,很難想象如果沒有Notif...
    劉望舒閱讀 2,370評論 4 16
  • 一、什么是Notification? Notification是一種有全局效果的通知,可以顯示在系統(tǒng)通知欄。以下內(nèi)...
    douhao1333閱讀 769評論 0 1
  • 最近時不時地有人問我這樣或那樣的通知如何實現(xiàn),所以本文將根據(jù)個人經(jīng)驗對Notification做個總結(jié),以供參考!...
    ConnorLin閱讀 36,240評論 27 98
  • 春節(jié)怎么陪孩子玩?寒假大面積的時間里陪孩子如何高效利用,除了陪著玩以外,怎么打發(fā)?我想著何不讓孩子來涂寫畫畫。 作...
    平平無奇小個子閱讀 753評論 0 0

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