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
 

猜你喜欢

转载自blog.csdn.net/kongbaidepao/article/details/84100151
今日推荐