0

按照Firebase dev docs的指示,我已經實現了一個擴展FirebaseMessagingService並覆蓋onMessageReceived回調的服務。我在onMessageReceived方法的第一行放置了一條日誌消息。Firebase雲消息傳遞onMessageReceived沒有被觸發

應用程序運行在後臺 我沒有在日誌中看到logcat的,但我看到張貼在系統試的通知。

應用前景 我不能看日誌也沒有在系統托盤中的通知

任何想法是怎麼回事?

艙單

<service 
     android:name=".fcm.MovieMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 

服務類

public class MovieMessagingService extends FirebaseMessagingService { 

    private static final String LOG_TAG = MovieMessagingService.class.getSimpleName(); 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 


     Log.d(LOG_TAG, "From: " + remoteMessage.getFrom()); 

    } 

    /** 
    * Create and show a simple notification containing the received FCM message. 
    * 
    * @param messageBody FCM message body received. 
    */ 
    private void sendNotification(String messageBody) { 
     Log.d(LOG_TAG, "Presenting Notification with message body: " + messageBody); 
//more code 
    } 
} 
+0

添加服務類的代碼和代碼清單的問題 – Shubhank

+0

是路徑正確的服務(包名) – Shubhank

+0

是,其他應用程序甚至不會編譯。如您所見,我在清單中使用相對路徑 - 「.fcm.MovieMessagingService」 – Sai

回答

2

實際上,應用程序的行爲,而接收消息既包括通知和數據有效載荷,取決於該應用是否是在背景或前景等:

當在後臺時,應用程序會在通知托盤中收到通知有效內容,並且只在用戶ta時處理數據有效內容ps通知。

當處於前臺時,您的應用程序會收到一個消息對象,並且兩個負載均已附加。

因此,總結是,當應用程序在後臺,你可以看到在系統托盤中的通知,不能看到任何日誌,直到攻的通知,但你會看到開場僅活動日誌不是服務日誌中它已經被執行。

而當前臺應用程序中您可以看到logcat中的日誌,但在系統托盤中看不到任何通知,因爲您的應用程序已經打開狀態,您將只收到數據有效載荷。

1

以下是如何接收消息以及如何處理不同類型的代碼示例。這裏是代碼的source

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

/** 
* 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) { 
    // [START_EXCLUDE] 
    // There are two types of messages data messages and notification messages. Data messages are handled 
    // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type 
    // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app 
    // is in the foreground. When the app is in the background an automatically generated notification is displayed. 
    // When the user taps on the notification they are returned to the app. Messages containing both notification 
    // and data payloads are treated as notification messages. The Firebase console always sends notification 
    // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options 
    // [END_EXCLUDE] 

    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()); 
    } 

} 
相關問題