react native集成极光推送 android版

demo源码

1.注册极光推送
https://www.jiguang.cn/accounts/register?nextUrl=
注册成功之后,进入 服务中心/极光开发者服务/开发者平台 创建应用
创建成功之后,选择 推送设置-android,填写包名
在应用信息 选项中 用到 AppKeyMaster Secret 信息
2.安装 jpush-react-nativejcore-react-native
安装包成功后,执行 react-native link
需要填写刚刚申请的 AppKey
检查配置项
加入 JPushPackage,有参数!
3. 在项目的入口文件中,加入以下代码:

import React,{Component} from 'react';
import {View,Text,Button} from 'react-native';
import JPush from 'jpush-react-native';

export default class Pagea extends Component{
    constructor(props){
        super(props);
        this.state={
            RegistrationID:''
        };
    }
    componentDidMount(): void {
        JPush.notifyJSDidLoad((data)=>{});
        JPush.getRegistrationID((registId)=>{
            console.log(registId);
            this.setState({RegistrationID:registId.toString()})
        });
        ///接收推送事件
        JPush.addReceiveNotificationListener((message)=>{
            console.log('message',message);
            alert('推送信息'+message.alertContent);
        });
        ///接收自定义消息 仅仅android
        JPush.addReceiveCustomMsgListener((message)=>{
            console.log('message',message);
            alert('接收自定义消息'+message);
        });
        ///点击推送事件
        JPush.addReceiveOpenNotificationListener((data)=>{
            console.log('data',data);
        });
    }
    componentWillUnmount(): void {
        JPush.removeReceiveNotificationListener(()=>{});
        JPush.removeReceiveOpenNotificationListener(()=>{});
        JPush.removeReceiveCustomMsgListener((cb)=>{});
    }

    render(){
        let {navigate}=this.props.navigation;
        let state=this.state;
        return (
            
                这是A页面
                注册Id:{state.RegistrationID}
                

4.在 极光开发者服务 中,使用推送功能-发送通知,客户端即可接收到通知
react native集成极光推送 android版_第1张图片
客户端收到通知

react-native 简单集成极光推送服务完成!

你可能感兴趣的:(React,Native)