开发者社区> 问答> 正文

SDK API都有什么?(4)



3. 自定义样式通知(V2.3.3及以上版本开始支持)


Android Push SDK支持用户自定义通知样式,用户可以设定自己的通知样式,涉及的内容包括通知的提醒方式(声音、震动、静默),通知在状态栏的显示图标,推送消息应用内到达时是否创建通知以及自定义通知布局文件等。自定义样式通知的设置包括两部分:

3.1 客户端设置通知样式

  • 用户利用SDK提供的自定义通知样式接口创建自定义样式通知。SDK中有两个自定义样式通知类:1)BasicCustomPushNotification;2)AdvancedCustomPushNotification。其中BasicCustomPushNotification用户设置基础样式,包括提醒方式、状态栏图标以及当推送消息到达时应用正处于前台情况下是否创建该通知等。AdvancedCustomPushNotification是BasicCustomPushNotification的子类,继承了BasicCustomPushNotification的所有方法,同时还可以设置通知样式布局文件
  • 每个样式都需要对应一个特定的整数类型id,如果多个样式设置为同一个id,则最后设置的样式有效。如果SDK没有找到对应id的样式则会创建默认样式的通知
  • 样式只需设置一次,SDK会记住这个设置,在需要使用时加载对应样式
  • 具体使用例子请参考Demo


3.2 后端推送消息时添加自定义样式id

  • 用户利用OpenApi推送消息时设定特定样式的id
  • 服务端不能设置样式,只能指定需要展现的样式id
  • 指定id的样式必须在客户端已经进行设置,否则SDK会创建默认样式的通知


3.3 Example




<a name="Example-BasicCustomPushNotification" class="reference-link" style="box-sizing: border-box; color: rgb(0, 193, 224); background: 0px 0px; transition: color 0.2s; font-weight: inherit;">Example-BasicCustomPushNotification

  1. [backcolor=transparent]BasicCustomPushNotification[backcolor=transparent] notification [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]BasicCustomPushNotification[backcolor=transparent]();
  2. [backcolor=transparent]notification[backcolor=transparent].[backcolor=transparent]setRemindType[backcolor=transparent]([backcolor=transparent]BasicCustomPushNotification[backcolor=transparent].[backcolor=transparent]REMIND_TYPE_SOUND[backcolor=transparent]);
  3. [backcolor=transparent]notification[backcolor=transparent].[backcolor=transparent]setStatusBarDrawable[backcolor=transparent]([backcolor=transparent]R[backcolor=transparent].[backcolor=transparent]drawable[backcolor=transparent].[backcolor=transparent]logo_yuanjiao_120[backcolor=transparent]);
  4. [backcolor=transparent]boolean[backcolor=transparent] res [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]CustomNotificationBuilder[backcolor=transparent].[backcolor=transparent]getInstance[backcolor=transparent]().[backcolor=transparent]setCustomNotification[backcolor=transparent]([backcolor=transparent]1[backcolor=transparent],[backcolor=transparent] notification[backcolor=transparent]);


<a name="Example-AdvancedCustomPushNotification" class="reference-link" style="box-sizing: border-box; color: rgb(0, 193, 224); background: 0px 0px; transition: color 0.2s; font-weight: inherit;">Example-AdvancedCustomPushNotification

  1. [backcolor=transparent]AdvancedCustomPushNotification[backcolor=transparent] notification [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]AdvancedCustomPushNotification[backcolor=transparent]([backcolor=transparent]R[backcolor=transparent].[backcolor=transparent]layout[backcolor=transparent].[backcolor=transparent]notitfication_layout[backcolor=transparent],[backcolor=transparent] R[backcolor=transparent].[backcolor=transparent]id[backcolor=transparent].[backcolor=transparent]m_icon[backcolor=transparent],[backcolor=transparent] R[backcolor=transparent].[backcolor=transparent]id[backcolor=transparent].[backcolor=transparent]m_title[backcolor=transparent],[backcolor=transparent] R[backcolor=transparent].[backcolor=transparent]id[backcolor=transparent].[backcolor=transparent]m_text[backcolor=transparent]);
  2. [backcolor=transparent]notification[backcolor=transparent].[backcolor=transparent]setServerOptionFirst[backcolor=transparent]([backcolor=transparent]true[backcolor=transparent]);
  3. [backcolor=transparent]notification[backcolor=transparent].[backcolor=transparent]setBuildWhenAppInForeground[backcolor=transparent]([backcolor=transparent]false[backcolor=transparent]);
  4. [backcolor=transparent]boolean[backcolor=transparent] res [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]CustomNotificationBuilder[backcolor=transparent].[backcolor=transparent]getInstance[backcolor=transparent]().[backcolor=transparent]setCustomNotification[backcolor=transparent]([backcolor=transparent]2[backcolor=transparent],[backcolor=transparent] notification[backcolor=transparent]);


<a name="Example-OpenApi" class="reference-link" style="box-sizing: border-box; color: rgb(0, 193, 224); background: 0px 0px; transition: color 0.2s; font-weight: inherit;">Example-OpenApi


客户端设置完成后,服务端在推送通知时需要利用OpenApi指明对应的自定义样式ID
  1. [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]SimpleDateFormat[backcolor=transparent] dateFormat [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]SimpleDateFormat[backcolor=transparent]([backcolor=transparent]"MM-dd HH:mm:ss"[backcolor=transparent]);
  2. [backcolor=transparent]final[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] date [backcolor=transparent]=[backcolor=transparent] dateFormat[backcolor=transparent].[backcolor=transparent]format[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Date[backcolor=transparent]());
  3. [backcolor=transparent]PushRequest[backcolor=transparent] pushRequest [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]PushRequest[backcolor=transparent]();
  4. [backcolor=transparent]// 推送目标
  5. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setAppKey[backcolor=transparent]([backcolor=transparent]appKey[backcolor=transparent]);
  6. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setTarget[backcolor=transparent]([backcolor=transparent]"device"[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]//推送目标: device:推送给设备; account:推送给指定帐号,tag:推送给自定义标签; all: 推送给全部
  7. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setTargetValue[backcolor=transparent]([backcolor=transparent]"deviceId"[backcolor=transparent]);[backcolor=transparent]
  8. [backcolor=transparent]// 推送配置
  9. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setType[backcolor=transparent]([backcolor=transparent]1[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 0:表示消息(默认为0), 1:表示通知
  10. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setTitle[backcolor=transparent]([backcolor=transparent]date[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 消息的标题
  11. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setBody[backcolor=transparent]([backcolor=transparent]"PushRequest body"[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 消息的内容
  12. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setSummary[backcolor=transparent]([backcolor=transparent]"PushRequest summary"[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 通知的摘要
  13. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setAndroidNotificationBarType[backcolor=transparent]([backcolor=transparent]2[backcolor=transparent]);[backcolor=transparent]//设置的通知样式ID,通知栏自定义样式范围0-100
  14. [backcolor=transparent]// 推送配置: Android
  15. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setAndroidOpenType[backcolor=transparent]([backcolor=transparent]"1"[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 点击通知后动作,1:打开应用 2: 打开应用Activity 3:打开 url
  16. [backcolor=transparent]pushRequest[backcolor=transparent].[backcolor=transparent]setAndroidExtParameters[backcolor=transparent]([backcolor=transparent]"{\"_NOTIFICATION_BAR_STYLE_\":\"2\"}"[backcolor=transparent]);


3.4 BasicCustomPushNotification API




默认构造函数

  • BasicCustomPushNotification的默认构造函数,所有配置采用默认设置:通知方式采用震动+通知;NotificationFlag采用Notification.FLAG_AUTO_CANCEL,状态栏图标用的是android.R.drawable.stat_notify_chat。
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]BasicCustomPushNotification[backcolor=transparent]();



构造函数



参数

  • drawable 状态栏图标
  • flags NotificationFlags,支持系统Notification下的Flag参数
  • remindType 提醒类型,有BasicCustomPushNotification.REMIND_TYPE_SILENT:静默;BasicCustomPushNotification.REMIND_TYPE_VIBRATE:震动;BasicCustomPushNotification.REMIND_TYPE_SOUND:声音;BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND:声音+震动
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]BasicCustomPushNotification[backcolor=transparent]([backcolor=transparent]int[backcolor=transparent] drawable[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] flags[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] remindType[backcolor=transparent]);



获取状态栏图标

  • 获取已设置的状态栏图标
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] getStatusBarDrawable[backcolor=transparent]()



设置状态栏图标

  • 更改状态栏图标设置


参数

  • statusBarDrawable 状态栏图标资源id
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] setStatusBarDrawable[backcolor=transparent]([backcolor=transparent]int[backcolor=transparent] statusBarDrawable[backcolor=transparent]);



获取提醒方式

  • 获取已经设置的提醒方式
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] getRemindType[backcolor=transparent]();



设置提醒方式

  • 更改自定义通知的提醒方式


参数

  • remindType 提醒方式,提供的参数有:BasicCustomPushNotification.REMIND_TYPE_SILENT:静默;BasicCustomPushNotification.REMIND_TYPE_VIBRATE:震动;BasicCustomPushNotification.REMIND_TYPE_SOUND:声音;BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND:声音+震动
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] setRemindType[backcolor=transparent]([backcolor=transparent]int[backcolor=transparent] remindType[backcolor=transparent]);



获取Notification Flags参数

  • 获取已经设置的notification flag参数
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] getNotificationFlags[backcolor=transparent]();



设置Notification Flags参数

  • 更改自定义通知的flags参数


参数

  • notificationFlags 支持系统自带的Notification Flag参数
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] setNotificationFlags[backcolor=transparent]([backcolor=transparent]int[backcolor=transparent] notificationFlags[backcolor=transparent]);



获取是否服务端设置优先

  • 利用OpenApi或者阿里云推送控制台推送消息都可以设置提醒方式,当后端设置的提醒方式和自定义样式提醒方式冲突时,SDK根据serverOptionFirst参数来判断提醒方式策略。如果该参数为true,则采用后端设定的提醒方式;如果该参数为false,则采用自定义样式指定的提醒方式。默认为false
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]boolean[backcolor=transparent] isServerOptionFirst[backcolor=transparent]();



设置是否服务端优先

  • 更改自定义通知的serverOptionFirst参数


参数

  • serverOptionFirst 是否服务器配置优先
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] setServerOptionFirst[backcolor=transparent]([backcolor=transparent]boolean[backcolor=transparent] serverOptionFirst[backcolor=transparent]);



获取推送前台到达否创建通知参数

  • 当推送到达时,如果应用处在前台,用户可以通过自定义样式决定是否创建通知。默认是创建通知
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]boolean[backcolor=transparent] isBuildWhenAppInForeground[backcolor=transparent]();



设置推送前台到达否创建通知参数

  • 更改当推送到达时应用处在前台情况下是否创建通知的设置


参数

  • buildWhenAppInForeground 是否创建通知
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] setBuildWhenAppInForeground[backcolor=transparent]([backcolor=transparent]boolean[backcolor=transparent] buildWhenAppInForeground[backcolor=transparent]);



3.5 AdvancedCustomPushNotification API

  • AdvancedCustomPushNotification是BasicCustomPushNotification的子类,继承了上文中BasicCustomPushNotification的所有方法。


AdvancedCustomPushNotification构造函数

  • AdvancedCustomPushNotification类的构造函数,AdvancedCustomPushNotification没有默认构造函数


参数

  • view 自定义通知布局文件id。注:Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局。
  • iconViewId 自定义布局文件中icon的viewId
  • titleViewId 自定义布局文件中title的viewId
  • contentViewId 自定义布局文件中显示通知正文的viewId
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]AdvancedCustomPushNotification[backcolor=transparent]([backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] view[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] iconViewId[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] titleViewId[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] contentViewId[backcolor=transparent]);



设置通知图标

  • 设置通知栏中显示的图标,该图标显示在iconViewId所指定的控件中。


参数

  • icon icon图标资源id
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] setIcon[backcolor=transparent]([backcolor=transparent]int[backcolor=transparent] icon[backcolor=transparent]);



获取通知图标

  • 获取设置的通知图标
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]int[backcolor=transparent] getIcon[backcolor=transparent]();



3.6 CustomNotificationBuilder API

  • CustomNotificationBuilder用于注册用户设定好的自定义样式通知


获取CustomNotificationBuilder实例

  • CustomNotificationBuilder是单例类,必须通过指定接口来获取实例
  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]CustomNotificationBuilder[backcolor=transparent] getInstance[backcolor=transparent]();



注册自定义样式通知

  • 用户创建好自定义样式通知后需要将其注册,并赋予其一个特定的id


参数

  • customNotificationId 所注册的自定义样式通知的id,id必须大于0。如果将多个不同的自定义样式通知赋予同一个id,则最后注册的通知有效,其他的通知将会被覆盖
  • notification 创建的通知,该通知可以是BasicCustomPushNotification对象也可以是AdvancedCustomPushNotification对象,但是不能为null


返回

  • 该方法会返回一个boolean类型的结果,如果返回true,则注册成功;反之则失败。
  1. [backcolor=transparent]public[backcolor=transparent]  [backcolor=transparent]boolean[backcolor=transparent] setCustomNotification[backcolor=transparent]([backcolor=transparent]int[backcolor=transparent] customNotificationId[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]BasicCustomPushNotification[backcolor=transparent] notification[backcolor=transparent]);

展开
收起
猫饭先生 2017-10-23 11:01:38 1632 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
ACE 区域技术发展峰会:Flink Python Table API入门及实践 立即下载
Java Spring Boot开发实战系列课程【第15讲】:Spring Boot 2.0 API与Spring REST Docs实战 立即下载
一个跨平台的云服务SDK需要什么 立即下载