Skip to content

Commit

Permalink
Run Notification Processing via JobScheduler API (#946)
Browse files Browse the repository at this point in the history
* run notification processing via job scheduler

* lint
  • Loading branch information
phil-lopreiato authored Mar 8, 2020
1 parent 71e40ff commit ac96cae
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 33 deletions.
5 changes: 4 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@
android:name=".receivers.NotificationChangedReceiver"
android:exported="false" />

<service android:name=".gcm.GCMMessageHandler" />
<service
android:name=".gcm.GCMMessageHandler"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<service
android:name=".accounts.AccountAuthenticatorService"
android:exported="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.thebluealliance.androidclient.config.ConfigModule;
import com.thebluealliance.androidclient.datafeed.gce.GceModule;
import com.thebluealliance.androidclient.gcm.GCMMessageHandler;
import com.thebluealliance.androidclient.gcm.GcmModule;
import com.thebluealliance.androidclient.renderers.RendererModule;

import javax.inject.Singleton;
Expand All @@ -11,7 +12,7 @@

@Singleton
@Component(
modules = {GceModule.class, RendererModule.class, ConfigModule.class},
modules = {GceModule.class, RendererModule.class, ConfigModule.class, GcmModule.class},
dependencies = ApplicationComponent.class)
public interface NotificationComponent {
void inject(GCMMessageHandler gcmMessageHandler);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package com.thebluealliance.androidclient.gcm;

import android.app.Activity;
import android.content.ComponentName;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.legacy.content.WakefulBroadcastReceiver;

import com.thebluealliance.androidclient.TbaLogger;

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
public class GCMBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
TbaLogger.d("Got GCM Message. " + intent);
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GCMMessageHandler.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, intent.setComponent(comp));
setResultCode(Activity.RESULT_OK);
GCMMessageHandler.enqueueWork(context, intent);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.thebluealliance.androidclient.gcm;

import android.app.IntentService;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.core.app.JobIntentService;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

Expand Down Expand Up @@ -55,7 +56,7 @@

import javax.inject.Inject;

public class GCMMessageHandler extends IntentService implements FollowsChecker {
public class GCMMessageHandler extends JobIntentService implements FollowsChecker {

/**
* Stack (bundle) notifications together into a Group for better UX on Nougat+ and Android Wear
Expand All @@ -74,6 +75,9 @@ public class GCMMessageHandler extends IntentService implements FollowsChecker {
/** If grouping won't work, use this ID to make each notification replace its predecessor. */
public static final int SINGULAR_NOTIFICATION_ID = 363;

public static final int JOB_ID = 254;

@Inject GoogleCloudMessaging mCloudMessaging;
@Inject MyTbaDatafeed mMyTbaDatafeed;
@Inject DatabaseWriter mWriter;
@Inject SharedPreferences mPrefs;
Expand All @@ -86,14 +90,6 @@ public class GCMMessageHandler extends IntentService implements FollowsChecker {

private NotificationComponent mComponenet;

public GCMMessageHandler() {
this("GCMMessageHandler");
}

public GCMMessageHandler(String name) {
super(name);
}

@Override
public void onCreate() {
super.onCreate();
Expand All @@ -109,6 +105,7 @@ private void getComponent() {
.datafeedModule(application.getDatafeedModule())
.rendererModule(new RendererModule())
.authModule(application.getAuthModule())
.gcmModule(application.getGcmModule())
.build();
}
}
Expand All @@ -131,14 +128,19 @@ public boolean followsTeam(Context context, String teamNumber, String matchKey,
|| subTable.hasNotificationType(teamAtEventInterestKey, notificationType);
}

@Override
protected void onHandleIntent(Intent intent) {
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, GCMMessageHandler.class, JOB_ID, work);
}

@Override
protected void onHandleWork(Intent intent) {
Bundle extras = intent.getExtras();
if (extras == null) {
TbaLogger.w("Intent with no extras!");
return;
}

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

String messageType = gcm.getMessageType(intent);
String messageType = mCloudMessaging.getMessageType(intent);
TbaLogger.d("GCM Message type: " + messageType);
TbaLogger.d("Intent extras: " + extras.toString());

Expand All @@ -148,8 +150,6 @@ protected void onHandleIntent(Intent intent) {
handleMessage(getApplicationContext(), type, data);

TbaLogger.i("Received : (" + type + ") " + data);

GCMBroadcastReceiver.completeWakefulIntent(intent);
}

public void handleMessage(Context c, String messageType, String messageData) {
Expand Down Expand Up @@ -255,7 +255,7 @@ public void handleMessage(Context c, String messageType, String messageData) {
}
} catch (Exception e) {
// We probably tried to post a null notification or something like that. Oops...
e.printStackTrace();
TbaLogger.e("Error parsing notification", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.thebluealliance.androidclient;

import com.google.firebase.FirebaseApp;
import com.thebluealliance.androidclient.di.DaggerMockApplicationComponent;
import com.thebluealliance.androidclient.di.DaggerMockDatafeedComponent;
import com.thebluealliance.androidclient.di.MockAccountModule;
Expand Down Expand Up @@ -29,7 +28,6 @@ public class TestTbaAndroid extends TbaAndroid {
@Override
public void onCreate() {
disableStetho();
FirebaseApp.initializeApp(this);
super.onCreate();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.thebluealliance.androidclient.gcm;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.Context;
import android.content.Intent;

import com.google.gson.JsonObject;
import com.thebluealliance.androidclient.datafeed.framework.ModelMaker;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.annotation.Nullable;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import static org.junit.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)
public class TestGCMBroadcastReceiver {

private Context mApplicationContext;
private JobScheduler mJobScheduler;

@Before
public void setUp() {
mApplicationContext = ApplicationProvider.getApplicationContext();
mJobScheduler = (JobScheduler) mApplicationContext.getSystemService(Context.JOB_SCHEDULER_SERVICE);
}

@Test
public void testSchedulesJob() {
Intent intent = buildIntent();
mApplicationContext.sendBroadcast(intent);

@Nullable JobInfo job = mJobScheduler.getPendingJob(GCMMessageHandler.JOB_ID);
assertNotNull(job);
}

private Intent buildIntent() {
JsonObject notificationData = ModelMaker.getModel(JsonObject.class, "notification_upcoming_match");
Intent intent = new Intent("com.google.android.c2dm.intent.RECEIVE");
intent.putExtra("notification_type", "upcoming_match");
intent.putExtra("message_data", notificationData.toString());
return intent;
}
}
2 changes: 1 addition & 1 deletion scripts/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def notify(message_type, json_data):
--es message_data '%s'"""
command = template % (message_type, json_text)

print "\nSending " + message_type + " broadcast"
print("\nSending " + message_type + " broadcast")

subprocess.call(["adb", "shell", command])

Expand Down

0 comments on commit ac96cae

Please sign in to comment.