0

按照 Firebase dev docs中的说明,我实现了一个扩展 FirebaseMessagingService 并覆盖 onMessageReceived 回调的服务。我在 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
    }
}
4

2 回答 2

2

实际上,应用程序的行为,在接收包括通知和数据负载在内的消息时,取决于应用程序是在后台还是前台,例如:

在后台时,应用程序在通知托盘中接收通知负载,并且仅在用户点击通知时处理数据负载。

在前台时,您的应用程序会收到一个带有两个有效负载的消息对象。

因此,摘要是当应用程序处于后台时,您可以在系统托盘中看到通知,并且在点击通知之前看不到任何日志,但您只会看到打开活动日志而不是服务日志,因为它已经执行。

当应用程序在前台时,您可以在 logcat 中看到日志,但在系统托盘中看不到任何通知,因为您的应用程序已经打开状态,您只会收到数据有效负载。

于 2016-08-11T07:27:43.243 回答
1

这是一个如何接收消息以及如何处理不同类型的代码示例。这是代码的来源

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

}
于 2017-03-02T13:56:30.197 回答