【问题标题】:Notifications from firebase when app is in foreground not called当应用程序处于前台时来自firebase的通知未调用
【发布时间】:2016-09-03 15:05:54
【问题描述】:

我正在尝试设置一个系统,让 Android 手机从服务器接收通知:对于这项任务,我们选择了 Firebase。 当应用程序在后台时,一切正常:在我从 firebase 控制台推送消息后,系统托盘上会出现一条通知,并且在用户单击消息后,带有额外数据的 Intent 将发送到 @987654323 @。

当应用程序已经在前台时,我的问题是独一无二的。 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>

【问题讨论】:

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


    【解决方案1】:

    使清单中的服务声明成为“应用程序”标签的子级!

    【讨论】:

    • 令人难以置信的是,另一双眼睛竟然能在这么短的时间内解决问题!刚刚尝试并完美地工作!
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2019-07-02
    • 2021-12-13
    相关资源
    最近更新 更多