1
我在我的應用中實現了Firebase雲消息傳遞,以在重寫的onMessageReceived函數中執行代碼。當應用程序處於前臺而不是閒置/睡眠或後臺時,IT可以正常工作。我確實收到了通知托盤中的消息,但代碼無法運行。我知道當用戶點擊trey中的通知時,可以設置一個操作來運行代碼,但我希望它在沒有任何用戶交互的情況下運行。有沒有辦法讓這個運行或有一個更好的解決方案,FCM。在睡眠或在沒有用戶操作的後臺運行FireMessageReceived時運行Firebase雲消息
最終目標是從服務器ping設備(現在通過FCM令牌完成),然後立即讓設備上載設備位置。有沒有更好的辦法?
這是我的FCM代碼。
import android.content.Intent;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
import static android.content.ContentValues.TAG;
public class FMS extends FirebaseMessagingService {
public FMS() {
}
public static final String INTENT_FILTER = "Remote_Execute";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(INTENT_FILTER);
sendBroadcast(intent);
Map<String, String> data = remoteMessage.getData();
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
}
在您需要發送一個'data'- *僅*消息負載。請參閱[處理消息](https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages)文檔。 –
瞭解它與數據只工作謝謝。發現我無法在Firebase控制檯中發送只有數據的測試,因此我必須編寫一些PHP cURL代碼才能完成此操作。在開放和睡眠狀態下,信息都很好地通過。奇怪的行爲,雖然一切正常,當應用程序是開放的,但當應用程序關閉應用程序的一些功能關閉,如果在打開應用程序之前發送2消息應用程序凍結了一些時間。儘管我在logcat中看不到任何錯誤。我猜我沒有處理onMessageReceived函數中的消息。看着它。想法是受歡迎的。 – Aktyon