当前位置:   article > 正文

uniApp消息推送(极光/阿里云)_uniapp极光推送

uniapp极光推送

目录

一、极光推送

1.1、在极光官网创建应用

1.2、插件下载

1.3、代码填充

1.4、发送通知/消息

二、阿里云推送

2.1、在阿里云官网创建应用

2.2、插件下载

 2.3、代码填充

2.4、发给后端的值(API类型的通知

一、极光推送

1.1、在极光官网创建应用

参考 极光文档 (jiguang.cn)=======》极光文档 > 极光推送 > 快速开始 > 创建应用

在官网创建应用,前提:uniapp项目已经有自己的Android包名

1.2、插件下载

在插件市场下载对应的插件:

(1)极光JCore官方SDK 

极光JCore官方SDK - DCloud 插件市场

(2)极光JPush官方SDK

极光JPush官方SDK - DCloud 插件市场

(3)将下载的压缩包解压放在项目目录的nativeplugins文件夹下面(没有就自己新建一个)

(4)在项目的manifest.json中的App原生插件配置里面填写AppKeyAppSecret(应用在官网创建好之后就可以拿到)

1.3、代码填充

App.vue页面的onLaunch生命周期里面,写入以下代码:

  1. <script>
  2. //插件调用
  3. const jpushModule = uni.requireNativePlugin('JG-JPush')
  4. export default {
  5. onLaunch: function() {
  6. //#ifdef APP-PLUS
  7. jpushModule.setLoggerEnable(true);
  8. // 初始化函数
  9. jpushModule.initJPushService();
  10. jpushModule.addConnectEventListener(result => {
  11. let connectEnable = result.connectEnable
  12. console.log("jpush连接", connectEnable)
  13. })
  14. jpushModule.getRegistrationID(result => {
  15. // console.log("注册ID.....", result)
  16. this.registerID = result.registerID
  17. // uni.showToast({
  18. // title: result.registerID,
  19. // icon: "success",
  20. // })
  21. if (result.registerID != '') {
  22. uni.setStorageSync('registerID', result.registerID);
  23. console.log("存设备ID", this.registerID);
  24. }
  25. })
  26. jpushModule.isPushStopped(result => {
  27. let code = result.code
  28. console.log('连接状态回调', result)
  29. });
  30. // 设置别名
  31. jpushModule.setAlias({
  32. 'alias': 'coder',
  33. 'sequence': 1
  34. })
  35. jpushModule.addNotificationListener(result => {
  36. let notificationEventType = result.notificationEventType
  37. let messageID = result.messageID
  38. let title = result.title
  39. let content = result.content
  40. let extras = result.extras
  41. console.log('通知事件回调', result)
  42. // 推送一个本地通知
  43. jpushModule.addLocalNotification({
  44. messageID,
  45. title,
  46. content,
  47. extras
  48. })
  49. })
  50. // vuex防止丢失
  51. let isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  52. if (isiOS) {
  53. //在页面刷新时将vuex里的信息保存到缓存里
  54. plus.globalEvent.addEventListener("pagehide", () => {
  55. uni.setStorageSync('messageStore', JSON.stringify(this.$store.state));
  56. // localStorage.setItem("messageStore", JSON.stringify(this.$store.state))
  57. })
  58. //在页面加载时读取localStorage里的状态信息
  59. uni.getStorageSync("messageStore") && this.$store.replaceState(Object.assign(this.$store.state, JSON
  60. .parse(uni.getStorageSync("messageStore"))));
  61. // localStorage.getItem("messageStore") && this.$store.replaceState(Object.assign(this.$store.state, JSON
  62. // .parse(localStorage.getItem("messageStore"))));
  63. } else {
  64. //在页面刷新时将vuex里的信息保存到缓存里
  65. plus.globalEvent.addEventListener("beforeunload", () => {
  66. // localStorage.setItem("messageStore", JSON.stringify(this.$store.state))
  67. uni.setStorageSync('messageStore', JSON.stringify(this.$store.state));
  68. })
  69. //在页面加载时读取localStorage里的状态信息
  70. // localStorage.getItem("messageStore") && this.$store.replaceState(Object.assign(this.$store.state, JSON
  71. // .parse(localStorage.getItem("messageStore"))));
  72. uni.getStorageSync("messageStore") && this.$store.replaceState(Object.assign(this.$store.state, JSON
  73. .parse(uni.getStorageSync("messageStore"))));
  74. }
  75. if (uni.getStorageSync.getItem("list")) {
  76. this.$store.replaceState(
  77. Object.assign({},
  78. this.$store.state,
  79. JSON.parse(uni.getStorageSync.getItem("list"))
  80. )
  81. );
  82. }
  83. plus.globalEvent.addEventListener("beforeunload", () => {
  84. uni.setStorageSync.setItem("list", JSON.stringify(this.$store.state));
  85. });
  86. //#endif
  87. },
  88. onShow: function() {
  89. console.log('App Show')
  90. },
  91. onHide: function() {
  92. console.log('App Hide')
  93. }
  94. }
  95. </script>
  96. <style lang="scss">
  97. @import "uview-ui/index.scss";
  98. /*每个页面公共css */
  99. @import '@/uni_modules/uni-scss/index.scss';
  100. /* #ifndef APP-NVUE */
  101. @import '@/static/customicons.css';
  102. // 设置整个项目的背景色
  103. page {
  104. background-color: #f5f5f5;
  105. }
  106. /* #endif */
  107. .example-info {
  108. font-size: 14px;
  109. color: #333;
  110. padding: 10px;
  111. }
  112. </style>

1.4、发送通知/消息

在官网登陆后,进入【消息推送】-【推送管理】-【创建推送】-【通知消息】页面创建推送任务。

成功的话,在手机上就可以看到通知。这种属于“控制台”推送,后台的“API”推送功能需要后端配置。

二、阿里云推送

2.1、在阿里云官网创建应用

参考官网文档  快速入门_移动研发平台EMAS-阿里云帮助中心  

注意:创建的是Native应用,不是H5应用。

在官网创建应用,前提:uniapp项目已经有自己的Android包名

2.2、插件下载

(1)阿里云移动推送

阿里云移动推送 - DCloud 插件市场

(2)将下载的压缩包解压放在项目目录的nativeplugins文件夹下面(没有就自己新建一个)

(3)在项目的manifest.json中的App原生插件配置里面填写AppKeyAppSecret(应用在官网创建好之后就可以拿到)

(4)对2的补充:2属于本地下载使用插件,也可以选择for云打包,则在云端插件里勾选配置

这两种任选一种进行配置即可! 

注意:项目打包前在manifest.json里面,一定要勾选Push(消息推送)权限

 2.3、代码填充

App.vue页面的onLaunch生命周期里面,写入以下代码:

  1. <script>
  2. // const jpushModule = uni.requireNativePlugin('JG-JPush')
  3. const aliyunPush = uni.requireNativePlugin('Aliyun-Push');
  4. // const aliyunThirdPush = uni.requireNativePlugin('Aliyun-ThirdPush');
  5. export default {
  6. onLaunch: function() {
  7. //#ifdef APP-PLUS
  8. // 通知通道创建
  9. const channel = uni.requireNativePlugin('Aliyun-Push-NotificationChannel');
  10. channel.createChannel({
  11. id: 'aaa',
  12. //特别注意:发送消息在高级设置==>Android8.0特殊配置:===>通知通道:===>里面加上id
  13. name: '智慧校园',
  14. desc: '测试创建通知通道',
  15. importance: 3,
  16. });
  17. // 注册推送
  18. // const aliyunPush = uni.requireNativePlugin('Aliyun-Push');
  19. aliyunPush.registerPush({}, result => {
  20. const event = result.event;
  21. if (event === 'registerPush') {
  22. if (result.code === 'success') {
  23. console.log("注册推送 成功 ");
  24. } else {
  25. console.log("注册推送 " + result.code + " " + result.msg);
  26. }
  27. } else {
  28. const data = JSON.stringify(result.data);
  29. console.log("receive push event : " + event);
  30. console.log("receive push data : " + data);
  31. }
  32. });
  33. // (可选)注册厂商通道
  34. // const aliyunThirdPush = uni.requireNativePlugin('Aliyun-ThirdPush');
  35. // aliyunThirdPush.registerThirdPush({}, result => {
  36. // const data = JSON.stringify(result);
  37. // console.log("receive third push : " + data);
  38. // });
  39. // 获取设备ID
  40. // const aliyunPush = uni.requireNativePlugin('Aliyun-Push');
  41. const result = aliyunPush.getDeviceId();
  42. // console.log("getDeviceId : ", result.data.deviceId);
  43. if (result.data.deviceId != '') {
  44. uni.setStorageSync('registerID', result.data.deviceId);
  45. // console.log("存设备ID", result.data.deviceId);
  46. }
  47. // vuex防止丢失
  48. let isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  49. if (isiOS) {
  50. //在页面刷新时将vuex里的信息保存到缓存里
  51. plus.globalEvent.addEventListener("pagehide", () => {
  52. uni.setStorageSync('messageStore', JSON.stringify(this.$store.state));
  53. // localStorage.setItem("messageStore", JSON.stringify(this.$store.state))
  54. })
  55. //在页面加载时读取localStorage里的状态信息
  56. uni.getStorageSync("messageStore") && this.$store.replaceState(Object.assign(this.$store.state, JSON
  57. .parse(uni.getStorageSync("messageStore"))));
  58. // localStorage.getItem("messageStore") && this.$store.replaceState(Object.assign(this.$store.state, JSON
  59. // .parse(localStorage.getItem("messageStore"))));
  60. } else {
  61. //在页面刷新时将vuex里的信息保存到缓存里
  62. plus.globalEvent.addEventListener("beforeunload", () => {
  63. // localStorage.setItem("messageStore", JSON.stringify(this.$store.state))
  64. uni.setStorageSync('messageStore', JSON.stringify(this.$store.state));
  65. })
  66. //在页面加载时读取localStorage里的状态信息
  67. // localStorage.getItem("messageStore") && this.$store.replaceState(Object.assign(this.$store.state, JSON
  68. // .parse(localStorage.getItem("messageStore"))));
  69. uni.getStorageSync("messageStore") && this.$store.replaceState(Object.assign(this.$store.state, JSON
  70. .parse(uni.getStorageSync("messageStore"))));
  71. }
  72. if (uni.getStorageSync.getItem("list")) {
  73. this.$store.replaceState(
  74. Object.assign({},
  75. this.$store.state,
  76. JSON.parse(uni.getStorageSync.getItem("list"))
  77. )
  78. );
  79. }
  80. plus.globalEvent.addEventListener("beforeunload", () => {
  81. uni.setStorageSync.setItem("list", JSON.stringify(this.$store.state));
  82. });
  83. //#endif
  84. },
  85. onShow: function() {
  86. console.log('App Show')
  87. },
  88. onHide: function() {
  89. console.log('App Hide')
  90. }
  91. }
  92. </script>
  93. <style lang="scss">
  94. @import "uview-ui/index.scss";
  95. /*每个页面公共css */
  96. @import '@/uni_modules/uni-scss/index.scss';
  97. /* #ifndef APP-NVUE */
  98. @import '@/static/customicons.css';
  99. // 设置整个项目的背景色
  100. page {
  101. background-color: #f5f5f5;
  102. }
  103. /* #endif */
  104. .example-info {
  105. font-size: 14px;
  106. color: #333;
  107. padding: 10px;
  108. }
  109. </style>

2.4、发给后端的值(API类型的通知)

RAM 访问控制 (aliyun.com)    将得到的 AccessKey ID和AccessKey Secret发给后端。

特别注意:

发送消息时高级设置==>Android8.0特殊配置===>通知通道===>里面加上代码里id的值(我的是’aaa‘)。

最后,项目打包图片:

 最后两项二选一,看自己需求。正式包选择’打正式包‘,调试打’自定义调试基座‘。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/950324
推荐阅读
相关标签
  

闽ICP备14008679号