【Uniapp】接入极光推送

极光推送用的是第三方的云端插件,推送和认证一般都差不多,以下为引入步骤:

插件地址:https://ext.dcloud.net.cn/plugin?id=741

  1. 引入插件到项目(此插件正式对接时需要购买)我在这里接的是试用版本

1607571043(1).jpg

  1. 从极光后台获取 appkey,然后填入下方位置

1607570991(1).jpg

  1. 配置完后打自定义包

image.png

  1. 打包的时候用自有证书,(前提要在极光后台配置好,一般配置信息只需要个包名和签名),

image.png

  1. 打包完成后就可以直接真机预览了,预览的时候选择自己的基座

image.png

  1. 以上准备工作就完成了,接下来直接根据插件给的 api 测试就可以了,下图是我测试时候的代码
<template>
  <view class="content">
    <image class="logo" src="/static/logo.png"></image>
    <view class="text-area"><button type="default">授权1</button></view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        jyJPush: [],
      }
    },
    onReady() {
      this.jyJPush = uni.requireNativePlugin('JY-JPush')
      this.jyJPush.setJYJPushAlias(
        {
          userAlias: 'testAlias',
        },
        result => {
          console.log(result)
          //  设置成功或者失败,都会通过这个result回调返回数据;数据格式保持极光返回的安卓/iOS数据一致
          //  注:若没有返回任何数据,考虑是否初始化完成
          // uni.showToast({
          //  icon: 'none',
          //  title: JSON.stringify(result)
          // });
        }
      )
      this.jyJPush.addJYJPushReceiveNotificationListener(result => {
        console.log(result)
        //  监听成功后,若收到推送,会在result返回对应的数据;数据格式保持极光返回的安卓/iOS数据一致
        uni.showToast({
          icon: 'none',
          title: JSON.stringify(result.notificationContent),
        })
      })
      this.jyJPush.addJYJPushReceiveOpenNotificationListener(result => {
        //  监听成功后,若点击推送消息,会触发result;数据格式保持极光返回的安卓/iOS数据一致
        uni.showToast({
          icon: 'none',
          title: JSON.stringify(result.notificationContent),
        })
      })
    },
    methods: {},
  }
</script>

<style>
  .content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .logo {
    height: 200rpx;
    width: 200rpx;
    margin-top: 200rpx;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 50rpx;
  }

  .text-area {
    display: flex;
    justify-content: center;
  }

  .title {
    font-size: 36rpx;
    color: #8f8f94;
  }
</style>
  1. 接下来说一下容易出的错

image.png

上图所示就表示插件接入成功,如果提示 6002 的话可能是短时间内调用了两次或多次,
推送消息需要从极光后台进行推送,一般免费的广播所有人就能发 10 次,但可以用别名等方式发送,设置别名插件 api 上有,只有设置好别名才能进行推送,顺序不要反了,不然后台会报错,

image.png

发送信息有可能不在通知栏显示,这时候就要自己去设置手机 app 的权限了,允许通知后就可以在系统栏收到消息了,如果需要点击消息打开 app 也是一样道理,设置下系统权限就好

posted @ 2021-05-11 10:42  [ABing]  阅读(484)  评论(0编辑  收藏  举报