• uniapp添加极光推送


    1.从dcloud插件市场下载极光SDK插件 (可以云打包,也可以离线打包)

     极光JCore官方SDK下载地址

    https://ext.dcloud.net.cn/plugin?id=4028https://ext.dcloud.net.cn/plugin?id=4028

    极光JPush官方SDK下载地址

    极光JPush官方SDK - DCloud 插件市场极光JPush官方SDK HBuilder插件版本https://ext.dcloud.net.cn/plugin?id=4035

    下载SDK插件 

    云打包:从插件市场点击for打云包,

    离线打包:点击离线打包,将SDK下载到本地,在根目录中新建【nativeplugins】文件夹,将下载的文件放在里面

     2.在项目中红配置mainifest.json

    (1)本地插件,点击mainifest.json中的【APP原生插件配置】,选择本地插件,点击确定按钮导入,在mainifest.json的源码视图中配置应用包名和AppKey

    添加本地插件

     配置JG-JPUSH,设置Android和ios的应用包名

    android_package_name:"包名" 

    ios_bundle_id:"包名"

    配置JG-JCore ,设置Android和ios的应用包名和极光官网信息的AppKey

    android_package_name:"包名"

    ios_bundle_id:"包名"

    JPUSH_APPKEY_ANDROID:"AppKey"

    (2)云端插件配置

    点击mainifest.json中的【APP原生插件配置】,选择云端插件,点击确定按钮导入,在mainifest.json的源码视图中配置应用包名和AppKey(跟上述一致)

    3.页面代码

     在App.vue中添加以下代码

    1. <script>
    2. const jpushModule = uni.requireNativePlugin('JG-JPush')
    3. export default {
    4. onLaunch: function() {
    5. console.log('App Launch')
    6. if (uni.getSystemInfoSync().platform == "ios") {
    7. // 请求定位权限
    8. let locationServicesEnabled = jpushModule.locationServicesEnabled()
    9. let locationAuthorizationStatus = jpushModule.getLocationAuthorizationStatus()
    10. console.log('locationAuthorizationStatus', locationAuthorizationStatus)
    11. if (locationServicesEnabled == true && locationAuthorizationStatus < 3) {
    12. jpushModule.requestLocationAuthorization((result) => {
    13. console.log('定位权限', result.status)
    14. })
    15. }
    16. jpushModule.requestNotificationAuthorization((result) => {
    17. let status = result.status
    18. if (status < 2) {
    19. uni.showToast({
    20. icon: 'none',
    21. title: '您还没有打开通知权限',
    22. duration: 3000
    23. })
    24. }
    25. })
    26. }
    27. jpushModule.initJPushService();
    28. jpushModule.setLoggerEnable(true);
    29. jpushModule.addConnectEventListener(result => {
    30. console.log(result)
    31. let connectEnable = result.connectEnable
    32. uni.$emit('connectStatusChange', connectEnable)
    33. });
    34. jpushModule.addNotificationListener(result => {
    35. console.log(result)
    36. if (result.notificationEventType == "notificationOpened") {
    37. //点击窗口通知栏推送的消息 跳转指定页面
    38. uni.navigateTo({
    39. url: '/pages/home/warningCenter/warningCenter',
    40. complete(res) {
    41. console.log(res);
    42. }
    43. })
    44. }
    45. });
    46. jpushModule.addCustomMessageListener(result => {
    47. let type = result.type
    48. let messageType = result.messageType
    49. let content = result.content
    50. uni.showToast({
    51. icon: 'none',
    52. title: JSON.stringify(result),
    53. duration: 3000
    54. })
    55. })
    56. jpushModule.addLocalNotificationListener(result => {
    57. let messageID = result.messageID
    58. let title = result.title
    59. let content = result.content
    60. let extras = result.extras
    61. uni.showToast({
    62. icon: 'none',
    63. title: JSON.stringify(result),
    64. duration: 3000
    65. })
    66. })
    67. jpushModule.addGeofenceListener(result => {
    68. let code = result.code
    69. let type = result.type
    70. let geofenceId = result.geofenceId
    71. let userInfo = result.userInfo
    72. uni.showToast({
    73. icon: 'none',
    74. title: '触发地理围栏',
    75. duration: 3000
    76. })
    77. })
    78. jpushModule.setIsAllowedInMessagePop(true)
    79. jpushModule.pullInMessage(result => {
    80. let code = result.code
    81. console.log(code)
    82. })
    83. jpushModule.addInMessageListener(result => {
    84. let eventType = result.eventType
    85. let messageType = result.messageType
    86. let content = result.content
    87. console.log('inMessageListener', eventType, messageType, content)
    88. uni.showToast({
    89. icon: 'none',
    90. title: JSON.stringify(result),
    91. duration: 3000
    92. })
    93. })
    94. },
    95. onShow: function() {
    96. console.log('App Show')
    97. },
    98. onHide: function() {
    99. console.log('App Hide')
    100. }
    101. }
    102. script>

    登录页代码

    1. <script>
    2. const jpushModule = uni.requireNativePlugin('JG-JPush')
    3. export default {
    4. data() {
    5. return {
    6. //极光推送
    7. connectStatus: '未连接',
    8. registrationID: '未获得',
    9. }
    10. },
    11. methods: {
    12. //点击登录按钮
    13. login() {
    14. //登录成功后
    15. //设置别名
    16. jpushModule.setAlias({
    17. 'alias': this.phone,
    18. 'sequence': 1
    19. })
    20. },
    21. connect() {
    22. uni.$on('connectStatusChange', (connectStatus) => {
    23. console.log('进入连接')
    24. var connectStr = ''
    25. if (connectStatus == true) {
    26. connectStr = '已连接'
    27. this.getRegistrationID()
    28. } else {
    29. connectStr = '未连接'
    30. }
    31. console.log('监听到了连接状态变化 --- ', connectStr)
    32. this.connectStatus = connectStr
    33. })
    34. },
    35. //获取推送ID
    36. getRegistrationID() {
    37. jpushModule.getRegistrationID(result => {
    38. let registerID = result.registerID
    39. console.log(result)
    40. this.registrationID = registerID
    41. })
    42. }
    43. },
    44. onLoad() {
    45. this.connect()
    46. }
    47. }
    48. script>

  • 相关阅读:
    Pytorch安装
    常用排序算法总结对比
    第十四届蓝桥杯第一期模拟赛 python
    MySQL总结(一)
    一百八十三、大数据离线数仓完整流程——步骤二、在Hive的ODS层建外部表并加载HDFS中的数据
    一文讲透DevOps理论体系的演进
    NumPy sum、mean、cumsum 及参数 axis,keepdims,where 的含义
    ARM DMIPS算力说明
    搜索二维矩阵[二分巧用 + 记录不同于插入二分的解法]
    在线点餐微信小程序插件 小程序一键接入肯德基麦当劳星巴克在线点餐
  • 原文地址:https://blog.csdn.net/workhard0905/article/details/127109799