通过 gcm 从 firebase 发送通知后反应本机应用程序崩溃
react native app crash after sending notification from firebase via gcm
每当我尝试从 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>
抱歉,代码太长了,请帮帮我
好的,我明白了。
首先,我没有添加
是我的错
"com.google.android.gms:play-services-gcm:17.0.0"
构建时的依赖项 gradle。
之后又发现了一个问题
问题是 gcm 已被弃用。
On April 10, 2018, Google deprecated GCM. The GCM server and client
APIs were removed on May 29, 2019, and currently any calls to those
APIs can be expected to fail. Migrate GCM apps to Firebase Cloud
Messaging (FCM), which inherits the reliable and scalable GCM
infrastructure, plus many new features.
然后我按照迁移指南进行操作 ==> 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 我需要删除某个代码。
因为
The FCM SDK automatically adds all required permissions as well as the
required receiver funtionality.
这就是我从 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" />
之后,通知工作正常,没有任何错误。
希望这个问题可以帮助到其他人
每当我尝试从 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>
抱歉,代码太长了,请帮帮我
好的,我明白了。
首先,我没有添加
是我的错"com.google.android.gms:play-services-gcm:17.0.0"
构建时的依赖项 gradle。
之后又发现了一个问题
问题是 gcm 已被弃用。
On April 10, 2018, Google deprecated GCM. The GCM server and client APIs were removed on May 29, 2019, and currently any calls to those APIs can be expected to fail. Migrate GCM apps to Firebase Cloud Messaging (FCM), which inherits the reliable and scalable GCM infrastructure, plus many new features.
然后我按照迁移指南进行操作 ==> 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 我需要删除某个代码。 因为
The FCM SDK automatically adds all required permissions as well as the required receiver funtionality.
这就是我从 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" />
之后,通知工作正常,没有任何错误。
希望这个问题可以帮助到其他人