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

时间:2016-09-03 15:05:55

标签: android firebase firebase-cloud-messaging firebase-notifications

我正在尝试设置Android手机接收来自服务器的通知的系统:对于此任务,我们选择了Firebase。 虽然应用程序在后台运行,但一切正常:在我从firebase控制台推送消息后,系统托盘上会显示一条消息,并且在用户单击消息后,会向Intent发送带有额外数据的消息。 Activity

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

  • 我们正在使用Android Studio 2.1.2;
  • " it.bagozi.ada.tutorial.firebase"包含两个类(正如您可能从pacakge声明中推断出来的那样);

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

主要活动

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>

1 个答案:

答案 0 :(得分:5)

使清单中的服务声明成为&#34;应用程序&#34;的一个孩子。标记!