应用程序在前台时不会触发onMessageReceived [英] onMessageReceived not fired when app in foreground

查看:72
本文介绍了应用程序在前台时不会触发onMessageReceived的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注教程,以在我的android设备上接收推送通知.

I'm following this tutorial to receive push notifications on my android device.

我可以从Firebase控制台发送推送通知-我还可以在logcat中看到Firebase事件,但是我的onMessageReceived从未触发过.我的应用程序在前台运行.

I can send push notifications from the Firebase console - and I can also see the Firebase event in logcat but my onMessageReceived is never fired. My app is running in the foreground.

这是我发送通知后logcat中的输出:

Here is the output in logcat after I send a notification:

12-31 12:23:54.741 9453-11132/com.example.app D/FA: Logging event (FE): notification_receive(_nr), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=407943440756026938, message_device_time(_ndt)=0, message_name(_nmn)=Label Arse, message_time(_nmt)=1514723034, message_id(_nmid)=3115372290763926350}]
12-31 12:23:54.784 9453-11132/com.example.app V/FA: Connecting to remote service
12-31 12:23:54.797 9453-11132/com.example.app D/FA: Logging event (FE): notification_foreground(_nf), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=407943440756026938, message_device_time(_ndt)=0, message_name(_nmn)=Label Arse, message_time(_nmt)=1514723034, message_id(_nmid)=3115372290763926350}]
12-31 12:23:54.828 9453-11132/com.example.app V/FA: Connection attempt already in progress
12-31 12:23:54.829 9453-11132/com.example.app D/FA: Connected to remote service
12-31 12:23:54.829 9453-11132/com.example.app V/FA: Processing queued up service tasks: 2
12-31 12:23:59.875 9453-11132/com.example.app V/FA: Inactivity, disconnecting from the service

MyFirebaseMessageingService.java

MyFirebaseMessageingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
}

FirebaseIDService.java

FirebaseIDService.java

public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

AndroidManifest.xml:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app">

    <uses-permission android:name="android.permission.INTERNET" />

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

        <service
            android:name=".services.MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="true" />
        <service
            android:name=".services.FirebaseIDService"
            android:enabled="true"
            android:exported="true"></service>
    </application>
</manifest>

build.gradle

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            java.srcDirs = ['src/main/java', 'src/main/java/services']
        }
    }
}

repositories {
    maven { url "https://jitpack.io" }
}

dependencies {
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-messaging:11.8.0'
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.volley:volley:1.1.0'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.github.PhilJay:MPAndroidChart:v2.1.6'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:25.4.0'
    compile 'com.google.firebase:firebase-appindexing:11.8.0'
    compile files('libs/glide-full-4.3.1.jar')
}


apply plugin: 'com.google.gms.google-services'

推荐答案

,按照Google firebase邮件指南

as per google firebase messaging guide

尝试将清单文件中的服务标签替换为:

try replacing your service tag in the manifest file with :

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

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

在onMessageReceived()中在此处引用,您可以获得通知/数据有效载荷:

in the onMessageReceived() refer here you can get the notification / data payload :

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        }

        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " +
                    remoteMessage.getNotification().getBody());
        }

    }

}

这篇关于应用程序在前台时不会触发onMessageReceived的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆