收到FCM通知时,从不调用onMessageReceived,上面的API 19

时间:2018-03-22 07:14:09

标签: android firebase firebase-cloud-messaging

更新 onMessageReceived在API 19中调用,但在23和24中没有调用。 的更新

我正在从服务器发送FCM通知,我可以接收通知但是onMessageReceived没有被调用(前台,后台),即使有数据消息。这是我正在使用的代码。

import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import uk.co.socktchat.socketchat.MainActivity;

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) {
        // [START_EXCLUDE]
        // There are two types of messages data messages and notification messages. Data messages are handled
        // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
        // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
        // is in the foreground. When the app is in the background an automatically generated notification is displayed.
        // When the user taps on the notification they are returned to the app. Messages containing both notification
        // and data payloads are treated as notification messages. The Firebase console always sends notification
        // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
        // [END_EXCLUDE]

        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: 
        Log.d(TAG, "From: " + remoteMessage.getFrom());

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload: " + remoteMessage.getData());

            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());
        }

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

        // [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);


    }
}

我的清单文件

<service
    android:name=".fcm.MyFirebaseMessagingService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
<!-- [END firebase_service] -->
<!-- [START firebase_iid_service] -->
<service
    android:name=".fcm.MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

json我从服务器发帖

{
    "data": {
    "title": "5x1",
    "body": "15:10"
  },
    "to" : "d6IJZlXAUZ4:APA91bF8kB_jBn_N93otIdRFsu89ZDhrFsL--i0I4TmS-LO_h3QQ79lu6VlTQe0poNpuUni7N45_jw2XMli",
    "priority" : "high",
    "content_available" : true



 }
我没有看到任何错误,我尝试了不同版本的Android,思考android版本可能是任何原因,但似乎没有找到任何问题。有任何想法吗?谢谢。

3 个答案:

答案 0 :(得分:0)

检查邮件是否包含通知有效负载。

if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Notification Payload: " + remoteMessage.getNotification().getBody());
            //handleNotification(remoteMessage.getNotification().getBody());
        }

检查消息是否包含数据有效负载。

if (remoteMessage.getData().size() > 0){
   Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
   handleNotification(remoteMessage.getData());
}

答案 1 :(得分:0)

根据FCM文件

  

通知消息包含一组预定义的用户可见键。   相反,数据消息仅包含用户定义的自定义   键值对。通知消息可以包含可选数据   有效载荷。两种消息类型的最大有效负载为4KB,除非   从Firebase控制台发送邮件,强制执行1024   字符限制。

检查您的邮件大小是否在限制范围内。

答案 2 :(得分:0)

事实上,onMessageReceived一直被调用,只是Logcat没有显示任何消息,奇怪的是它只发生在上面的API 19设备上,在具有API 19 Logcat的设备上显示消息,我无法解释为什么会发生这种情况,但我的问题已经解决了。