push.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap" v-if="provider[0]">
  5. <view class="uni-btn-v uni-common-mt">
  6. <button type="primary" @tap="listenTranMsg">监听透传数据</button>
  7. </view>
  8. <view class="uni-btn-v uni-common-mt">
  9. <button type="primary" @tap="requireTranMsg">发送"透传数据"消息</button>
  10. </view>
  11. <view class="uni-title uni-common-mt">透传内容:</view>
  12. <view class="uni-textarea">
  13. <textarea v-model="tranMsg" />
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. export default {
  20. data() {
  21. return {
  22. title: 'push',
  23. provider: [],
  24. pushServer: 'http://demo.dcloud.net.cn/push/?',
  25. tranMsg:''
  26. }
  27. },
  28. onLoad: function () {
  29. uni.getProvider({
  30. service: "push",
  31. success: (e) => {
  32. console.log("success", e);
  33. this.provider = e.provider;
  34. },
  35. fail: (e) => {
  36. console.log("获取推送通道失败", e);
  37. }
  38. });
  39. },
  40. onUnload:function(){
  41. this.tranMsg = ''
  42. },
  43. methods: {
  44. listenTranMsg() {
  45. // IOS端在客户端在运行时收到推送消息触发receive事件,离线接收到的推送消息全部进入系统消息中心。点击消息中心的消息触发click
  46. plus.push.addEventListener('click', (msg)=> {
  47. this.tranMsg = JSON.stringify(msg)
  48. });
  49. plus.push.addEventListener('receive',(msg)=>{
  50. this.tranMsg = JSON.stringify(msg)
  51. })
  52. uni.showToast({
  53. title: '开始监听透传数据',
  54. icon: 'success'
  55. })
  56. },
  57. requireTranMsg() { //请求‘透传数据’推送消息
  58. var inf = plus.push.getClientInfo();
  59. var url = this.pushServer + 'type=tran&appid=' + encodeURIComponent(plus.runtime.appid);
  60. inf.id && (url += '&id=' + inf.id);
  61. url += ('&cid=' + encodeURIComponent(inf.clientid));
  62. if (plus.os.name == 'iOS') {
  63. url += ('&token=' + encodeURIComponent(inf.token));
  64. }
  65. url += ('&title=' + encodeURIComponent('Hello uniapp'));
  66. url += ('&content=' + encodeURIComponent('带透传数据推送通知!'));
  67. if(plus.os.name === 'iOS'){
  68. url += ('&payload=' + encodeURIComponent('{"title":"Hello uniapp Test","content":"test content"}'));
  69. }else{
  70. url += ('&payload=' + encodeURIComponent('\'{"title":"Hello uniapp Test","content":"test content"}\''));
  71. }
  72. url += ('&version=' + encodeURIComponent(plus.runtime.version));
  73. plus.runtime.openURL(url);
  74. }
  75. }
  76. }
  77. </script>
  78. <style>
  79. </style>