React Native Notification Android

环境, 项目初始化, Firebase 配置的部分参照 IOS 的文档

copy google-services.json./android/app

修改 ./android/build.gradle

buildscript {
    ext {
        buildToolsVersion = "28.0.3"
        minSdkVersion = 16
        compileSdkVersion = 28
        targetSdkVersion = 28
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.4.2")
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

修改 ./android/app/build.gradle

project.ext.react = [
    entryFile: "index.js",
    enableHermes: false,  // clean and rebuild if changing
    bundleInRelease: true,
]
dependencies {
    implementation project(':react-native-firebase')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.facebook.react:react-native:+"  // From node_modules

    //Add these lines fcm
    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-messaging:18.0.0'
//    implementation 'com.google.firebase:firebase-messaging:20.0.0'

    compile(project(':react-native-firebase')) {
        transitive = false
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

文件最后添加

apply plugin: 'com.google.gms.google-services'

修改包名

修改 ./android/build.gradle

applicationId "com.gz.fcmtest"

修改 MainApplication.java 的文件路径为 com.gz.fcmtest

修改 MainApplication.java 的 package 为 package com.gz.fcmtest;

修改 MainActivity.java 的 package 为 package com.gz.fcmtest;

修改 MainActivity.java :

  protected String getMainComponentName() {
    return "com.gz.fcmtest";
  }

修改 app.json : "name": "com.gz.fcmtest"

修改 appDelegate.m : moduleName:@“com.gz.fcmtest”

修改 MainApplication.java

添加

  packages.add(new RNFirebaseMessagingPackage());
  packages.add(new RNFirebaseNotificationsPackage());

注释

//    initializeFlipper(this); // Remove this line if you don't want Flipper enabled

Android Studio 打开 android 目录, 并对项目进行构建

重构 react-native-firebase

安装 app 的时候 Android Studio 会提示错误, 根据错误进行修复

我用的 react-native-firebase5.6.0 其中引入的 androidx 是旧版本的, 所以需要重构其中的引用

例如 文件 ReactNativeFirebaseAppRegistrar.java 中的 import android.support.annotation.Keep; 改成 import androidx.annotation.Keep;

android.support.annotation. --> androidx.annotation.
android.support.v4.app. --> androidx.core.app.
android.support.v4.content. --> androidx.localbroadcastmanager.content.

启动 react-native 调试服务

react-native start

curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"

安装 app

react-native run-android

Android 的:

  1. 需要翻墙
  2. 需要安装 Google Service
  3. 不可以设置 Badge
  4. 到达率不是100%, 会丢失
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值