1. 程式人生 > >Notification 用法示例

Notification 用法示例

1.1  普通通知
1.2  帶進度條的通知    Android 4.1.2   

1.3  擴展布局      

              適配
              (1) 4.1 api 16 開始支援擴展布局,最頂部的通知自動展開
              (2) 7.0 api 24 每條通知都可以單獨展開7.0 
          (3) 8.0 api 26 多條通知可以合併顯示在一個裡面( notificationChannelID 一樣)


          操作按鈕
          (1) api19 4.4 開機支援新增操作按鈕,每個展開的通知的最多支援3個操作按(通知下方右下角的)
              builder.addAction(icon1,title1,pendingIntent1);
                  builder.addAction(icon2,title2,pendingIntent2);

          樣式分類

          (1)多行文字通知   
          .setStyle(new Notification.BigTextStyle().bigText(""))

          (2)大圖通知
          .setStyle(new Notification.BigPictureStyle().bigPicture(bitmap))

          (3)收件箱通知(最多顯示5行訊息)
          .setStyle(new Notification.InboxStyle());

              (4)MediaStyle播放介面通知
          關聯音訊播放服務的  api21, 點選和清空狀態列不會消失


1.4  浮動通知(headup 類似來電話)
              android 5.0 api 21
          通知具有較高優先順序  Priority_MAX 或 HEIGHT

1.5  鎖屏通知 
              android 5.0 api 21
          VISIBILITY_PUBLIC  顯示通知的完整內容
          VISIBILITY_SECRET  不會在鎖定螢幕上顯示此通知的任何部分
              VISIBILITY_PRIVATE 顯示通知圖示和標題,但是隱藏完整內容

1.6  自定義通知 romateViews (很早的版本就支援了,類似音樂播放器的)

         
Notification.FLAG_SHOW_LIGHTS --  builder.setLightes   builder.setDefaults 

Notification.FLAG_ONLY_ALERT_ONCE (提醒鈴聲震動滾動等只執行一次)  builder.setOnlyAlertOnce(true);

Notification.FLAG_ONGOING_EVENT (正在進行的通知,使用者不能取消,類似FLAG_NO_CLEAR)   builder.setOngoing(true);

Notification.FLAG_INSISTENT (持續提醒聲音或震動,直到使用者取消)   n.flag |= Notification.FLAG_INSISTENT

Notification_FLAG_NO_CLEAR  使用者無法取消

Notification_FLAG_FOREGROUND_SERVICE  正在執行的服務


Notifcation.PRIORITY_MAX 重要緊急的通知,

Notification.PRIORITY_HIGH  高優先順序

Notification.PRIORITY_DEFAULT  預設

Notificaiton.PRIORITY_LOW   低優先順序,通知使用者但又不是很緊急

Notification.PRIORITY_NIN   使用者後臺訊息,只有用下拉通知抽屜才能看到內容


Notification.DEFAULT_SOUND  新增預設聲音提醒


Notification.DEFAULT_VIBRATE 新增預設震動提醒

Notification.DEFAULT_LIGHTS  新增預設呼吸燈

Notification.DEFAULT_ALL  同時新增以上三種預設提醒


三類intent:
setContentIntent(PendingIntent contentIntent)
setDeleteIntent(PendingIntent deleteIntent)
setFullScreenIntent(PendingIntent fullscreenIntent)

 


builder.setTicker(CharSequence ticker)
builder.setContentTitle()
builder.setContentText()


//設定預設notification title name
Bundle a = new Bundle();
a.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, newAppName)
builder.addExtras(a);


//大小圖示
builder.setSamllIcon(int smallIcon)
builder.setLargeIcon(largeIcon)

//設定按鈕事件
setAction(position,string,pendingintent)


//設定時間
builder.setWhen(long date)
//設定是否顯示時間
builder.setShowWhen(long data)
//
builder.setSubText()
builer.setProgress(max,progress,indeteminate)

//新增預設聲音提醒
builder.setDefaults(Notification.DEFAULT_SOUND);
//新增預設呼吸燈提醒
builder.setDefaults(Notification.DEFAULT_LIGHTS);
//
builder.setSound(Uri.parse(""));

builder.setVibrate(new long[]{200,200,200,200});

builder.setLights(oxffffoooo(color), 300(on) ,100(off))

//設定pendingintent
builder.setContentIntent(pendingintent)
//取消
builder.setDeleteIntent(pendingintent)
//全屏(來電)
builder.setFullScreenIntent(pendingtent,true);

builder.setAutoCancel(true);
//設定為前臺服務的notification
builder.setForgroundService() 


builder.setStyle(new Notification.BigTextStyle().bigText(""))
builder.setStyle(new Notification.MessagingStyle("self name"))


builder.setVisibility(VISIBILITY_PUBLIC / VISIBILITY_SECRET /  VISIBILITY_PRIVATE );

builder.setLocalOnly();
builder.extend(new Notification.TvExtender());

notificationManager.notifyAsUser
notificationManager.notify

//remoteViews

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notif_custom_view);
remoteViews.setImageViewResource(R.id.image_icon, iconResource);
remoteViews.setTextViewText(R.id.text_title, title);
remoteViews.setTextViewText(R.id.text_message, message);
remoteViews.setImageViewResource(R.id.image_end, imageResource);

//--- 7.0
                 
//設定字型顏色
builder.setColor(ContextCompat.getColor());

//設定通知組
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    // Other properties
    .setGroupSummary(true)
    .setGroup(KEY_NOTIFICATION_GROUP);

//新增直接回復
//-------------------簡訊會用到的場景------------------------------------
RemoteInput remoteInput = new RemoteInput.Builder("KEY_TEXT_REPLY_ID")
    .setLabel("LABEL_REPLY")
    .build();

Intent replyIntent = new Intent(context,xxxActivity.class);
replyIntent.put(number.value);
replyIntent.put(msg,value);
replyIntent.put(key,value);

PendingIntent replyPendIntent = PendingIntent.getActivity(context,
        requestCode,
        getDirectReplyIntent(context, LABEL_REPLY),
        PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Action replyAction =
        new NotificationCompat.Action.Builder(R.drawable.ic_reply,
                 LABEL_REPLY//傳送按鈕, 
         replyIntent)
                .addRemoteInput(remoteInput)
                .build();
builder.addAction(replyAction);

oncreate中處理
  oncreate(){
    Intent in = getIntent();
    //處理的地方
    Bundle bundle = RemoteInput.getResultsFromIntent(intent);
    if (remoteInput != null) {
        return bundle.getCharSequence(NotificationUtil.KEY_TEXT_REPLY);
    }
  }
-------------------------------------------------------------------------------

https://blog.csdn.net/hss01248/article/details/55096553
https://github.com/hss01248/NotifyUtil
詳細分類: https://gold.xitu.io/post/5863264861ff4b0068b1817d 
7.0新特性:http://www.jianshu.com/p/33e84d5cb43f 
每種型別都有很全的示例程式碼:http://blog.csdn.net/w804518214/article/details/51231946