Skip to content

Commit

Permalink
feat: add notification actions support 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
EdricChan03 committed Apr 21, 2018
1 parent 0740f13 commit 9f9476e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
18 changes: 15 additions & 3 deletions app/src/main/java/com/edricchan/studybuddy/SharedHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.edricchan.studybuddy;

import android.app.Activity;
import android.content.Context;

import com.edricchan.studybuddy.interfaces.Notification;
Expand Down Expand Up @@ -32,27 +33,38 @@ public class SharedHelper {
private Context mContext;
private int dynamicId = 0;
private AtomicInteger atomicInteger = new AtomicInteger(0);

public SharedHelper(Context context) {
this.mContext = context;
}

public SharedHelper() {
}

public static String getTag(Class tagClass) {
return tagClass.getSimpleName();
}

/**
* Parses the string (<code>s</code>) into an object (<code>clazz</code>)
* @param s The string to parse
*
* @param s The string to parse
* @param clazz The class type to parse the string to
* @param <T> Any
* @param <T> Any
* @return The constructor
* @throws Exception
*/
public static <T> T parseObjectFromString(String s, Class<T> clazz) throws Exception {
return clazz.getConstructor(new Class[] {String.class }).newInstance(s);
return clazz.getConstructor(new Class[]{String.class}).newInstance(s);
}

/**
* Dynamically creates a new ID for use with Android's notification manager
*/
public int getDynamicId() {
return atomicInteger.incrementAndGet();
}

/**
* Creates a notification action
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package com.edricchan.studybuddy.interfaces;

public class NotificationAction {
private String mAction;
private String mActionType;
private String mActionIcon;
public String action;
public String actionType;
public String actionIcon;

public NotificationAction() {

}
public NotificationAction(NotificationAction notificationAction) {
this.mAction = notificationAction.getAction();
this.mActionType = notificationAction.getActionType();
this.mActionIcon = notificationAction.getActionIcon();
this.action = notificationAction.getAction();
this.actionType = notificationAction.getActionType();
this.actionIcon = notificationAction.getActionIcon();
}
public NotificationAction(String action, String actionType, String actionIcon) {
this.mAction = action;
this.mActionType = actionType;
this.mActionIcon = actionIcon;
this.action = action;
this.actionType = actionType;
this.actionIcon = actionIcon;
}

public String getAction() {
return this.mAction;
return this.action;
}

public String getActionIcon() {
return this.mActionIcon;
return this.actionIcon;
}

public String getActionType() {
return this.mActionType;
return this.actionType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.preference.PreferenceActivity;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.crashlytics.android.Crashlytics;
import com.edricchan.studybuddy.MainActivity;
Expand All @@ -24,11 +25,13 @@
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StudyBuddyMessagingService extends FirebaseMessagingService {
private SharedHelper sharedHelper = new SharedHelper(this);

// To be used for Android's Log
private String TAG = sharedHelper.getTag(this.getClass());
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Expand Down Expand Up @@ -83,29 +86,19 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
}
// Check if the data of the remote message has a notificationActions
if (remoteMessage.getData().containsKey("notificationActions")) {
System.out.println(remoteMessage.getData().get("notificationActions"));
Log.d(TAG, "notificationActions: " + remoteMessage.getData().get("notificationActions"));
List<NotificationAction> notificationActions = new ArrayList<>();
try {
Gson gson = new Gson();
List<NotificationAction> notificationActions = gson.fromJson(remoteMessage.getData().get("notificationActions"), new TypeToken<List<NotificationAction>>() {}.getType());
System.out.println("Size: " + notificationActions.size());
System.out.println("NotificationAction#action: " + notificationActions.get(0).getAction());
// ArrayList<NotificationAction> defaultNotificationActions = null;
// defaultNotificationActions.add(new NotificationAction("Test", SharedHelper.ACTION_NOTIFICATIONS_SETTINGS, SharedHelper.ACTION_SETTINGS_ICON));
// Gson gson = new Gson();
// Type type = new TypeToken<NotificationAction[]>(){}.getType();
// notificationActions = gson.fromJson(remoteMessage.getData().get("notificationActions"), type);
// int size = Jsoner.deserialize(remoteMessage.getData().get("notificationActions"), new JsonArray()).size();
// System.out.println("Size: " + size);

// defaultNotificationActions = new ArrayList<NotificationAction>(Jsoner.deserialize(remoteMessage.getData().get("notificationActions"), new JsonArray()).toArray(NotificationAction[] notificationActions));
// System.out.println("Result: " + remoteMessage.getData().get("notificationActions"));
// System.out.println("Result (json array): " + notificationActions.toString());
// System.out.println("notificationActions (length): " + notificationActions.length);
NotificationAction[] remoteNotificationActions = gson.fromJson(remoteMessage.getData().get("notificationActions"), NotificationAction[].class);
notificationActions = Arrays.asList(remoteNotificationActions);
Log.d(TAG, "Size: " + remoteNotificationActions.length);
Log.d(TAG, "JSON: " + remoteNotificationActions.toString());
Log.d(TAG, "NotificationAction#action: " + remoteNotificationActions[0].getAction());
} catch (Exception e) {
e.printStackTrace();
Crashlytics.logException(e);
}
/*
for (NotificationAction notificationAction : notificationActions) {
int icon = 0;
// Initial intent
Expand All @@ -125,7 +118,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
icon = R.drawable.ic_settings_24dp;
break;
}
switch (notificationAction.getActionType()) {
switch (notificationAction.actionType) {
case SharedHelper.ACTION_NOTIFICATIONS_SETTINGS:
intent = new Intent(this, SettingsActivity.class);
intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, SettingsActivity.NotificationPreferenceFragment.class.getName());
Expand All @@ -135,7 +128,6 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
}
builder.addAction(new NotificationCompat.Action(icon, notificationAction.getAction(), notificationPendingIntent));
}
*/
}
}
Intent mainActivityIntent = new Intent(this, MainActivity.class);
Expand Down

0 comments on commit 9f9476e

Please sign in to comment.