From 2bf908f9de6fd47ddb0a88ff19b2a64c9baab0d4 Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Tue, 5 Jul 2016 12:20:10 -0500 Subject: [PATCH] Ensured user was subscribed to notification only when they do not have the device token --- .../PushNotificationManager.java | 30 +++++++++++++--- .../habitica/ui/activities/MainActivity.java | 8 +++-- .../lib/models/HabitRPGUser.java | 10 ++++++ .../lib/models/PushDevice.java | 34 +++++++++++++++++++ 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 Habitica/src/main/java/com/magicmicky/habitrpgwrapper/lib/models/PushDevice.java diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.java b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.java index e45a19403..ed58f8288 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.java +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/helpers/notifications/PushNotificationManager.java @@ -10,6 +10,8 @@ import com.google.firebase.messaging.RemoteMessage; import com.habitrpg.android.habitica.APIHelper; import com.habitrpg.android.habitica.HabiticaApplication; import com.habitrpg.android.habitica.callbacks.HabitRPGUserCallback; +import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser; +import com.magicmicky.habitrpgwrapper.lib.models.PushDevice; import java.util.HashMap; import java.util.Map; @@ -40,12 +42,18 @@ public class PushNotificationManager { private String refreshedToken; private SharedPreferences sharedPreferences; private Context context; + private HabitRPGUser user; protected PushNotificationManager(Context context) { HabiticaApplication.getInstance(context).getComponent().inject(this); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); } + public void setUser(HabitRPGUser user) { + Log.v("test", "sdffsdf"); + this.user = user; + } + public static PushNotificationManager getInstance(Context context) { if(instance == null) { instance = new PushNotificationManager(context); @@ -66,11 +74,6 @@ public class PushNotificationManager { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(DEVICE_TOKEN_PREFERENCE_KEY, refreshedToken); editor.commit(); - - - if (this.userIsSubscribedToNotifications()) { - this.addPushDeviceUsingStoredToken(); - } } //@TODO: Use preferences @@ -82,6 +85,14 @@ public class PushNotificationManager { if (this.refreshedToken == null || this.refreshedToken.isEmpty()) { return; } + Log.v("Test", this.user.toString()); + if (this.user == null || this.userHasPushDevice()) { + return; + } + + if (!this.userIsSubscribedToNotifications()) { + return; + } Map pushDeviceData = new HashMap(); pushDeviceData.put("regId", this.refreshedToken); @@ -97,6 +108,15 @@ public class PushNotificationManager { .subscribe(aVoid -> {}, throwable -> {}); } + private Boolean userHasPushDevice() { + for(PushDevice pushDevice : this.user.getPushDevices()) { + if(pushDevice.getRegId().equals(this.refreshedToken)) { + return true; + } + } + return false; + } + public void displayNotification (RemoteMessage remoteMessage) { String remoteMessageIdentifier = remoteMessage.getData().get("identifier"); diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/MainActivity.java b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/MainActivity.java index 71f499fe5..dfdd1e5fd 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/MainActivity.java +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/activities/MainActivity.java @@ -206,6 +206,8 @@ public class MainActivity extends BaseActivity implements Action1, Ha return (Math.round(value * Math.pow(10, n))) / (Math.pow(10, n)); } + PushNotificationManager pushNotificationManager; + @Override protected int getLayoutResId() { return R.layout.activity_main; @@ -222,8 +224,7 @@ public class MainActivity extends BaseActivity implements Action1, Ha //Check if reminder alarm is set scheduleReminder(this); - PushNotificationManager pushNotificationManager = PushNotificationManager.getInstance(this); - pushNotificationManager.addPushDeviceUsingStoredToken(); + pushNotificationManager = PushNotificationManager.getInstance(this); new Select().from(HabitRPGUser.class).where(Condition.column("id").eq(hostConfig.getUser())).async().querySingle(userTransactionListener); @@ -352,8 +353,9 @@ public class MainActivity extends BaseActivity implements Action1, Ha displayDeathDialogIfNeeded(); if (!fromLocalDb) { - displayNewInboxMessagesBadge(); + pushNotificationManager.setUser(user); + pushNotificationManager.addPushDeviceUsingStoredToken(); // Update the oldEntries new Thread(() -> { diff --git a/Habitica/src/main/java/com/magicmicky/habitrpgwrapper/lib/models/HabitRPGUser.java b/Habitica/src/main/java/com/magicmicky/habitrpgwrapper/lib/models/HabitRPGUser.java index eea72813f..5521c042c 100644 --- a/Habitica/src/main/java/com/magicmicky/habitrpgwrapper/lib/models/HabitRPGUser.java +++ b/Habitica/src/main/java/com/magicmicky/habitrpgwrapper/lib/models/HabitRPGUser.java @@ -99,6 +99,8 @@ public class HabitRPGUser extends BaseModel { foreignColumnName = "user_id")}) private Invitations invitations; + private List pushDevices = new ArrayList(); + private Purchases purchased; private TasksOrder tasksOrder; @@ -297,6 +299,14 @@ public class HabitRPGUser extends BaseModel { this.tasksOrder = tasksOrder; } + public List getPushDevices() { + return this.pushDevices; + } + + public void setPushDevices(List pushDevices) { + this.pushDevices = pushDevices; + } + @Override public void save() { // We need to set the user_id to all other objects diff --git a/Habitica/src/main/java/com/magicmicky/habitrpgwrapper/lib/models/PushDevice.java b/Habitica/src/main/java/com/magicmicky/habitrpgwrapper/lib/models/PushDevice.java new file mode 100644 index 000000000..c70ec2a06 --- /dev/null +++ b/Habitica/src/main/java/com/magicmicky/habitrpgwrapper/lib/models/PushDevice.java @@ -0,0 +1,34 @@ +package com.magicmicky.habitrpgwrapper.lib.models; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +/** + * Created by keithholliday on 7/5/16. + */ +public class PushDevice { + + @SerializedName("regId") + @Expose + private String regId; + + @SerializedName("type") + @Expose + private String type; + + public String getRegId() { + return this.regId; + } + + public void setRegId(String regId) { + this.regId = regId; + } + + public String getType() { + return this.type; + } + + public void setType(String type) { + this.type = type; + } +}