当应用程序处于前台时,来自 firebase 的通知未被调用

Notifications from firebase when app is in foreground not called

我正在尝试设置一个系统,其中 Android 移动设备 phone 从服务器接收通知:为此我们选择了 Firebase。 当应用程序在后台时,一切正常:在我从 firebase 控制台推送一条消息后,系统托盘上会出现一条通知,在用户单击该消息后,带有额外数据的 Intent 会发送到Activity.

当应用程序已经在前台时,我的问题是独一无二的。 firebase notification 非常清楚:如果应用程序在前台,无论服务器发送哪种类型的消息,都不会调用 OnMessageReceived...那么为什么我的简单应用程序失败了?帮助您解决此问题的一些注意事项:

您可以在下面找到所有使用的源代码:

主要Activity

package it.bagozi.ada.tutorial.firebase;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private TextView notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.notification = (TextView) this.findViewById(R.id.notification_setter);

        Intent i = this.getIntent();
        Log.e(TAG, "Intent from Activity caller: " + i);

        if (getIntent().getExtras() != null) {
            for (String key : getIntent().getExtras().keySet()) {
                String value = getIntent().getExtras().getString(key);
                Log.d(TAG, "Key: " + key + " Value: " + value);
            }
        }

    }
}

FirebaseNotificationService

package it.bagozi.ada.tutorial.firebase;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FirebaseNotificationService extends FirebaseMessagingService {

    private static final String TAG = FirebaseNotificationService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        Log.e(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Message data payload: " + remoteMessage.getData());
        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }


}

Android 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.bagozi.ada.tutorial.firebase">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

使清单中的服务声明成为 "application" 标记的子标记!