问题描述: 在Android应用程序中,使用Firebase Cloud Messaging (FCM)来发送和接收推送通知。但是,在启动服务并接收响应时遇到了问题。
解决方法:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// 处理接收到的消息
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// 处理数据部分
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
// 处理通知部分
}
}
}
implementation 'com.google.firebase:firebase-messaging:20.2.4'
classpath 'com.google.gms:google-services:4.3.8'
apply plugin: 'com.google.gms.google-services'
这些解决方法应该能够帮助您解决Android FCM无法启动服务并接收响应的问题。如果问题仍然存在,请检查日志以获取更多详细信息,并确保您的代码没有其他错误。
下一篇:Android 分级/嵌套复选框