无法使用onMessageReceived方法

时间:2017-05-02 11:14:25

标签: android push-notification

当应用处于前台时,无法接收推送通知。此外,在后台接收推送通知,但在收到推送时无法获取数据。它由FCM自动生成

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.e("inside","inside");

        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        JSONObject object=new JSONObject(remoteMessage.getData());
        Log.e("jsonobject",object.toString());
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());
            sendNotification(remoteMessage.getData().toString());

//            if (/* Check if data needs to be processed by long running job */ true) {
//                // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
//                scheduleJob();
//            } else {
//                // Handle message within 10 seconds
//                handleNow();
//
//            }

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
            sendNotification(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.
    }
    // [END receive_message]

    /**
     * Schedule a job using FirebaseJobDispatcher.
     */
    private void scheduleJob() {
        // [START dispatch_job]
        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
        Job myJob = dispatcher.newJobBuilder()
                .setService(MyJobService.class)
                .setTag("my-job-tag")
                .build();
        dispatcher.schedule(myJob);
        // [END dispatch_job]
    }

    /**
     * Handle time allotted to BroadcastReceivers.
     */
    private void handleNow() {
        Log.d(TAG, "Short lived task is done.");
    }

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

我也是这个

  

W / ContextImpl:forgetServiceDispatcher失败:                                                                       java.lang.IllegalArgumentException:服务未注册:   com.google.android.gms.measurement.internal.zzad$zza@30f53b70

 java.lang.IllegalArgumentException: Service not registered: com.google.android.gms.measurement.internal.zzad$zza@30f53b70
                                                                            at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1105)
                                                                            at android.app.ContextImpl.unbindService(ContextImpl.java:1872)
                                                                            at android.content.ContextWrapper.unbindService(ContextWrapper.java:562)
                                                                            at com.google.android.gms.common.stats.zzb.zza(Unknown Source)
                                                                            at com.google.android.gms.measurement.internal.zzad.disconnect(Unknown Source)
                                                                            at com.google.android.gms.measurement.internal.zzad.zzabl(Unknown Source)
                                                                            at com.google.android.gms.measurement.internal.zzad.zzb(Unknown Source)
                                                                            at com.google.android.gms.measurement.internal.zzad$1.run(Unknown Source)
                                                                            at com.google.android.gms.measurement.internal.zzf$1.run(Unknown Source)
                                                                            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                                                                            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                            at com.google.android.gms.measurement.internal.zzw$zzd.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

如果您正在使用此类其他Google服务,请检查版本是否相同。查看文件build.gradle(Module:app)

compile 'com.google.android.gms:play-services-location:10.2.4'
apply plugin: 'com.google.gms.google-services'
compile 'com.google.firebase:firebase-messaging:10.2.4'

检查清单文件是否与此类似:

     ...<activity android:name=".actMyLastActivityFromManifest"></activity>

    <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>
</application>