1 获取AccessKeyId和AccessKeySecret
>>前往阿里云官网控制台获取
2 获取appKey
>>前往移动推动控制台获取 app列表->应用证书
3 引入SDK依赖
- [backcolor=transparent]import[backcolor=transparent] properties
- [backcolor=transparent]from[backcolor=transparent] aliyunsdkpush[backcolor=transparent].[backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]v20150827 [backcolor=transparent]import[backcolor=transparent] [backcolor=transparent]PushRequest
- [backcolor=transparent]from[backcolor=transparent] aliyunsdkcore [backcolor=transparent]import[backcolor=transparent] client
4 示例代码(替换成您的AccessKeyId、AccessKeySecret、appKey)
- [backcolor=transparent]clt [backcolor=transparent]=[backcolor=transparent] client[backcolor=transparent].[backcolor=transparent]AcsClient[backcolor=transparent]([backcolor=transparent]properties[backcolor=transparent].[backcolor=transparent]accessKeyId[backcolor=transparent],[backcolor=transparent]properties[backcolor=transparent].[backcolor=transparent]accessKeySecret[backcolor=transparent],[backcolor=transparent]properties[backcolor=transparent].[backcolor=transparent]regionId[backcolor=transparent])
- [backcolor=transparent]request [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]PushRequest[backcolor=transparent].[backcolor=transparent]PushRequest[backcolor=transparent]()
- [backcolor=transparent]#替换成自己的appKey
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_AppKey[backcolor=transparent]([backcolor=transparent]properties[backcolor=transparent].[backcolor=transparent]appKey[backcolor=transparent])
- [backcolor=transparent]#推送目标: device:推送给设备; account:推送给指定帐号,tag:推送给自定义标签; all: 推送给全部
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Target[backcolor=transparent]([backcolor=transparent]'all'[backcolor=transparent])
- [backcolor=transparent]#根据Target来设定,如Target=device, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制)
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_TargetValue[backcolor=transparent]([backcolor=transparent]'all'[backcolor=transparent])
- [backcolor=transparent]#设备类型deviceType 取值范围为:0~3. iOS设备: 0; Android设备: 1; 全部: 3, 这是默认值
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_DeviceType[backcolor=transparent]([backcolor=transparent]1[backcolor=transparent])
- [backcolor=transparent]#0:表示消息(默认为0), 1:表示通知
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Type[backcolor=transparent]([backcolor=transparent]0[backcolor=transparent])
- [backcolor=transparent]#消息的标题
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Title[backcolor=transparent]([backcolor=transparent]"Hello Open Api!"[backcolor=transparent])
- [backcolor=transparent]#消息的内容
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Body[backcolor=transparent]([backcolor=transparent]"Body from ali push open api:Push"[backcolor=transparent])
- [backcolor=transparent]#通知的摘要
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Summary[backcolor=transparent]([backcolor=transparent]"Summary from ali push open api:Push"[backcolor=transparent])
- [backcolor=transparent]#离线消息是否保存,若保存, 在推送时候,用户即使不在线,下一次上线则会收到
- [backcolor=transparent]#request.set_StoreOffline(False)
- [backcolor=transparent]# iOS配置
- [backcolor=transparent]#iOS应用图标右上角角标
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSBadge[backcolor=transparent]([backcolor=transparent]"5"[backcolor=transparent])
- [backcolor=transparent]#iOS通知声音
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSMusic[backcolor=transparent]([backcolor=transparent]"default"[backcolor=transparent])
- [backcolor=transparent]# iOS通知标题(iOS 10+)
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSTitle[backcolor=transparent]([backcolor=transparent]"iOS 10 Title"[backcolor=transparent])
- [backcolor=transparent]# iOS通知副标题(iOs 10+)
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSSubtitle[backcolor=transparent]([backcolor=transparent]"iOS 10 Subtitle"[backcolor=transparent])
- [backcolor=transparent]# 使能通知扩展处理(iOS 10+)
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSMutableContent[backcolor=transparent]([backcolor=transparent]True[backcolor=transparent])
- [backcolor=transparent]# 设定通知Category(iOS 10+)
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSNotificationCategory[backcolor=transparent]([backcolor=transparent]"test_category"[backcolor=transparent])
- [backcolor=transparent]#自定义的kv结构,开发者扩展用 针对iOS设备(iOS 10+ 可以使用关键字attachment来指定富媒体推送通知的资源Url)
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSExtParameters[backcolor=transparent]([backcolor=transparent]"{\"attachment\":\"https://xxxx.xxx/notification_pic.png\",\"k2\":\"v2\"}"[backcolor=transparent])
- [backcolor=transparent]#推送时设备不在线(既与移动推送的服务端的长连接通道不通),则这条推送会做为通知,通过苹果的APNs通道送达一次(发送通知时,Summary为通知的内容,Message不起作用)。注意:离线消息转通知仅适用于生产环境
- [backcolor=transparent]#request.set_Remind(False)
- [backcolor=transparent]#iOS环境
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_ApnsEnv[backcolor=transparent]([backcolor=transparent]"DEV"[backcolor=transparent])
- [backcolor=transparent]#android配置
- [backcolor=transparent]#设置该参数后启动小米托管弹窗功能,此处指定通知点击后跳转的Activity(托管弹窗的前提条件:1. 继承小米辅助通道;2. storeOffline设为true)
- [backcolor=transparent]#request.set_XiaomiActivity("_Your_XiaoMi_Activity_")
- [backcolor=transparent]#点击通知后动作,1:打开应用 2: 打开应用Activity 3:打开 url 4 : 无跳转逻辑
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_AndroidOpenType[backcolor=transparent]([backcolor=transparent]"3"[backcolor=transparent])
- [backcolor=transparent]#Android收到推送后打开对应的url,仅仅当androidOpenType=3有效
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_AndroidOpenUrl[backcolor=transparent]([backcolor=transparent]"http://www.baidu.com"[backcolor=transparent])
- [backcolor=transparent]#设定android类型设备通知的扩展属性
- [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_AndroidExtParameters[backcolor=transparent]([backcolor=transparent]"{\"k1\":\"android\",\"k2\":\"v2\"}"[backcolor=transparent])
- [backcolor=transparent]result [backcolor=transparent]=[backcolor=transparent] clt[backcolor=transparent].[backcolor=transparent]do_action[backcolor=transparent]([backcolor=transparent]request[backcolor=transparent])
- [backcolor=transparent]print[backcolor=transparent] result