Bug 1301558 - Use WakefulBroadcastReceiver to process incoming push intents r=nalexander
MozReview-Commit-ID: HNrk30UXsuX
--- a/mobile/android/base/GcmAndroidManifest_services.xml.in
+++ b/mobile/android/base/GcmAndroidManifest_services.xml.in
@@ -2,28 +2,40 @@
<service
android:name="org.mozilla.gecko.gcm.GcmInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
- <!-- Provided by on-device Google Play Services. Directs inbound messages to internal listener service. -->
+ <!-- Provided by on-device Google Play Services. Necessary for compatibility with API<19.
+ We could route all push intents (including RECEIVE) through this, but GcmReceiver seems
+ to drop our messages if Fennec isn't running at the time message was received by device.
+ -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
- <action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="@ANDROID_PACKAGE_NAME@" />
</intent-filter>
</receiver>
- <!-- Handle messages directed by the GCM receiver. -->
+ <!-- Handle all inbound RECEIVE intents. -->
+ <receiver
+ android:name="org.mozilla.gecko.gcm.GcmWakefulBroadcastReceiver"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="com.google.android.c2dm.intent.RECEIVE" />
+ </intent-filter>
+ </receiver>
+
+ <!-- Handle messages directed by the GCM receiver and the wakeful broadcast receiver. -->
<service
android:name="org.mozilla.gecko.gcm.GcmMessageListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
+ <category android:name="@ANDROID_PACKAGE_NAME@" />
</intent-filter>
</service>
--- a/mobile/android/base/java/org/mozilla/gecko/gcm/GcmMessageListenerService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/gcm/GcmMessageListenerService.java
@@ -10,17 +10,18 @@ import android.util.Log;
import com.google.android.gms.gcm.GcmListenerService;
import org.mozilla.gecko.push.PushService;
import org.mozilla.gecko.util.ThreadUtils;
/**
* This service actually handles messages directed from the on-device Google
- * Play Services package. We simply route them to the AndroidPushService.
+ * Play Services package or from GcmWakefulBroadcastReceiver.
+ * We simply route them to the AndroidPushService.
*/
public class GcmMessageListenerService extends GcmListenerService {
/**
* Called when message is received.
*
* @param from SenderID of the sender.
* @param bundle Data bundle containing message data as key/value pairs.
*/
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/gcm/GcmWakefulBroadcastReceiver.java
@@ -0,0 +1,24 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.gcm;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.support.v4.content.WakefulBroadcastReceiver;
+
+/**
+ * This receiver handles RECEIVE push intents. We have this in addition to GcmMessageListenerService
+ * in order to receive push messages with just an encrypted payload while Fennec isn't running at all.
+ */
+public class GcmWakefulBroadcastReceiver extends WakefulBroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ intent.setClass(context, GcmMessageListenerService.class);
+ startWakefulService(context, intent);
+ setResultCode(Activity.RESULT_OK);
+ }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -761,16 +761,17 @@ if CONFIG['MOZ_CRASHREPORTER']:
gbjar.sources += [ 'java/org/mozilla/gecko/CrashReporter.java' ]
ANDROID_RES_DIRS += [ 'crashreporter/res' ]
if CONFIG['MOZ_ANDROID_GCM']:
gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [
'gcm/GcmInstanceIDListenerService.java',
'gcm/GcmMessageListenerService.java',
'gcm/GcmTokenClient.java',
+ 'gcm/GcmWakefulBroadcastReceiver.java',
'push/Fetched.java',
'push/PushClient.java',
'push/PushManager.java',
'push/PushRegistration.java',
'push/PushService.java',
'push/PushState.java',
'push/PushSubscription.java',
]]