Skip to content

Instantly share code, notes, and snippets.

@efung
Last active September 26, 2016 16:51
Show Gist options
  • Save efung/20322d6f137f06fca2988051e0f38ad5 to your computer and use it in GitHub Desktop.
Save efung/20322d6f137f06fca2988051e0f38ad5 to your computer and use it in GitHub Desktop.
Code snippets to accompany my talk given at DevFest DC on 2016-09-24
Slides: https://speakerdeck.com/efung/
final int flags = PendingIntent.FLAG_UPDATE_CURRENT;
Intent serviceIntent = new Intent(NotificationActionService.ACTION_TAPPED,
null,
context,
NotificationActionService.class);
serviceIntent.putExtra(NotificationActionService.EXTRA_ITEM_ID, itemId);
Intent intent = PendingIntent.getService(context.getApplicationContext(),
REQUEST_CODE_TAPPED,
serviceIntent,
flags);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(intent);
# Sample shell script using cURL to send a data notification payload
# to Firebase Cloud Messaging.
BODY=$(cat <<END
{ "data": {
"key1": "val1",
"key2": "val2"
},
"to" : "/topics/all_new_articles",
}
END
)
curl \
--header "Authorization: key=${FCM_SERVER_KEY}" \
--header "Content-Type: application/json" \
https://fcm.googleapis.com/fcm/send \
--data-ascii "${BODY}"
private static final int GROUP_NEW_ARTICLE = 1;
NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context)
.setContentTitle("New article published")
.setContentText("Time Management Tips")
.setSmallIcon(R.drawable.ic_stat_notify)
.setGroup(GROUP_NEW_ARTICLE);
NotificationCompat.Builder summaryBuilder = new NotificationCompat.Builder(context)
.setContentTitle(res.getQuantityString(R.plurals.summary_title, numActive, numActive))
.setSmallIcon(R.drawable.ic_stat_notify)
.setStyle(inboxStyle)
.setGroup(GROUP_NEW_ARTICLE);
.setGroupSummary(true);
private static final int SYNC_MIN = 900; // 15 minutes
private static final int SYNC_MIN = 1800; // 30 minutes
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
long articleId = parseIdFromData(data);
// TBD: Persist articleId so we remember to show notification for it later
boolean isBackground = ((MyApp)getApplication()).isBackground();
firebaseAnalytics.logEvent(isBackground ? "notif_receive" : "notif_foreground",
params);
scheduleArticleFetch(rng.nextInt(SYNC_MIN) + 1, SYNC_MAX);
}
private void scheduleArticleFetch(int min, int max) {
OneoffTask task = new OneoffTask.Builder()
.setService(SyncBackgroundService.class)
.setExtras(taskExtras)
.setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
.setExecutionWindow(min, max)
.setUpdateCurrent(true)
.build();
gcmNetworkManager.schedule(task);
}
}
public class MyGcmTaskService extends GcmTaskService {
public static final EXTRA_DATE = "date";
@Override
public int onRunTask(TaskParams taskParams) {
Bundle extras = taskParams.getExtras();
long latestArticleDate = extras.getLong(EXTRA_DATE);
apiService.fetchLatest(latestArticleDate, …);
// TBD: On successful completion, notify that sync has completed, e.g. via event bus
// Then, for all of the persisted IDs we recorded in onMessageReceived(), create notifications
// for each one
}
}
public class NotificationActionService extends IntentService {
public static final String EXTRA_ITEM_ID = "item_id";
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
long itemId = extras.getLong(EXTRA_ITEM_ID);
switch (intent.getAction()) {
case ACTION_TAPPED:
firebaseAnalytics.logEvent("notif_open", params);
// TBD: Create intent to launch activity to show the item
break;
case ACTION_DISMISSED:
firebaseAnalytics.logEvent("notif_dismissed", params);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment