Ensured user was subscribed to notification only when they do not have the device token

This commit is contained in:
Keith Holliday 2016-07-05 12:20:10 -05:00
parent c8b05d54b8
commit 2bf908f9de
4 changed files with 74 additions and 8 deletions

View file

@ -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<String, String> pushDeviceData = new HashMap<String, String>();
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");

View file

@ -206,6 +206,8 @@ public class MainActivity extends BaseActivity implements Action1<Throwable>, 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<Throwable>, 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<Throwable>, Ha
displayDeathDialogIfNeeded();
if (!fromLocalDb) {
displayNewInboxMessagesBadge();
pushNotificationManager.setUser(user);
pushNotificationManager.addPushDeviceUsingStoredToken();
// Update the oldEntries
new Thread(() -> {

View file

@ -99,6 +99,8 @@ public class HabitRPGUser extends BaseModel {
foreignColumnName = "user_id")})
private Invitations invitations;
private List<PushDevice> pushDevices = new ArrayList<PushDevice>();
private Purchases purchased;
private TasksOrder tasksOrder;
@ -297,6 +299,14 @@ public class HabitRPGUser extends BaseModel {
this.tasksOrder = tasksOrder;
}
public List<PushDevice> getPushDevices() {
return this.pushDevices;
}
public void setPushDevices(List<PushDevice> pushDevices) {
this.pushDevices = pushDevices;
}
@Override
public void save() {
// We need to set the user_id to all other objects

View file

@ -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;
}
}