我正在从 GCM 切换到 FCM。我只用它通过 FCM 向我的服务器发送消息。我扩展FirebaseMessagingService
以实现回调。例如,onSendError()
工作得很好,但从onMessageSent()
不执行......我不知道它是否相关,但我在收到消息时从我的服务器发送“ack”节。
我的 FirebaseMessegingService 类:
public class FCMListenerService extends FirebaseMessagingService {
public FCMListenerService() {
super();
}
@Override
public void onDeletedMessages() {
Log.d("wouter", "onDeletedMessages: ");
super.onDeletedMessages();
}
@Override
public void onMessageSent(String msgID) {
Log.e("wouter", "##########onMessageSent: " + msgID );
super.onMessageSent(msgID);
Database database = new Database(this);
database.confirmFCMMessageSend(msgID);
database.closeDatabase();
}
@Override
public void onSendError(String msgID, Exception exception) {
Log.e("wouter", "onSendError ", exception );
super.onSendError(msgID, exception);
Database database = new Database(this);
database.eraseFCMMsgIDFromFGMMessageLog(msgID);
database.closeDatabase();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
}
}
我的清单的相关部分:
<service android:name=".FCM.FCMListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>