web-dev-qa-db-ja.com

FCM:onMessageReceivedが呼び出されません、fcmにメッセージを送信した後も通知が届きませんでしたか?

pHPを介してFCMプッシュ通知を実装したいアプリを開発しています。

だから私は2つのJavaファイルを作成しました:1.FirebaseInstanceID(正常に動作し、データベースでトークンを正しく取得しています)2.FirebaseMessagingSerivice(呼び出されません)

My 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サーバーにプッシュされたときにアプリが正常に実行されます。

My 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

前もって感謝します!

9

適切なコードに従っていません。 このリンクをもう一度確認してください

ソリューション

以下のコードを使用してください

<action Android:name="com.google.firebase.MESSAGING_EVENT" />

の代わりに

<action Android:name="com.google.firebase.MESSAGE_EVENT"/>
2
Yogesh Rathi

よくある間違い

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>
1
Usman lqbal

残念ながら、 sman Iqbal の答えは完全には正しくありません。公式AndroidドキュメントはMESSAGING_EVENTを使用します:

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

出典: https://firebase.google.com/docs/cloud-messaging/Android/receive#edit-the-app-manifest

1
Maviza101