极光推送注册登陆完>>>点击右上角的应用管理>>>然后点击创建应用>>>拿到应用包名和AppKey

基础配置就按照官网的配置就好了
https://www.npmjs.com/package/jpush-react-native

JPUSH_CHANNEL配置一般直接是“default”

切记包名改成极光推送上面的之后,其他地方的包名也要保持一致,不然会报错
然后就可以测试了

import JPush from 'jpush-react-native';//极光推送
import Config from "react-native-config"; // 这是区分开发和线上版本的插件
省略...
componentDidMount() {
    console.log('a')
    this.props.navigation.dispatch(resetAction)
    JPush.init(result => {
      console.log('inti', result)
    });
    //连接状态
    this.connectListener = result => {
      console.log("connectListener:" + JSON.stringify(result))
    };
    JPush.addConnectEventListener(this.connectListener);
    //通知回调
    this.notificationListener = result => {
      this.navigation.navigate('Datamonitoring')
      console.log("notificationListener:" + JSON.stringify(result))
    };
    JPush.addNotificationListener(this.notificationListener);
    //本地通知回调
    this.localNotificationListener = result => {
      console.log("localNotificationListener:" + JSON.stringify(result))
    };
    JPush.addLocalNotificationListener(this.localNotificationListener);
    //自定义消息回调
    this.customMessageListener = result => {
      console.log("customMessageListener:" + JSON.stringify(result))
    };
    // 指定推送用户 我这里的user是区别开发和线上,使用的是react-native-config插件
    JPush.setAlias({ sequence: 2, alias: String(Config.SET_ALIAS + '_user') })
    JPush.addCustomMessagegListener(this.customMessageListener);
    //tag alias事件回调
    this.tagAliasListener = result => {
      console.log("tagAliasListener:" + JSON.stringify(result))
    };
    JPush.addTagAliasListener(this.tagAliasListener);
    //手机号码事件回调
    this.mobileNumberListener = result => {
      console.log("mobileNumberListener:" + JSON.stringify(result))
    };
    JPush.addMobileNumberListener(this.mobileNumberListener);

    // 安卓监听返回按钮事件
    if (Platform.OS === 'android') {
      console.log('进来了')
      BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
    }
  }

在这里插入图片描述
在这里插入图片描述
这里是选择设备别名来发送,设备别名为user可以接收到通知,也可以广播所有人但是有次数限制!

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐