问题描述
每当我尝试从Firebase发送通知时,我的应用崩溃。
然后我想通过adb logcat找出崩溃的原因。
gcm依赖项似乎有问题,我不知道erorr的来源。
它说“无法实例化接收器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。
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" />
之后,通知运行良好,没有任何错误。
我希望这个问题可以帮助其他人。