FCM:onMessageReceived未被调用,即使发送消息到fcm也没有收到通知?

9

我正在开发一个应用程序,希望通过php实现FCM推送通知。

因此,我创建了两个java文件: 1.FirebaseInstanceID(在数据库中正常运行并获得令牌) 2.FirebaseMessagingSerivice(未被调用)

我的FirebaseMessagingService.java

package com.example.xyz;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.RemoteMessage;

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message) {

    Intent i = new Intent(this,Dashboard.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("FCM Test")
            .setContentText(message)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0,builder.build());
}
}

运行我的php脚本发送消息到FCM时的结果:

 {\"multicast_id\":7077449602201888040,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1465110073498648%d215149df9fd7ecd\"}]}

但是我仍然没有收到任何通知,所有配置,如 API 密钥、包名称(在 FCM 控制台中,我的项目)都已经检查过了,并且它们都是正确的。
当我在 FirebaseMessagingService.java 中的 onMessageReceived() 创建断点进行调试时,它并没有进入该方法,当消息通过 PHP 脚本推送到 FCM 服务器时,应用程序会正常运行。
我的 Dashboard.java。
public class Dashboard extends AppCompatActivity {

private Toolbar toolbar;
private DrawerLayout drawer_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    toolbar=(Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawer_layout=(DrawerLayout) findViewById(R.id.drawer_layout);
    NavigationDrawerFragment drawerfragment= (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerfragment.setUp(R.id.fragment_navigation_drawer,drawer_layout,toolbar);


    FirebaseMessaging.getInstance().subscribeToTopic("test");
    FirebaseInstanceId.getInstance().getToken();

}

但是我的仪表板活动包含一个片段。我不确定这是否会成为问题。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.noticeboard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
    android:name=".Dashboard"
    android:label="@string/title_activity_dashboard" >
</activity>
<activity
    android:name=".SplashScreen"
    android:label="@string/title_activity_splash_screen" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity
    android:name=".Login"
    android:label="@string/title_activity_login" >
</activity>
<activity
    android:name=".NoticeViewer"
    android:label="@string/title_activity_notice_viewer" >
</activity>
<service
    android:name=".FirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGE_EVENT"/>
    </intent-filter>
</service>
<service
    android:name=".FirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>
</application>
</manifest>

LOGCAT

06-05 12:06:05.678 32386-32386/com.example.noticeboard W/ActivityThread: Application com.example.noticeboard is waiting for the debugger on port 8100...
06-05 12:06:05.686 32386-32386/com.example.noticeboard I/System.out: Sending WAIT chunk
06-05 12:06:05.892 32386-32393/com.example.noticeboard I/art: Debugger is active
06-05 12:06:05.897 32386-32386/com.example.noticeboard I/System.out: Debugger has connected
06-05 12:06:05.897 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:06.107 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:06.317 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:06.527 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:06.587 32386-32393/com.example.noticeboard W/art: Suspending all threads took: 41.152ms
06-05 12:06:06.741 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:06.947 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:07.157 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:07.368 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:07.577 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:07.786 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:07.998 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle... 
06-05 12:06:08.208 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:08.418 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle...
06-05 12:06:08.628 32386-32386/com.example.noticeboard I/System.out: waiting for debugger to settle... 
06-05 12:06:08.838 32386-32386/com.example.noticeboard I/System.out: debugger has settled (1466)
06-05 12:06:08.884 32386-32386/com.example.noticeboard W/System: ClassLoader referenced unknown path: /data/app/com.example.noticeboard-1/lib/x86
06-05 12:06:08.906 32386-32386/com.example.noticeboard D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
06-05 12:06:08.918 32386-32386/com.example.noticeboard D/FirebaseApp: Initialized class com.google.firebase.iid.FirebaseInstanceId.
06-05 12:06:08.919 32386-32386/com.example.noticeboard D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
06-05 12:06:08.943 32386-32386/com.example.noticeboard I/FA: App measurement is starting up, version: 9080
06-05 12:06:08.943 32386-32386/com.example.noticeboard I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
06-05 12:06:09.022 32386-32386/com.example.noticeboard D/FirebaseApp: Initialized class com.google.android.gms.measurement.AppMeasurement.
06-05 12:06:09.022 32386-32386/com.example.noticeboard I/FirebaseInitProvider: FirebaseApp initialization successful
06-05 12:06:09.045 32386-32456/com.example.noticeboard W/GooglePlayServicesUtil: Google Play services out of date.  Requires 9080000 but found 8087470
06-05 12:06:09.130 32386-32457/com.example.noticeboard D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

06-05 12:06:09.134 32386:32386 D/         ]
                                                                     HostConnection::get() New Host Connection established 0xabe6ce00, tid 32386
06-05 12:06:09.141 32386-32386/com.example.noticeboard W/FA: Service connection failed: ConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}

[ 06-05    12:06:09.188 32386:32457 D/         ]
                                                         HostConnection::get() New Host Connection established 0xabe6cff0, tid 32457
06-05 12:06:09.193 32386-32457/com.example.noticeboard I/OpenGLRenderer: Initialized EGL, version 1.4
06-05 12:06:09.215 32386-32457/com.example.noticeboard W/EGL_emulation: eglSurfaceAttrib not implemented
06-05 12:06:09.215 32386-32457/com.example.noticeboard W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3e6ca80, error=EGL_SUCCESS
06-05 12:06:17.473 32386-32394/com.example.noticeboard W/art: Method processed more than once: void java.lang.Thread.run()
06-05 12:06:17.474 32386-32395/com.example.noticeboard W/art: Method processed more than once: void java.lang.Object.wait(long)
06-05 12:06:17.474 32386-32395/com.example.noticeboard W/art: Method processed more than once: void java.lang.Thread.run()
06-05 12:06:17.478 32386-32396/com.example.noticeboard W/art: Method processed more than once: void java.lang.Thread.run()
06-05 12:06:32.514 32386-32457/com.example.noticeboard W/EGL_emulation: eglSurfaceAttrib not implemented
06-05 12:06:32.514 32386-32457/com.example.noticeboard W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa24ff280, error=EGL_SUCCESS
06-05 12:06:32.624 32386-32457/com.example.noticeboard E/Surface: getSlotFromBufferLocked: unknown buffer: 0xabee3340
06-05 12:06:32.656 32386-332/com.example.noticeboard D/FirebaseInstanceId: topic sync succeeded

提前感谢您!


你是否发送到了正确的令牌?能否添加你清单文件的代码。 - Shubhank
已经添加了……是的,我发送了正确的令牌,只有这样才能从 FCM 服务器获得结果中的 success:1。请检查一下。 - Tejasv Kulshrestha
如果您将消息发送到另一个设备标记,也将成功。请卸载应用程序并重新安装以获得新的标记,并使用它来发送消息。 - Shubhank
我也做了这件事,我百分之百确定我发送到 FCM 服务器的令牌与我想要通知的设备相同...谢谢。 - Tejasv Kulshrestha
你能分享一下你正在发送的服务器端 object 吗?因为我也遇到了同样的问题。 - Usman lqbal
显示剩余2条评论
3个回答

2
您没有遵循正确的代码。请再次查看此链接解决方案 请使用以下代码。
<action android:name="com.google.firebase.MESSAGING_EVENT" />

替代

<action android:name="com.google.firebase.MESSAGE_EVENT"/>

1

1
常见错误。
android:name="YourApplicationPackageName.YourFirebaseMessagingServiceName"

你需要更改一些部分 AndroidManifest.xml你的代码:
 <service
        android:name=".FirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGE_EVENT"/>
        </intent-filter>
    </service>
    <service
        android:name=".FirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

替换为以下代码:

用以下代码替换:

<service
            android:name="com.example.noticeboard.FirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGE_EVENT"/>
            </intent-filter>
        </service>
        <service
            android:name="com.example.noticeboard.FirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

最近几个小时前,我解决了这个问题,问题出在清单文件中。我只是将“com.google.firebase.MESSAGE_EVENT”更改为“com.google.firebase.MESSAGING_EVENT”..这解决了我的问题..顺便说一句,感谢你的努力! - Tejasv Kulshrestha
我也有同样的问题,上面提到的答案对我有效。 - Usman lqbal
你改了什么? - Usman lqbal
我刚才在评论中提到,我将“com.google.firebase.MESSAGE_EVENT”更改为“com.google.firebase.MESSAGING_EVENT”,问题得到解决! - Tejasv Kulshrestha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接