1

Firebase Messaging Service Not received , While the application is closed in the background. message appears to be sent when I send a message

{
    "multicast_id": 5871474003172698383,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1569854682800517%358f5cee358f5cee"
        }
    ]
}

However, notification is not received until the app is opened.

and while the phone screen is off , Image does not load

        .setStyle(new NotificationCompat.BigPictureStyle()
                .bigPicture(image)
                .bigLargeIcon(null))

what am i missing ?

post Json

{
    "to": "fai1***************V0h6PuF5UA",
    "collapse_key": "type_a",
    "notification": {
        "body": "Body 5",
        "title": "Title"
    },
    "data": {
        "image": false,
        "image_url": "https://res.cloudinary.com/yhackup/image/upload/v1569505733/indirim_copy.png",
        "intent_url": "http://polisoft.com.tr/Blog/musteri-destek-ve-egitim-personeli-alinacaktir"
    }
}

Manifest Tag

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

MyFirebaseMessagingService

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

    @Override
    public void onSendError(@NonNull String s, @NonNull Exception e) {
        Log.e("onMessageReceived",s);
        super.onSendError(s, e);
    }

    // [START receive_message]
    @SuppressLint("WrongThread")
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.e("onMessageReceived","Mesaj Geldi.");
        if (remoteMessage.getNotification() != null) {
            if (remoteMessage.getData().size() > 0) {
                JSONObject jsonObject = new JSONObject(remoteMessage.getData());
                boolean isImage = jsonObject.optBoolean("image", false);
                if (isImage) {
                    new sendImageNotification().execute(
                            remoteMessage.getNotification().getTitle(),
                            remoteMessage.getNotification().getBody(),
                            jsonObject.optString("image_url"),
                            jsonObject.optString("intent_url"));
                } else {
                    sendNotification(
                            remoteMessage.getNotification().getTitle(),
                            remoteMessage.getNotification().getBody());
                }
            }
        }
    }
    // [END receive_message]


    // [START on_new_token]

    @Override
    public void onNewToken(String token) {
        Log.d(TAG, "Refreshed token: " + token);
        sendRegistrationToServer(token);
    }
    // [END on_new_token]


    private void sendRegistrationToServer(String token) {
        Context context = this;
         Tools.SendFirebaseInstanceId(token, context);
    }
}
Ashish
  • 6,005
  • 3
  • 17
  • 40
yhackup
  • 27
  • 7
  • checkout this https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background – Ankit Oct 01 '19 at 05:48
  • 1
    you need to change your json payload to data only to get notification in background – Ankit Oct 01 '19 at 05:49
  • try to add action data in your payload so it will be called onMessageReceived method otherwise, it will not call this method – Harsh Shah Oct 01 '19 at 05:58
  • @Ankit I sent this way but I did not { "data":{ "id": 1 }, "to": "fai1kc9O********1NL4719ymo-V0h6PuF5UA", "priority": "high" } – yhackup Oct 01 '19 at 06:12
  • try something like this { "data": { "body": "here is body", "title": "Title" }, "to": "xxxxxxx" } – Ankit Oct 01 '19 at 06:25
  • @Ankit It was not – yhackup Oct 01 '19 at 06:27
  • I think @Ankit is wright - you need to use data only message. Check this answer https://stackoverflow.com/questions/57947042/firebase-cloud-messaging-didnt-see-xiaomi-devices/57951824#57951824 – MarkWalczak Oct 01 '19 at 07:17
  • @MarkWalczak wrong, it can not be – yhackup Oct 01 '19 at 07:29

1 Answers1

1

{ "to" : "dBKpb8U7QKyMRKIXqHlLcg:APA91bH_lWnsFCaisz7pOUhxqNtWRCn5NdCiQ2ofvVKWfIcK7LCycVwv-RuJvOCViZfBOlkrz7KG1r72LCkxNycwPymFFbTWwjS3XPOwshpobMEWB_lblBgjXShdzzyufJAO4c4z0pfk", "data" : { "command":"restart" } }

send your json firebase. and ever your app enter for FirebaseMessagingService

fred rueda
  • 11
  • 2