我在Android應用程序中使用FirebaseMessaging進行即時消息傳遞。FirebaseMessaging Android有些消息丟失
對於從服務器到設備Android的消息,不會丟失消息。
但是從移動到服務器一些消息丟失...
服務器接收ACK消息,但在其他方向(移動到服務器)移動不接收ACK。
其結果是大約30%的消息丟失。如果我快速發送多條消息,會有更多的損失(40-60%)。如果我緩慢地發送多個消息,則有10%的消息丟失...
當我將消息發送到服務器時,我注意到在我的FirebaseMessagingService中,方法「onMessageSent」被10個消息模糊地調用。這是正常的嗎? 爲什麼 「onMessageSent」 不是一次,只要消息被髮送稱爲...
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
/** constante pour renvoyer la position GPS */
public static final String BROADCAST_FIREBASE_FILTER= "android.intent.action.MY_APP_FIREBASE";
private DatabaseHelper helper;
@Override
public void onCreate() {
super.onCreate();
}
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Notification method below.
DebugLog.logw(TAG,"MESSAGE RECEIVED");
String clickAction = remoteMessage.getNotification().getClickAction();
[..]
}
// [END receive_message]
/**
* Create and show a simple notification containing the received FCM message.
*
* @param messageBody FCM message body received.
*/
private void sendNotification(String title, String messageBody,Intent intent2) {
intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent2,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
DebugLog.logi(TAG, "onDeletedMessages");
}
@Override
public void onMessageSent(String s) {
super.onMessageSent(s);
DebugLog.logi(TAG, "onMessageSent = " + s);
}
@Override
public void onSendError(String s, Exception e) {
super.onSendError(s, e);
DebugLog.logi(TAG, "onSendError = " + s);
}
}
我在我的FragmentMessaging方法發送消息:
public void sendMessage(Message msg){
DebugLog.logd("FragmentMessagerie","sendMessage msg = " + msg.getMsg());
if(textMessage.getText().length()>0) {
final Message message = msg;
AtomicInteger atomicInteger = new AtomicInteger();
FirebaseMessaging fm = FirebaseMessaging.getInstance();
RemoteMessage rm = new RemoteMessage.Builder(senderID)
.setMessageId("myApp_" + atomicInteger.incrementAndGet() + System.currentTimeMillis())
.addData("action", "chat")
.addData("destinataire", String.valueOf(contact.getId()))
.addData("emetteur",username)
.addData("texte",msg.getMsg())
.setTtl(0)
.build();
fm.send(rm); // appel à fireBase pour l'envoi du message
[..]
textMessage.setText("");
} else {
[..]
}
}
}
我的問題是:
當發送消息(firebaseMessaging.send(remoteMessage))Firebase應該處理向服務器發送消息。
Android是否將消息錯誤地發送到Firebase?
還是Firebase將消息嚴重地發送到我的服務器?
在服務器上,我收到發送給手機的ACK消息。
在我的設備Android上,我沒有收到ACK消息,當發送10條消息時,只有方法「onMessageSent」被調用了10次......這是正常的嗎?
如何才能收到ACK消息設備Android到服務器?從我的設備?
在此先感謝您的幫助!