简体   繁体   中英

GCM 3.0 notification payload not showing android notification when app is in foreground

When i am sending a gcm message with notification(no data) payload, then the android notification is shown only if my app is in background. If the app is in foreground then no android notification is shown but the onMessageReceived() in GcmListenerService implementation is called with null "message".

onMessageReceived() is not called when the app is in background and android notification is shown as expected.

Is this the expected behavior or am i missing something? Please let me know in case if any client side code is required.

Here is the server side code snippet :

Message.Builder messageBuilder = new Message.Builder();
messageBuilder.notification(new Notification.Builder("ic_launcher")
.clickAction("ScrHome1")
.title("Bling")
.body(blingNotification.getPayload().getValue())
.build());
Message message = messageBuilder.build();
sender.send(message, targetIdList, retries);

UPDATE

I tried to check the same behavior in sample app provided b google and found that its same. So is it supposed to be like that ?

Here is the gcm sample server side code in which we have done minor changes to send a message with notification only payload.

public class GcmSender {

public static final String API_KEY = "AIaSyCMzWOageHbcX9yxNtfL6RygdbLT-7Ls";

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    try {
        // Prepare JSON containing the GCM message content. What to send and where to send.
        JSONObject jGcmData = new JSONObject();
        JSONObject jData = new JSONObject();
        JSONObject jNotification = new JSONObject();
        jData.put("message", "hello");
        jNotification.put("body", "hi dev");
        jNotification.put("title", "POC");
        jNotification.put("icon", "ic_launcher");
        // Where to send GCM message.
        jGcmData.put("to",
                "eucb9MGv3g:APA91bGJWZjBET12nYuDrX8yiylPqt3uy87ThP2f4E9Nw89GOvbZkWSTFVPyQ8keTPQubWzpW_10-Aydqu04MD1GvzeTUAh6SoFk6qeXSUW0205h6sbQdTe74VZfMu8t2P9nrWOE");
        // What to send in GCM message.
        jGcmData.put("notification", jNotification);

        // Create connection to send GCM Message request.
        URL url = new URL("https://android.googleapis.com/gcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "key=" + API_KEY);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        // Send GCM message content.
        OutputStream outputStream = conn.getOutputStream();
        outputStream.write(jGcmData.toString().getBytes());

        // Read GCM response.
        InputStream inputStream = conn.getInputStream();
        String resp = IOUtils.toString(inputStream);
        System.out.println(resp);
        System.out.println("Check your device/emulator for notification or logcat for "
                + "confirmation of the receipt of the GCM message.");
    } catch (IOException e) {
        System.out.println("Unable to send GCM message.");
        System.out.println("Please ensure that API_KEY has been replaced by the server "
                + "API key, and that the device's registration token is correct (if specified).");
        e.printStackTrace();
    }
}

}

The observation is same, whenever the sample client app is in background the android notification is shown without any client code intervention , but whenever the sample client app is in foreground the android notification is not shown but the MyGcmListenerService.onMessageReceived(String from, Bundle data) is called with values as From: 234552842207 Message: null.

It should be noted that this method wasn`t called in previous case when the sample client app was in foreground.

So now there are 3 possibilities.

  • There is something wrong or missing in the way we are sending downstream message with notification payload.
  • The behavior is like this only.
  • Possibility of a bug.

Please help me out here.

So it was possibility no. 3 "The behavior is like this only."

Here is the reply from gcm-dev-support@google.com :

This is to confirm to you that the notification payload on Android will only show on the notification tray when the app is in the background. If it is in the foreground and you would like to deploy a notification, you could consider deploying notifications similarly to this method in the sample app. It is called from onMessageReceived(). Hope this helps.

In your Chat activity create a broadcast receiver like below and call it in the onCreate function.

Add variable BroadcastReceiver mRegistrationBroadcastReceiver; to your class

private void setUpBroadcastReceiver() {

    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //create notification
        }
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM