Android

安卓端配置文档地址

https://dev.yunxin.163.com/docs/product/IM%E5%8D%B3%E6%97%B6%E9%80%9A%E8%AE%AF/SDK%E5%BC%80%E5%8F%91%E9%9B%86%E6%88%90/Web%E5%BC%80%E5%8F%91%E9%9B%86%E6%88%90/ReactNative

核心示例代码:

  1. // iOS/安卓端外推送代码
  2. const iosPushConfig = {
  3. tokenName: 'push_online',
  4. };
  5. const androidPushConfig = {
  6. xmAppId: '2882303761517806219',
  7. xmAppKey: '5971780672219',
  8. xmCertificateName: 'RN_MI_PUSH',
  9. hwCertificateName: 'RN_HW_PUSH',
  10. mzAppId: '113798',
  11. mzAppKey: 'b74148973e6040c6abbda2af4c2f6779',
  12. mzCertificateName: 'RN_MZ_PUSH',
  13. fcmCertificateName: 'RN_FCM_PUSH',
  14. vivoCertificateName: "vivo证书名",
  15. oppoAppId: "xxx", // oppoAppId, oppoAppKey, oppoAppSercet 在oppo推送平台注册得到
  16. oppoAppKey: "xxx",
  17. oppoAppSercet: "xxx",
  18. oppoCertificateName: "oppo证书名"
  19. };
  20. var nim = SDK.NIM.getInstance({
  21. // ...
  22. iosPushConfig,
  23. androidPushConfig,
  24. // ...
  25. })
  26. // 安卓端内推送示例代码
  27. import { showNotification } from '../nim/NIM_Android_Push';
  28. showNotification({
  29. icon: '', title: msg.from, content: showText, time: `${msg.time}`,
  30. });

其中各个平台的 certificateName 是在网易云信后台添加的证书名称:
示例如下图:
网易云信后台访问地址

网易云信安卓端各大厂商推送配置文档:

https://dev.yunxin.163.com/docs/product/IM%E5%8D%B3%E6%97%B6%E9%80%9A%E8%AE%AF/SDK%E5%BC%80%E5%8F%91%E9%9B%86%E6%88%90/Android%E5%BC%80%E5%8F%91%E9%9B%86%E6%88%90/%E6%8E%A8%E9%80%81?#OPPO%E6%8E%A8%E9%80%81

安卓端各个平台推送是建立在app审核通过后,推送是需要额外配置开启功能的,厂商推送是为了在杀掉进程时由厂商发起推送,如果只是切换到后台时接到消息通知,不需要接厂商推送

Android 手动配置

请在 app gradle 文件 dependencies 添加此模块。

  • nimpush文件夹拷贝到android文件夹下

  • android/setting.gradle

    1. // 添加
    2. include ':nimpush'
  • android/app/build.gradle

    1. dependencies {
    2. // 添加
    3. implementation project(':nimpush')
    4. }
  • android/app/src/main/java/com/app/MainApplication.java

  1. // 添加
  2. import com.netease.nim.rn.push.NIMPushPackage;
  3. // ...
  4. import com.facebook.react.ReactNativeHost;
  5. import com.facebook.react.ReactPackage;
  6. // ...
  7. @Override
  8. protected List<ReactPackage> getPackages() {
  9. return Arrays.<ReactPackage>asList(
  10. // ...
  11. new MainReactPackage(),
  12. new RealmReactPackage(),
  13. // 添加
  14. new NIMPushPackage()
  15. );
  16. }

android/app/src/AndroidManifest.xml 配置

  • 权限部分

    1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    2. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    3. <uses-permission android:name="android.permission.INTERNET" />
    4. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    5. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    6. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    7. <uses-permission android:name="android.permission.GET_TASKS" />
    8. <uses-permission android:name="android.permission.VIBRATE"/>
    9. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  • 小米推送

自定义权限

  1. <permission
  2. android:name="你的应用包名.permission.MIPUSH_RECEIVE"
  3. android:protectionLevel="signature" />
  4. <uses-permission android:name="你的应用包名.permission.MIPUSH_RECEIVE" />

推送服务和广播

  1. <service
  2. android:name="com.xiaomi.push.service.XMJobService"
  3. android:enabled="true"
  4. android:exported="false"
  5. android:permission="android.permission.BIND_JOB_SERVICE"
  6. android:process=":mixpush" />
  7. <service
  8. android:name="com.xiaomi.push.service.XMPushService"
  9. android:enabled="true"
  10. android:process=":mixpush" />
  11. <receiver
  12. android:name="com.xiaomi.push.service.receivers.PingReceiver"
  13. android:exported="false"
  14. android:process=":mixpush">
  15. <intent-filter>
  16. <action android:name="com.xiaomi.push.PING_TIMER" />
  17. </intent-filter>
  18. </receiver>
  19. <receiver
  20. android:name="com.xiaomi.push.service.receivers.NetworkStatusReceiver"
  21. android:exported="true">
  22. <intent-filter>
  23. <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  24. <category android:name="android.intent.category.DEFAULT" />
  25. </intent-filter>
  26. </receiver>
  27. <service
  28. android:name="com.xiaomi.mipush.sdk.PushMessageHandler"
  29. android:enabled="true"
  30. android:exported="true" />
  31. <service
  32. android:name="com.xiaomi.mipush.sdk.MessageHandleService"
  33. android:enabled="true" />
  34. <receiver
  35. android:name="com.netease.nimlib.mixpush.mi.MiPushReceiver"
  36. android:exported="true">
  37. <intent-filter android:priority="0x7fffffff">
  38. <action android:name="com.xiaomi.mipush.RECEIVE_MESSAGE" />
  39. <action android:name="com.xiaomi.mipush.MESSAGE_ARRIVED" />
  40. <action android:name="com.xiaomi.mipush.ERROR" />
  41. </intent-filter>
  42. </receiver>

其他推送配置参照 android/nimpush/readme.md 文件配置

文档更新时间: 2021-08-03 09:57