Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup FCM to receive data payload messages in background? #88

Closed
2ndGAB opened this issue Jul 17, 2016 · 4 comments
Closed

How to setup FCM to receive data payload messages in background? #88

2ndGAB opened this issue Jul 17, 2016 · 4 comments
Assignees

Comments

@2ndGAB
Copy link

2ndGAB commented Jul 17, 2016

  • Android device: any
  • Android OS version: 6.0.1
  • Google Play Services version: 3.0.0
  • Firebase/Play Services SDK version: 9.2.1

Describe the problem:

I read many posts about receiving data-messages in background mainly on Stackoverflow and we can find as many post to tell it works as to tell it doesn't work!

And the doc is really fuzzy about this specific feature and as Stackoverflow is fuzzy as well, I ask here expecting a definitive answer.

About the doc, where is the ability to receive data-payload message in background explained?
I can see explanation about notification, notification+data, but even if I see few data only examples, I cannot see anywhere what are for and if they are received in background. But there were for sure! Is it still the case? Mistery.

My problem is that I build an application quite recently with studio 1.5.2, corresponding sdk version !?!, FCM 9.2.0 which perfectly receives data messages like below The message is sent via Advanced REST client. This message is successfully sent of course.

{
 "registration_ids": [
 "dhsy6lq_s9Y:APA91bEuG2yVh ... _VGwDhAey1B9_",
 ],
 "data": {
 "id": 36,
 "title": "My title",
 "msg": "texte",
 "code": 2,
 "infosUrl": "www..example.com",
 "eventLocation": "Nowhere",
 "latitude": 47.90022,
 "longitude": 15.019639377,
 "startDate": "2016/06/25 19:14",
 "endDate": "2016/06/30 20:00",
 "publishEndDate": "2016/06/30 23:59",
 "image": ""
 },
 "delay_while_idle": false,
 "priority": "high",
 "content_available": true
 }

But after I update studio to 2.1.2, sdk, and java (1.7 to 1.8), The same working project newly rebuilt is not able to receive any data message in background anymore.

My manifest is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.example"
    android:installLocation="auto"
    android:versionCode="52"
    android:versionName="@string/app_version">

    <uses-feature android:name="android.hardware.camera" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:logo="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

        </activity>

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

</manifest>

And MyFirebaseMessagingService. I use a data payload message to create a custom notification, between others because I also process other type of payload messages.

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    public static final int NOTIFICATION_ID = 1;

    private static final String TAG = "MyFirebaseMsgService";

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

        Log.i(Constants.APP_TAG, "Received message " + fcmMessage.getData());

        Map data = fcmMessage.getData();

        Spanned span = null;
        String id;
        String title;
        String message;
        String webSite;
        String image;
        String location;
        String latitude;
        String longitude;
        String startDate;
        String endDate;
        String publishEndDate;
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();

        int code = Integer.parseInt((String) data.get("code"));

        switch (code) {
            case 2: // Notification

                id = (String) data.get("id");
                title = (String) data.get("title");
                message = (String) data.get("msg");
                webSite = (String) data.get("infosUrl");
                location = (String) data.get("eventLocation");
                latitude = (String) data.get("latitude");
                longitude = (String) data.get("longitude");
                startDate = (String) data.get("startDate");
                endDate = (String) data.get("endDate");

                if (title != null) {
                    span = Html.fromHtml(title);
                    title = span.toString();
                }

                if (message != null) {
                    span = Html.fromHtml(message);
                    message = span.toString();
                }

                if (location != null) {
                    span = Html.fromHtml(location);
                    location = span.toString();
                }

                if (webSite != null) {
                    span = Html.fromHtml(webSite);
                    webSite = span.toString();
                }

                Log.i(Constants.APP_TAG,
                        "\ntitle = " + title +
                                "\n message = " + message +
                                "\n code = " + code +
                                "\n location = " + location +
                                "\n latitude = " + latitude +
                                "\n longitude = " + longitude +
                                "\n website = " + webSite +
                                "\n start date = " + startDate +
                                "\n end date = " + endDate);

                sendNotification(title, message, location, latitude, longitude, webSite, startDate, endDate);
                break;

            default:
                break;
        }
        // [END_EXCLUDE]
    }
    // [END receive_message]


}

When I put my application in background, I receive D/FirebaseApp: Notifying background state change listeners. What does it mean?

Another strange behavior. In my working application, if I receive the message in foreground which generates the notification, when I put the application in background, the notification stays in the notification center. With the newly built project, when I put the application in background, the notification is automatically removed. I don't know if that can explain somthing.

So aredata payload messages really supposed to be received in background?
If no, why do I have a working application ??
If yes, what is missing in my setting to explain it doesn't work?

And could you enlighten a bit the feature in the doc?

EDIT

I also tried your quickstart-android messaging app and it doesn't receive anything when in background.
Is there any mistake in the sent message??

@kroikie kroikie self-assigned this Jul 18, 2016
@kroikie
Copy link
Contributor

kroikie commented Jul 18, 2016

Could you clarify what you mean by "Advanced REST client"?

@kroikie
Copy link
Contributor

kroikie commented Jul 18, 2016

Closing since this seems to be a duplicate of #89 please file a new issue if this is not the case.

@kroikie kroikie closed this as completed Jul 18, 2016
@firebase firebase deleted a comment from pbhakt Nov 15, 2017
@imrankhanluhar
Copy link

Give priority to Data payload . don't send notification pay load in message .
your problem will be solved

@ubuntu2326
Copy link

Don't send notification payload, only send Data payload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants