GCM notifications not being recieved when app in foreground
<p>I am trying to setup push notifications to my app containing both data and notification payloads as described in the <a href="https://developers.google.com/cloud-messaging/concept-options#notifications_and_data_messages" rel="nofollow noreferrer">official reference</a>. I want both payloads so that I will get an automatic notification when my app is in the background, and the app can handle things itself while in the foreground.</p>

<p>The <code>onMessageReceived()</code> method in my <code>GcmListenerService</code> is not called when both payloads are included and the app is in the foreground. I have to leave out the notification payload for it to work.</p>

<p>When the app is in the background, it works fine. I get a notification and can open the app when the notification is clicked and do whatever I want from there.</p>

<p>I have looked at <a href="https://stackoverflow.com/questions/34018084/gcm-3-0-notification-payload-not-showing-android-notification-when-app-is-in-for">this question</a>, but they were trying to get a notification when the app is in foreground. I realize I won't get a notification in that case, I just want to get the data.</p>

<p>Relevant portion of AndroidManifest.xml:</p>

<pre><code>&lt;receiver
    android:name="com.google.android.gms.gcm.GcmReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND"&gt;
    &lt;intent-filter&gt;
        &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
        &lt;category android:name="arkenterprises.garage_o_matic.gcm" /&gt;
    &lt;/intent-filter&gt;
&lt;/receiver&gt;
&lt;service
    android:name=".MyGcmListenerService"
    android:enabled="true"
    android:exported="false"&gt;
    &lt;intent-filter&gt;
        &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
    &lt;/intent-filter&gt;
&lt;/service&gt;
</code></pre>

<p>MyGcmListenerService.java:</p>

<pre><code>public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    @Override
    public void onMessageReceived(String from, Bundle data) {
        Log.d(TAG, "Data: " + data);
        String message = data.getString("message");
        String doorStatus = data.getString("DoorStatus");
        String time = data.getString("time");
        Bundle notification = data.getBundle("notification");
        Log.d(TAG, "From: " + from);
        Log.d(TAG, "Message: " + message);
        Log.d(TAG, "DoorStatus: " + doorStatus);
        Log.d(TAG, "Time: " + time);
        Log.d(TAG, "Notification: " + notification);
}
</code></pre>

<p>Server code that sends the message:</p>

<pre><code>nowString = datetime.datetime.now().strftime("%c")
gcmSendURL = 'https://gcm-http.googleapis.com/gcm/send'
gcmHeaders = {'Authorization': 'key=' + API_KEY, 'Content-Type': 'application/json'}
gcmDataPayload = {'message': 'Garage door opened', 'DoorStatus': 'Open', 'time': nowString}
gcmNotificationPayload = {'title': 'Garage Door Has Opened', 'body': 'Garage door opened at {}'.format(nowString), 'icon': 'ic_door_open_notification2'}
print('Garage door opened at {}'.format(nowString))
gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'data': gcmDataPayload}
# gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'notification': gcmNotificationPayload}
# gcmPayload = {'to': regIDs[0].strip(), 'priority': 'high', 'delay_while_idle': False, 'time_to_live': 86400, 'content_available': True, 'data': gcmDataPayload, 'notification': gcmNotificationPayload}
print("\nPayload: {}".format(gcmPayload))
r = requests.post(gcmSendURL, headers=gcmHeaders, json=gcmPayload)
print("\nRequest response: " + r.text)
</code></pre>