开发者社区> 问答> 正文

python如何使用



1 获取AccessKeyId和AccessKeySecret


>>前往阿里云官网控制台获取

2 获取appKey


>>前往移动推动控制台获取 app列表->应用证书

3 引入SDK依赖

  1. [backcolor=transparent]import[backcolor=transparent] properties
  2. [backcolor=transparent]from[backcolor=transparent] aliyunsdkpush[backcolor=transparent].[backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]v20150827 [backcolor=transparent]import[backcolor=transparent] [backcolor=transparent]PushRequest
  3. [backcolor=transparent]from[backcolor=transparent] aliyunsdkcore [backcolor=transparent]import[backcolor=transparent] client


4 示例代码(替换成您的AccessKeyId、AccessKeySecret、appKey)

  1. [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])
  2. [backcolor=transparent]request [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]PushRequest[backcolor=transparent].[backcolor=transparent]PushRequest[backcolor=transparent]()
  3. [backcolor=transparent]#替换成自己的appKey
  4. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_AppKey[backcolor=transparent]([backcolor=transparent]properties[backcolor=transparent].[backcolor=transparent]appKey[backcolor=transparent])
  5. [backcolor=transparent]#推送目标: device:推送给设备; account:推送给指定帐号,tag:推送给自定义标签; all: 推送给全部
  6. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Target[backcolor=transparent]([backcolor=transparent]'all'[backcolor=transparent])
  7. [backcolor=transparent]#根据Target来设定,如Target=device, 则对应的值为 设备id1,设备id2. 多个值使用逗号分隔.(帐号与设备有一次最多100个的限制)
  8. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_TargetValue[backcolor=transparent]([backcolor=transparent]'all'[backcolor=transparent])
  9. [backcolor=transparent]#设备类型deviceType 取值范围为:0~3. iOS设备: 0; Android设备: 1; 全部: 3, 这是默认值
  10. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_DeviceType[backcolor=transparent]([backcolor=transparent]1[backcolor=transparent])
  11. [backcolor=transparent]#0:表示消息(默认为0), 1:表示通知
  12. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Type[backcolor=transparent]([backcolor=transparent]0[backcolor=transparent])
  13. [backcolor=transparent]#消息的标题
  14. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Title[backcolor=transparent]([backcolor=transparent]"Hello Open Api!"[backcolor=transparent])
  15. [backcolor=transparent]#消息的内容
  16. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Body[backcolor=transparent]([backcolor=transparent]"Body from ali push open api:Push"[backcolor=transparent])
  17. [backcolor=transparent]#通知的摘要
  18. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_Summary[backcolor=transparent]([backcolor=transparent]"Summary from ali push open api:Push"[backcolor=transparent])
  19. [backcolor=transparent]#离线消息是否保存,若保存, 在推送时候,用户即使不在线,下一次上线则会收到
  20. [backcolor=transparent]#request.set_StoreOffline(False)
  21. [backcolor=transparent]# iOS配置
  22. [backcolor=transparent]#iOS应用图标右上角角标
  23. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSBadge[backcolor=transparent]([backcolor=transparent]"5"[backcolor=transparent])
  24. [backcolor=transparent]#iOS通知声音
  25. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSMusic[backcolor=transparent]([backcolor=transparent]"default"[backcolor=transparent])
  26. [backcolor=transparent]# iOS通知标题(iOS 10+)
  27. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSTitle[backcolor=transparent]([backcolor=transparent]"iOS 10 Title"[backcolor=transparent])
  28. [backcolor=transparent]# iOS通知副标题(iOs 10+)
  29. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSSubtitle[backcolor=transparent]([backcolor=transparent]"iOS 10 Subtitle"[backcolor=transparent])
  30. [backcolor=transparent]# 使能通知扩展处理(iOS 10+)
  31. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSMutableContent[backcolor=transparent]([backcolor=transparent]True[backcolor=transparent])
  32. [backcolor=transparent]# 设定通知Category(iOS 10+)
  33. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSNotificationCategory[backcolor=transparent]([backcolor=transparent]"test_category"[backcolor=transparent])
  34. [backcolor=transparent]#自定义的kv结构,开发者扩展用 针对iOS设备(iOS 10+ 可以使用关键字attachment来指定富媒体推送通知的资源Url)
  35. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_iOSExtParameters[backcolor=transparent]([backcolor=transparent]"{\"attachment\":\"https://xxxx.xxx/notification_pic.png\",\"k2\":\"v2\"}"[backcolor=transparent])
  36. [backcolor=transparent]#推送时设备不在线(既与移动推送的服务端的长连接通道不通),则这条推送会做为通知,通过苹果的APNs通道送达一次(发送通知时,Summary为通知的内容,Message不起作用)。注意:离线消息转通知仅适用于生产环境
  37. [backcolor=transparent]#request.set_Remind(False)
  38. [backcolor=transparent]#iOS环境
  39. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_ApnsEnv[backcolor=transparent]([backcolor=transparent]"DEV"[backcolor=transparent])
  40. [backcolor=transparent]#android配置
  41. [backcolor=transparent]#设置该参数后启动小米托管弹窗功能,此处指定通知点击后跳转的Activity(托管弹窗的前提条件:1. 继承小米辅助通道;2. storeOffline设为true)
  42. [backcolor=transparent]#request.set_XiaomiActivity("_Your_XiaoMi_Activity_")
  43. [backcolor=transparent]#点击通知后动作,1:打开应用 2: 打开应用Activity 3:打开 url 4 : 无跳转逻辑
  44. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_AndroidOpenType[backcolor=transparent]([backcolor=transparent]"3"[backcolor=transparent])
  45. [backcolor=transparent]#Android收到推送后打开对应的url,仅仅当androidOpenType=3有效
  46. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_AndroidOpenUrl[backcolor=transparent]([backcolor=transparent]"http://www.baidu.com"[backcolor=transparent])
  47. [backcolor=transparent]#设定android类型设备通知的扩展属性
  48. [backcolor=transparent]request[backcolor=transparent].[backcolor=transparent]set_AndroidExtParameters[backcolor=transparent]([backcolor=transparent]"{\"k1\":\"android\",\"k2\":\"v2\"}"[backcolor=transparent])
  49. [backcolor=transparent]result [backcolor=transparent]=[backcolor=transparent] clt[backcolor=transparent].[backcolor=transparent]do_action[backcolor=transparent]([backcolor=transparent]request[backcolor=transparent])
  50. [backcolor=transparent]print[backcolor=transparent] result

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

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载