簡體   English   中英

收不到firebase雲消息,react--native-firebase

[英]Unable to receive firebase cloud message, react--native-firebase

我無法接收使用 Firebase 雲消息發送的消息。

我做了什么

  1. 克隆的 react-native-firebase-starter套件。
    git clone https://github.com/invertase/react-native-firebase-starter.git

  2. 生成 google-services.json 文件並將其放置在android/app

  3. react-native 運行-android
  4. 構建失敗並出現錯誤

    失敗:構建失敗,出現異常。

    • 出了什么問題:任務 ':app:transformClassesWithMultidexlistForDebug' 執行失敗。

      com.android.build.api.transform.TransformException:生成主 dex 列表時出錯。

  5. 將 RNFirebase 依賴項版本從 15.0.1 更改為 12.0.1 例如
    implementation "com.google.firebase:firebase-messaging:15.0.0"
    implementation "com.google.firebase:firebase-messaging:12.0.1"

  6. react-native 運行-android
    構建成功但有警告

    Could not find google-services.json while looking in [src/nullnull/debug, src/debug/nullnull, src/nullnull, src/debug, src/nullnullDebug] registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection) registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection) Could not find google-services.json while looking in [src/nullnull/release, src/release/nullnull, src/nullnull, src/release, src/nullnullRelease] registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
  7. 我仍然沒有收到來自 Firebase 控制台的任何消息。

  8. 我在 這里上傳了相同的存儲庫以及 google-services.json 文件

我已經嘗試過什么?

  1. 從警告中讀取我創建了android/app/src/releaseandroid/app/src/debug並在其中放置了google-services.json文件。 警告消失但消息不走運。

  2. 我還嘗試創建一個虛擬項目並遵循react-native-firebase文檔,手動集成,然后是特定於平台的 (android)
    Could not find google-services.json while looking in [src/nullnull/debug, src/debug/nullnull, src/nullnull, src/debug, src/nullnullDebug]
    虛擬項目結帳中分支attemp3/add-firebase firebase 以查看 firebase 代碼。
    git checkout attemp3/add-firebase

對於 react-native-firebase-starter

google-services.json

{
      "project_info": {
        "project_number": "985473870159",
        "firebase_url": "https://rnexampleproject.firebaseio.com",
        "project_id": "rnexampleproject",
        "storage_bucket": "rnexampleproject.appspot.com"
      },
      "client": [
        {
          "client_info": {
            "mobilesdk_app_id": "1:985473870159:android:8d4e41f333ad27fa",
            "android_client_info": {
              "package_name": "com.invertase.rnfirebasestarter"
            }
          },
          "oauth_client": [
            {
              "client_id": "985473870159-n397khjlfd8avu751vtqifctv1tggkf0.apps.googleusercontent.com",
              "client_type": 1,
              "android_info": {
                "package_name": "com.invertase.rnfirebasestarter",
                "certificate_hash": "bb9a16aed3785f6d36b453648e50b82aa885c693"
              }
            },
            {
              "client_id": "985473870159-u7p4nnujgedhcbov4k2rgb4nrh85tk8v.apps.googleusercontent.com",
              "client_type": 3
            }
          ],
          "api_key": [
            {
              "current_key": "AIzaSyBQJNkpMAh3-reDRBbHmK9AmLd5ykVy2vM"
            }
          ],
          "services": {
            "analytics_service": {
              "status": 1
            },
            "appinvite_service": {
              "status": 2,
              "other_platform_oauth_client": [
                {
                  "client_id": "985473870159-u7p4nnujgedhcbov4k2rgb4nrh85tk8v.apps.googleusercontent.com",
                  "client_type": 3
                }
              ]
            },
            "ads_service": {
              "status": 2
            }
          }
        }
      ],
      "configuration_version": "1"
    }


android/build.gradle

apply plugin: "com.android.application"
apply plugin: "io.fabric"

import com.android.build.OutputFile

/**
* The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
* and bundleReleaseJsAndAssets).
* These basically call `react-native bundle` with the correct arguments during the Android build
* cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
* bundle directly from the development server. Below you can see all the possible configurations
* and their defaults. If you decide to add a configuration block, make sure to add it before the
* `apply from: "../../node_modules/react-native/react.gradle"` line.
*
* project.ext.react = [
*   // the name of the generated asset file containing your JS bundle
*   bundleAssetName: "index.android.bundle",
*
*   // the entry file for bundle generation
*   entryFile: "index.android.js",
*
*   // whether to bundle JS and assets in debug mode
*   bundleInDebug: false,
*
*   // whether to bundle JS and assets in release mode
*   bundleInRelease: true,
*
*   // whether to bundle JS and assets in another build variant (if configured).
*   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
*   // The configuration property can be in the following formats
*   //         'bundleIn${productFlavor}${buildType}'
*   //         'bundleIn${buildType}'
*   // bundleInFreeDebug: true,
*   // bundleInPaidRelease: true,
*   // bundleInBeta: true,
*
*   // whether to disable dev mode in custom build variants (by default only disabled in release)
*   // for example: to disable dev mode in the staging build type (if configured)
*   devDisabledInStaging: true,
*   // The configuration property can be in the following formats
*   //         'devDisabledIn${productFlavor}${buildType}'
*   //         'devDisabledIn${buildType}'
*
*   // the root of your project, i.e. where "package.json" lives
*   root: "../../",
*
*   // where to put the JS bundle asset in debug mode
*   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
*
*   // where to put the JS bundle asset in release mode
*   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
*
*   // where to put drawable resources / React Native assets, e.g. the ones you use via
*   // require('./image.png')), in debug mode
*   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
*
*   // where to put drawable resources / React Native assets, e.g. the ones you use via
*   // require('./image.png')), in release mode
*   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
*
*   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
*   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
*   // date; if you have any other folders that you want to ignore for performance reasons (gradle
*   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
*   // for example, you might want to remove it from here.
*   inputExcludes: ["android/**", "ios/**"],
*
*   // override which node gets called and with what additional arguments
*   nodeExecutableAndArgs: ["node"],
*
*   // supply additional arguments to the packager
*   extraPackagerArgs: []
* ]
*/

project.ext.react = [
    entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"

/**
* Set this to true to create two separate APKs instead of one:
*   - An APK that only works on ARM devices
*   - An APK that only works on x86 devices
* The advantage is the size of the APK is reduced by about 4MB.
* Upload all the APKs to the Play Store and people will download
* the correct one based on the CPU architecture of their device.
*/
def enableSeparateBuildPerCPUArchitecture = false

/**
* Run Proguard to shrink the Java bytecode in release builds.
*/
def enableProguardInReleaseBuilds = false

android {
    // because of firestore:
    dexOptions {
        javaMaxHeapSize "4g"
    }
    compileSdkVersion 27

    defaultConfig {
        applicationId "com.invertase.rnfirebasestarter"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
        // because of firestore:
        multiDexEnabled true
    }    {
      "project_info": {
        "project_number": "985473870159",
        "firebase_url": "https://rnexampleproject.firebaseio.com",
        "project_id": "rnexampleproject",
        "storage_bucket": "rnexampleproject.appspot.com"
      },
      "client": [
        {
          "client_info": {
            "mobilesdk_app_id": "1:985473870159:android:8d4e41f333ad27fa",
            "android_client_info": {
              "package_name": "com.invertase.rnfirebasestarter"
            }
          },
          "oauth_client": [
            {
              "client_id": "985473870159-n397khjlfd8avu751vtqifctv1tggkf0.apps.googleusercontent.com",
              "client_type": 1,
              "android_info": {
                "package_name": "com.invertase.rnfirebasestarter",
                "certificate_hash": "bb9a16aed3785f6d36b453648e50b82aa885c693"
              }
            },
            {
              "client_id": "985473870159-u7p4nnujgedhcbov4k2rgb4nrh85tk8v.apps.googleusercontent.com",
              "client_type": 3
            }
          ],
          "api_key": [
            {
              "current_key": "AIzaSyBQJNkpMAh3-reDRBbHmK9AmLd5ykVy2vM"
            }
          ],
          "services": {
            "analytics_service": {
              "status": 1
            },
            "appinvite_service": {
              "status": 2,
              "other_platform_oauth_client": [
                {
                  "client_id": "985473870159-u7p4nnujgedhcbov4k2rgb4nrh85tk8v.apps.googleusercontent.com",
                  "client_type": 3
                }
              ]
            },
            "ads_service": {
              "status": 2
            }
          }
        }
      ],
      "configuration_version": "1"
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

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

    // RNFirebase required dependencies
    implementation "com.google.firebase:firebase-core:12.0.1"
    implementation "com.google.android.gms:play-services-base:12.0.1"

    // RNFirebase optional dependencies
    implementation "com.google.firebase:firebase-ads:12.0.1"
    implementation "com.google.firebase:firebase-auth:12.0.1"
    implementation "com.google.firebase:firebase-config:12.0.1"
    implementation "com.google.firebase:firebase-database:12.0.1"
    implementation "com.google.firebase:firebase-invites:12.0.1"
    implementation "com.google.firebase:firebase-firestore:12.0.1"
    implementation "com.google.firebase:firebase-messaging:12.0.1"
    implementation "com.google.firebase:firebase-perf:12.0.1"
    implementation "com.google.firebase:firebase-storage:12.0.1"

    implementation('com.crashlytics.sdk.android:crashlytics:2.9.1@aar') {
        transitive = true
    }

    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:27.0.2"
    implementation "com.facebook.react:react-native:+"  // From node_modules
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

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

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.invertase.rnfirebasestarter"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    <application
      android:name=".MainApplication"
      android:allowBackup="true"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
      <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
        <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
      </service>
      <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
        <intent-filter>
          <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
      </service>
    </application>

</manifest>

附加信息

平台 - android
節點版本 v8.6.0
npm 版本 5.3.0

謝謝!

我試過了,它奏效了:

  1. git 克隆https://github.com/invertase/react-native-firebase-starter.git
  2. 將您的 google-services.json 復制到 android/app
  3. 安裝
  4. react-native run-android

構建通過,應用程序加載到模擬器上。 我在 MAC 上。 我的 npm 版本是 5.7.1,節點版本是 9.8.0

您是否嘗試使用通道向令牌發送推送? 對我來說,我將發布請求發送到https://fcm.googleapis.com/fcm/send :有效載荷是:

{
 "to" : "ddUYveNp4QE:...",
 "notification" : {
 "body" : "un nouveau partage",
 "title" : "un partage de contact reçu",
 "priority" : "high",
 "vibration":true,
 "sound": "Enabled",
 "badge":1,
 "requireInteraction": true,
 "click_action": "fcm.ACTION.HELLO"
 },
 "data" : {
 "body" : "nouvelle opportunité",
 "title" : "un nouveau partage",
 "content_available" : true,
 "priority" : "high"
 } 
}

您必須將 google-services.json 文件放在 android/app 文件夾中我認為您的問題是您將 google-services.json 代碼放在 build.gradle 文件中,這是不正確的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM