【问题标题】:react native app crash after sending notification from firebase via gcm通过 gcm 从 firebase 发送通知后对本机应用程序崩溃做出反应
【发布时间】:2020-08-15 04:32:36
【问题描述】:

每当我尝试从 firebase 发送通知时,我的应用就会崩溃。

然后我试图用 adb logcat 找出崩溃的原因。

gcm 依赖似乎有问题,我不知道错误来自哪里。

它说“无法实例化接收器 com.google.android.gms.gcm.GcmReceiver”

这是错误图片:

我的 build.gradle 依赖项:

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+"  // From node_modules
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation "com.google.android.gms:play-services-base:16.1.0"
implementation "com.google.firebase:firebase-core:16.0.9"
implementation "com.google.firebase:firebase-messaging:18.0.0"
implementation project(':react-native-push-notification')

我的 AndroidManifest.xml 看起来像这样:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.hidroponik">

    <uses-permission android:name="android.permission.INTERNET" />
    
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission
        android:name="com.hidroponik.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.hidroponik.permission.C2D_MESSAGE" />
    <!-- < Only if you're using GCM or localNotificationSchedule() > -->
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher"
      android:usesCleartextTraffic="true"
      android:allowBackup="false"
      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" />
      

      <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
                android:value="YOUR NOTIFICATION CHANNEL NAME"/>
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                    android:value="YOUR NOTIFICATION CHANNEL DESCRIPTION"/>
        <!-- Change the resource name to your App's accent color - or any other color you want -->
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                    android:resource="@android:color/white"/><!-- < Only if you're using GCM or localNotificationSchedule() > -->
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.hidroponik" />
            </intent-filter>
        </receiver>
        <!-- < Only if you're using GCM or localNotificationSchedule() > --><receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/><!-- < Only if you're using GCM or localNotificationSchedule() > -->
        <service
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerServiceGcm"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <!-- </ Only if you're using GCM or localNotificationSchedule() > --><!-- < Else > -->
        <service
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>


    </application>

</manifest>

很抱歉代码太长,请帮帮我

【问题讨论】:

    标签: android react-native google-cloud-messaging


    【解决方案1】:

    好吧,我想通了。

    首先,我不添加是我的错

    "com.google.android.gms:play-services-gcm:17.0.0"
    

    构建 gradle 中的依赖项。

    然后我发现了另一个问题。

    问题在于 gcm 已被弃用。

    2018 年 4 月 10 日,Google 弃用了 GCM。 GCM 服务器和客户端 API 已于 2019 年 5 月 29 日删除,目前对这些 API 的任何调用 API 可能会失败。将 GCM 应用迁移到 Firebase Cloud 消息传递(FCM),它继承了可靠且可扩展的 GCM 基础设施,以及许多新功能。

    然后我遵循迁移指南 ==> GCM Migrate to FCM documentation

    指南中写到我应该替换build.gradle

    这一行

    "com.google.android.gms:play-services-gcm:17.0.0"
    

    "com.google.firebase:firebase-messaging:20.2.4"
    

    然后转到我需要删除某个代码的 AndroidManifest.xml。 因为

    FCM SDK 会自动添加所有必需的权限以及 所需的接收器功能。

    这就是我从我的 AndroidManifest.xml 中删除的内容

    这个

    <uses-permission android:name="android.permission.WAKE_LOCK" />
        <permission
            android:name="com.hidroponik.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
        <uses-permission android:name="com.hidroponik.permission.C2D_MESSAGE" />
    

    还有这个

    <receiver
      android:name="com.google.android.gms.gcm.GcmReceiver"
      android:exported="true"
      android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      <category android:name="com.hidroponik" />
    

    之后通知运行良好,没有任何错误。

    我希望这个问题可以帮助其他人。

    【讨论】:

    • 它对我有用。谢谢
    • 很高兴这对您有帮助
    猜你喜欢
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多