android - Firebase 云消息服务无法实例化服务 FCM.MessagingService
问题描述
我按照官方文档在我的应用程序中包含了 Firebase 云消息传递服务和推送通知服务。但是,当我收到消息时,我的应用程序崩溃了
但是,我无法从我的服务器接收推送通知。错误日志如下所述。
错误信息:
Unable to instantiate service com.abc.myapp.MyFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.abc.myapp.MyFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/com.abc.myapp-2/base.apk"],nativeLibraryDirectories=[/data/app/com.abc.myapp-2/lib/arm64, /system/lib64, /vendor/lib64]]
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.myapp">
<!-- adding internet permission -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.wifi" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".QRScanner" />
<activity android:name=".ApproveLeaveActivity" />
<activity android:name=".AddLeaveActivity" />
<activity android:name=".LeaveActivity" />
<activity android:name=".CustInParking" />
<activity android:name=".SalaryActivity" />
<activity android:name=".AttendanceActivityDataHandler" />
<activity android:name=".AttendanceActivity" />
<activity
android:name=".LoginActivity"
android:label="@string/title_activity_login"
android:windowSoftInputMode="stateHidden" />
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_name" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="firebase_messaging_auto_init_enabled"
android:value="ture" />
<meta-data
android:name="firebase_analytics_collection_enabled"
android:value="true" />
</application>
</manifest>
MyFirebaseMessagingService:
package com.abc.myapp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.RingtoneManager;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import static android.content.ContentValues.TAG;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//showNotification(remoteMessage.getNotification().getBody());
Log.e(TAG, "From: " + remoteMessage.getFrom());
Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(token);
}
private void sendRegistrationToServer(String token) {
Log.d("TOKEN ", token);
}
public void showNotification(String message){
PendingIntent pi = PendingIntent.getActivity(this,0, new Intent(this, MainActivity.class),0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Suzuki Islamabad Motors")
.setContentText(message)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,notification);
}
}
Gradle 依赖项:
apply plugin: 'com.google.gms.google-services'
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-messaging:20.1.0'
解决方案
推荐阅读
- c# - 我怎样才能给 Dispatcher.Invoke 一个论点?
- terraform - Generate file with dynamic content with Terragrunt
- python - TypeError: object.__new__() 在 keras 序列中只采用一个参数(要实例化的类型)
- flutter - trie 实现在颤动中引发收缩器错误
- html - Css悬停和元素之间的空间
- c# - 如何将相同的服务与数据库或本地集合一起使用
- python-3.x - 怀疑pandas逐行过滤数据
- wordpress - 用户提示将平板电脑/iPad 仅限于横向模式
- javascript - JavaScript foreach 并在 json 中创建函数
- apache - 阿帕奇重定向网址