当前位置:   article > 正文

app消息推送—极光推送(华为配置)react-native

app消息推送—极光推送(华为配置)react-native

rn版本0.72.4 andorid

消息推送打开app时和杀死进程时走的通道不同,当杀死进程走厂商通道,需要对每个安卓手机进行配置。

一、在线时配置(需要真机)

https://github.com/jpush/jpush-react-native

根据这个链接在项目中进行配置

首先执行这两行命令,进行安装 

npm install jcore-react-native --save

npm install jpush-react-native --save

rn在0.7版本不需要link

在android/app/build.gradle目录里面添加

  1. android {
  2. defaultConfig {
  3. applicationId "yourApplicationId" //在此替换你的应用包名,只需要添加有注释//的三行
  4. ...
  5. manifestPlaceholders = [
  6. JPUSH_APPKEY: "yourAppKey", //在此替换你的APPKey
  7. JPUSH_CHANNEL: "yourChannel" //在此替换你的channel
  8. ]
  9. }
  10. }

1、applicationId为包名,

即namespace

channel: default即可。

在同一个文件加上这两个

  1. dependencies {
  2. ...
  3. implementation project(':jpush-react-native') // 添加 jpush 依赖
  4. implementation project(':jcore-react-native') // 添加 jcore 依赖
  5. }

然后在android/settings.gradle文件加上

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

然后在android/app/src/main/AndroidMainifest.xml文件加上

  1. <meta-data
  2. android:name="JPUSH_CHANNEL"
  3. android:value="${JPUSH_CHANNEL}" />
  4. <meta-data
  5. android:name="JPUSH_APPKEY"
  6. android:value="${JPUSH_APPKEY}" />

注意位置,与activity并列。

配置完成了,编写页面代码,

  1. import React from 'react';
  2. import {StyleSheet, Text, View, TouchableHighlight} from 'react-native';
  3. import JPush from 'jpush-react-native';
  4. const styles = StyleSheet.create({
  5. container: {
  6. flex: 1,
  7. justifyContent: 'center',
  8. alignItems: 'center',
  9. backgroundColor: '#F5FCFF',
  10. },
  11. setBtnStyle: {
  12. width: 320,
  13. justifyContent: 'center',
  14. alignItems: 'center',
  15. marginTop: 10,
  16. borderWidth: 1,
  17. borderColor: '#3e83d7',
  18. borderRadius: 8,
  19. backgroundColor: '#3e83d7',
  20. padding: 10
  21. },
  22. textStyle: {
  23. textAlign: 'center',
  24. fontSize: 25,
  25. color: '#ffffff'
  26. }
  27. });
  28. class Button extends React.Component {
  29. render() {
  30. return <TouchableHighlight
  31. onPress={this.props.onPress}
  32. underlayColor='#e4083f'
  33. activeOpacity={0.5}
  34. >
  35. <View
  36. style={styles.setBtnStyle}>
  37. <Text
  38. style={styles.textStyle}>
  39. {this.props.title}
  40. </Text>
  41. </View>
  42. </TouchableHighlight>
  43. }
  44. }
  45. export default class App extends React.Component {
  46. constructor(props) {
  47. super(props);
  48. }
  49. componentDidMount() {
  50. JPush.init({"appKey":"8e5961b77c65287cf45c6b00","channel":"default","production":1});
  51. //连接状态
  52. this.connectListener = result => {
  53. console.log("connectListener:" + JSON.stringify(result))
  54. };
  55. JPush.addConnectEventListener(this.connectListener);
  56. //通知回调
  57. this.notificationListener = result => {
  58. console.log("notificationListener:" + JSON.stringify(result))
  59. };
  60. JPush.addNotificationListener(this.notificationListener);
  61. //本地通知回调
  62. this.localNotificationListener = result => {
  63. console.log("localNotificationListener:" + JSON.stringify(result))
  64. };
  65. JPush.addLocalNotificationListener(this.localNotificationListener);
  66. //自定义消息回调
  67. // this.customMessageListener = result => {
  68. // console.log("customMessageListener:" + JSON.stringify(result))
  69. // };
  70. // JPush.addCustomMessagegListener(this.customMessageListener);
  71. //tag alias事件回调
  72. this.tagAliasListener = result => {
  73. console.log("tagAliasListener:" + JSON.stringify(result))
  74. };
  75. JPush.addTagAliasListener(this.tagAliasListener);
  76. //手机号码事件回调
  77. this.mobileNumberListener = result => {
  78. console.log("mobileNumberListener:" + JSON.stringify(result))
  79. };
  80. JPush.addMobileNumberListener(this.mobileNumberListener);
  81. }
  82. render() {
  83. return (
  84. <View style={styles.container}>
  85. <Button title="setLoggerEnable"
  86. onPress={() => JPush.setLoggerEnable(true)
  87. }/>
  88. <Button title="getRegisterID"
  89. onPress={() => JPush.getRegistrationID(result =>
  90. console.log("registerID:" + JSON.stringify(result))
  91. )}/>
  92. {/*<Button title="addTags"
  93. onPress={() => JPush.addTags({sequence: 1, tags: ["1", "2", "3"]})}/>
  94. <Button title="updateTags"
  95. onPress={() => JPush.updateTags({sequence: 2, tags: ["4", "5", "6"]})}/>
  96. <Button title="deleteTag"
  97. onPress={() => JPush.deleteTag({sequence: 3, tags: ["4", "5", "6"]})}/>
  98. <Button title="deleteTags"
  99. onPress={() => JPush.deleteTags({sequence: 4})}/>
  100. <Button title="queryTag"
  101. onPress={() => JPush.queryTag({sequence: 4, tag: "1"})}/>
  102. <Button title="queryTags"
  103. onPress={() => JPush.queryTags({sequence: 5})}/>
  104. <Button title="setAlias"
  105. onPress={() => JPush.setAlias({sequence: 6,alias:"xxx"})}/>
  106. <Button title="deleteAlias"
  107. onPress={() => JPush.deleteAlias({sequence: 7})}/>
  108. <Button title="queryAlias"
  109. onPress={() => JPush.queryAlias({sequence: 8})}/>
  110. <Button title="setMobileNumber"
  111. onPress={() => JPush.setMobileNumber({mobileNumber: "13888888888"})}/>
  112. //仅ios
  113. <Button title="setBadge"
  114. onPress={() => JPush.setBadge({"badge":1,"appBadge":1})}/>
  115. <Button title="initCrashHandler"
  116. onPress={() => JPush.initCrashHandler()}/>
  117. <Button title="addLocalNotification"
  118. onPress={() => JPush.addLocalNotification({
  119. messageID: "123456789",
  120. title: "title123",
  121. content: "content123",
  122. extras: {"key123": "value123"}
  123. })}/>
  124. <Button title="removeLocalNotification"
  125. onPress={() => JPush.removeLocalNotification({messageID: '123456789'})}/>*/}
  126. </View>
  127. );
  128. }
  129. }

到此为止,安卓端在线消息推送配置成功。打开极光推送

发送消息,测试。消息发送成功。

二、厂商配置(华为手机)

目前我只配置了华为手机的。

文档中心

结合此文档进行配置。

1、重要---生成签名证书指纹

我是通过命令生成的签字证书指纹。

首先cd项目目录到andorid目录下执行命令(例如cd cd android/app)执行如下命令

  1. 1:keytool -genkey -alias monitor -keyalg RSA -storetype PKCS12 -validity 200000 -keysize 2500 -keystore monitor.keystore -v
  2. 2:keytool -importkeystore -srckeystore ./monitor.keystore -destkeystore ./monitor.jks -deststoretype JKS
  3. 3:keytool -list -v -keystore monitor.keystore

即可生成这两个文件

这个就是证书指纹

填这里。

按照官网一步步做完,每一步都需要,否则会失败。

具体配置代码如下

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2. buildscript {
  3. ext {
  4. buildToolsVersion = "33.0.0"
  5. minSdkVersion = 21
  6. compileSdkVersion = 33
  7. targetSdkVersion = 33
  8. // We use NDK 23 which has both M1 support and is the side-by-side NDK version from AGP.
  9. ndkVersion = "23.1.7779620"
  10. }
  11. repositories {
  12. google()
  13. mavenCentral()
  14. // 配置HMS Core SDK的Maven仓地址。
  15. maven { url 'https://developer.huawei.com/repo/' }
  16. maven { url 'https://repo.huaweicloud.com/repository/maven/'}
  17. }
  18. dependencies {
  19. classpath("com.android.tools.build:gradle:7.0.3")
  20. classpath("com.facebook.react:react-native-gradle-plugin")
  21. classpath ('com.huawei.agconnect:agcp:1.9.1.301')
  22. }
  23. allprojects {
  24. repositories {
  25. mavenCentral()
  26. maven {
  27. allowInsecureProtocol = true
  28. url 'http://developer.huawei.com/repo/'
  29. }
  30. mavenLocal()
  31. google()
  32. maven { url 'https://www.jitpack.io' }
  33. maven { url 'https://maven.google.com' }
  34. maven { url 'https://repo.huaweicloud.com/repository/maven/'}
  35. }
  36. }
  37. }

这一步keyAlias是生成指纹证书自己起的名字和设置的密码

此文件代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android">
  3. <uses-permission android:name="android.permission.INTERNET" />
  4. <application
  5. android:name=".MainApplication"
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:theme="@style/AppTheme">
  11. <service
  12. android:name="com.demo.HmsService"
  13. android:enabled="true"
  14. android:directBootAware="true"
  15. android:exported="false">
  16. <intent-filter>
  17. <action android:name="com.huawei.push.action.MESSAGING_EVENT"/>
  18. </intent-filter>
  19. </service>
  20. <activity
  21. android:name=".MainActivity"
  22. android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
  23. android:exported="true"
  24. android:label="@string/app_name"
  25. android:launchMode="singleTask"
  26. android:windowSoftInputMode="adjustResize">
  27. <intent-filter>
  28. <action android:name="android.intent.action.MAIN" />
  29. <category android:name="android.intent.category.LAUNCHER" />
  30. </intent-filter>
  31. </activity>
  32. <meta-data
  33. android:name="JPUSH_CHANNEL"
  34. android:value="${JPUSH_CHANNEL}" />
  35. <meta-data
  36. android:name="JPUSH_APPKEY"
  37. android:value="${JPUSH_APPKEY}" />
  38. </application>
  39. </manifest>

这个需要写,否则不成功。

implementation 'cn.jiguang.sdk.plugin:huawei:5.2.0'。

就可以了。

我关闭app后继续发送

成功了!!!!!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号