广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >react-native消息推送实现方式
  • 878
分享到

react-native消息推送实现方式

react-native消息推送react-native消息推送 2023-02-18 18:02:33 878人浏览 独家记忆
摘要

目录React-native极光推送一、安装插件二、配置安卓配置iOS配置三、使用解决ios角标无法清除总结react-native极光推送 先去官网注册:https://www.j

react-native极光推送

先去官网注册:https://www.jiguang.cn并创建应用

一、安装插件

jpush-react-native是极光推送官方开发的 React Native 版本插件,用于消息推送。

注意:如果项目里没有jcore-react-native,需要安装

npm install jpush-react-native --save
npm install jcore-react-native --save 

二、配置

安卓配置

1、修改Android/app/build.gradle文件

android {
      defaultConfig {
          applicationId "yourApplicationId"           //在此替换你的应用包名
          ...
          manifestPlaceholders = [
                  JPUSH_APPKEY: "yourAppKey",         //在此替换你的APPKey
                  JPUSH_CHANNEL: "developer-default"        //在此替换你的channel,也可使用默认的
          ]
      }
  }
dependencies {
      ...
      implementation project(':jpush-react-native')  // 添加 jpush 依赖
      implementation project(':jcore-react-native')  // 添加 jcore 依赖
  }

2、在android/setting.gradle文件中添加如下代码:

include ':jpush-react-native'
project(':jpush-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jpush-react-native/android')
include ':jcore-react-native'
project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')

3、在android/app/src/main/AndroidManifest.xml文件添加如下代码:

<meta-data
    android:name="JPUSH_CHANNEL"
    android:value="${JPUSH_CHANNEL}" />
<meta-data
    android:name="JPUSH_APPKEY"
    android:value="${JPUSH_APPKEY}" />

IOS配置

1、切换到ios目录下,Pod方式

pod install
//注意:如果项目里使用pod安装过,请先执行命令
pod deintegrate

2、手动方式

Libraries
Add Files to “your project name”
node_modules/jcore-react-native/ios/RCTJCoreModule.xcodeproj
node_modules/jpush-react-native/ios/RCTJPushModule.xcodeproj

Capabilities
Push Notification — ON

Build Settings
All — Search Paths — Header Search Paths — +
$(SRCROOT)/…/node_modules/jcore-react-native/ios/RCTJCoreModule/
$(SRCROOT)/…/node_modules/jpush-react-native/ios/RCTJPushModule/

Build Phases
libz.tbd
libresolv.tbd
UserNotifications.framework
libRCTJCoreModule.a
libRCTJPushModule.a

三、使用

import React from 'react';
import {StyleSheet, Text, View, TouchableHighlight} from 'react-native';
import JPush from 'jpush-react-native';

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    setBtnStyle: {
        width: 320,
        justifyContent: 'center',
        alignItems: 'center',
        marginTop: 10,
        borderWidth: 1,
        borderColor: '#3e83D7',
        borderRadius: 8,
        backgroundColor: '#3e83d7',
        padding: 10
    },
    textStyle: {
        textAlign: 'center',
        fontSize: 25,
        color: '#ffffff'
    }
});

class Button extends React.Component {
    render() {
        return <TouchableHighlight
            onPress={this.props.onPress}
            underlayColor='#e4083f'
            activeOpacity={0.5}
        >
            <View
                style={styles.setBtnStyle}>
                <Text
                    style={styles.textStyle}>
                    {this.props.title}
                </Text>
            </View>
        </TouchableHighlight>
    }
}

export default class App extends React.Component {

    constructor(props) {
        super(props);
    }

    componentDidMount() {
        JPush.init();
        //连接状态
        this.connectListener = result => {
            console.log("connectListener:" + JSON.stringify(result))
        };
        JPush.addConnectEventListener(this.connectListener);
        //通知回调
        this.notificationListener = result => {
            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))
        };
        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);
    }

    render() {
        return (
            <View style={styles.container}>
                <Button title="setLoggerEnable"
                        onPress={() => JPush.setLoggerEnable(true)
                        }/>

                <Button title="getReGISterID"
                        onPress={() => JPush.getRegistrationID(result =>
                            console.log("registerID:" + JSON.stringify(result))
                        )}/>

                {}

            </View>
        );
    }

}

----------------大功告成啦-----------------------------

更多配置信息以及使用可见GitHub:Https://github.com/jpush/jpush-react-native

解决ios角标无法清除

1.修改 node_modules/jpush-react-native/ios/RCTJPushModule/RCTJPushModule.m文件

RCT_EXPORT_METHOD(setBadge:(NSDictionary *)params)
{
    if(params[BADGE]){
        NSNumber *number = params[BADGE];
        [UIApplication sharedApplication].applicationIconBadgeNumber = [number integerValue]; // 增加这句
        [JPUSHService setBadge:[number integerValue]];
    }
}

2.使用手动清除

JPush.setBadge({badge: 0})

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: react-native消息推送实现方式

本文链接: https://www.lsjlt.com/news/196725.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作