Notification

Notification的作用

1.顯示接收到短消息、即使消息等信息 (如QQ、微信、新浪、短信)
2.顯示客戶端的推送消息(如有新版本發布,廣告,推薦新聞等)
3.顯示正在進行的事物(例如:后臺運行的程序)(如音樂播放器、版本更新時候的下載進度等)

Notification的Flag屬性

NotificationCompat.Builder:setDefaults()
Notification.DEFAULT_VIBRATE           //添加默認震動提醒  需要 VIBRATE permission
Notification.DEFAULT_SOUND             //添加默認聲音提醒
Notification.DEFAULT_LIGHTS            // 添加默認三色燈提醒
Notification.DEFAULT_ALL               // 添加默認以上3種全部提醒
Notification.FLAG_SHOW_LIGHTS          //三色燈提醒,在使用三色燈提醒時候必須加該標志符
Notification.FLAG_ONGOING_EVENT        //發起正在運行事件(活動中)
Notification.FLAG_INSISTENT            //讓聲音、振動無限循環,直到用戶響應(取消或者打開)
Notification.FLAG_ONLY_ALERT_ONCE      //發起Notification后,鈴聲和震動均只執行一次
Notification.FLAG_AUTO_CANCEL          //用戶單擊通知后自動消失
Notification.FLAG_NO_CLEAR             //只有全部清除時,Notification才會清除 ,不清除該通知 
Notification.FLAG_FOREGROUND_SERVICE   //表示正在運行的服務
setContentIntent:在通知窗口區域,Notification被單擊時的響應事件由該intent觸發;
setDeleteIntent:當用戶點擊全部清除按鈕時,響應該清除事件的Intent;
setFullScreenIntent:響應緊急狀態的全屏事件(例如來電事件),也就是說通知來的時候,跳過在通知區域點擊通知這一步,直接執行fullScreenIntent代表的事件。

普通通知

NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)                    //必須
           .setContentTitle("通知的標題")                        //必須
           .setContentText("通知的內容")                         //必須
           .setDefaults(                                       //設置通知方式
Notification.DEFAULT_SOUND);                                   //聲音
manager.notify(1,builder.build()) ;                            //發送通知

大視圖通知

NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)                   //必須
           .setContentTitle("通知的標題")                       //必須
           .setContentText("通知的內容")                        //必須
           .setDefaults(                                      //設置通知方式
Notification.DEFAULT_SOUND);                                  //聲音
//大圖片樣式
NotificationCompat.BigPictureStyle style = new BigPictureStyle() ;
style.setBigContentTitle("大視圖標題")
        .setSummaryText("大視圖文本");
style.bigPicture( BitmapFactory.decodeResource( getResources(), R.drawable.qq));
builder.setStyle(style) ;
manager.notify(2,builder.build());

進度通知

final NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
           .setContentText("下載")
           .setContentText("下載中...");

//開始模擬進度條
new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i = 0 ; i <=100 ; i++) {
               //第一個參數:進度指示器設置為最大值 
               //第二個參數:當前完成百分比 
               //第三個參數:活動指示器啟用 如果true 那么會是一個進度展示
               builder.setProgress(100,i,true);
               Thread.sleep(100);
               manager.notify(3,builder.build());
         }
         manager.cancel(3);
      }
}).start();

自定義通知

NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher)
           .setContentTitle("title")
           .setContentText("text")
          .setDefaults( Notification.DEFAULT_ALL ) ;
RemoteViews views = new RemoteViews(getPackageName(), R.layout.activity_notification ;//layoutId :自己布局文件
builder.setContent(views);
manager.notify(4,builder.build());

通過通知跳轉頁面

Intent intent = new Intent( this, NotificationActivity.class );
//封裝延遲意圖(點擊通知要進入的下一個頁面)
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_ONE_SHOT );
builder.setContentIntent( pendingIntent );       //通過構造者發送延時意圖
Notification notification = builder.build( );    //通過構造者建立一個通知對象
manager.notify( 1, notification );              //發送通知
//在跳轉的頁面
private NotificationManager manager;            //創建一個通知管理者對象
//在onCreate方法里創建出來
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(1);                             //id:就是當初發送通知的定義的id

PendingIntent的用法

PendingIntent和Intent略有不同,它可以設置執行次數,主要用于遠程服務通信、鬧鈴、通知、啟動器、短信中,在一般情況下用的比較少。
Notification支持多種Intent來響應單擊事件、消除事件、處理緊急狀態的全屏事件等。
這里就用到了setContentIntent(PendingIntent intent)來處理以上這么多的事件。

PendingIntent屬性:
PendingIntent的位標識符:
FLAG_ONE_SHOT                        表示返回的PendingIntent僅能執行一次,執行完后自動取消
FLAG_NO_CREATE                       表示如果描述的PendingIntent不存在,并不創建相應的PendingIntent,而是返回NULL
FLAG_CANCEL_CURRENT                  表示相應的PendingIntent已經存在,則取消前者,然后創建新的PendingIntent,這個有利于數據保持為最新的,可以用于即時通信的通信場景
FLAG_UPDATE_CURRENT                  表示更新的PendingIntent
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容