ucu*_*cup 1 android google-cloud-messaging react-native
每当我尝试从 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')
Run Code Online (Sandbox Code Playgroud)
我的 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>
Run Code Online (Sandbox Code Playgroud)
我很抱歉代码很长,请帮助我
好吧,我明白了。
首先,我没有添加是我的错
"com.google.android.gms:play-services-gcm:17.0.0"
Run Code Online (Sandbox Code Playgroud)
构建 gradle 时的依赖项。
然后我发现了另一个问题。
问题是 gcm 已被弃用。
2018 年 4 月 10 日,Google 弃用了 GCM。GCM 服务器和客户端 API 已于 2019 年 5 月 29 日删除,目前对这些 API 的任何调用都可能会失败。将 GCM 应用迁移到 Firebase Cloud Messaging (FCM),它继承了可靠且可扩展的 GCM 基础架构以及许多新功能。
然后我按照 ==> GCM Migrate to FCM 文档中的迁移指南进行操作
指南中写道我应该替换 build.gradle
这条线
"com.google.android.gms:play-services-gcm:17.0.0"
Run Code Online (Sandbox Code Playgroud)
到
"com.google.firebase:firebase-messaging:20.2.4"
Run Code Online (Sandbox Code Playgroud)
然后移动到 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" />
Run Code Online (Sandbox Code Playgroud)
和这个
<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" />
Run Code Online (Sandbox Code Playgroud)
之后通知运行良好,没有任何错误。
我希望这个问题可以帮助其他人。
| 归档时间: |
|
| 查看次数: |
1000 次 |
| 最近记录: |