mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-13 17:51:57 +00:00
Ensured user was subscribed to notification only when they do not have the device token
This commit is contained in:
parent
c8b05d54b8
commit
2bf908f9de
4 changed files with 74 additions and 8 deletions
|
|
@ -10,6 +10,8 @@ import com.google.firebase.messaging.RemoteMessage;
|
||||||
import com.habitrpg.android.habitica.APIHelper;
|
import com.habitrpg.android.habitica.APIHelper;
|
||||||
import com.habitrpg.android.habitica.HabiticaApplication;
|
import com.habitrpg.android.habitica.HabiticaApplication;
|
||||||
import com.habitrpg.android.habitica.callbacks.HabitRPGUserCallback;
|
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.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -40,12 +42,18 @@ public class PushNotificationManager {
|
||||||
private String refreshedToken;
|
private String refreshedToken;
|
||||||
private SharedPreferences sharedPreferences;
|
private SharedPreferences sharedPreferences;
|
||||||
private Context context;
|
private Context context;
|
||||||
|
private HabitRPGUser user;
|
||||||
|
|
||||||
protected PushNotificationManager(Context context) {
|
protected PushNotificationManager(Context context) {
|
||||||
HabiticaApplication.getInstance(context).getComponent().inject(this);
|
HabiticaApplication.getInstance(context).getComponent().inject(this);
|
||||||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUser(HabitRPGUser user) {
|
||||||
|
Log.v("test", "sdffsdf");
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
public static PushNotificationManager getInstance(Context context) {
|
public static PushNotificationManager getInstance(Context context) {
|
||||||
if(instance == null) {
|
if(instance == null) {
|
||||||
instance = new PushNotificationManager(context);
|
instance = new PushNotificationManager(context);
|
||||||
|
|
@ -66,11 +74,6 @@ public class PushNotificationManager {
|
||||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||||
editor.putString(DEVICE_TOKEN_PREFERENCE_KEY, refreshedToken);
|
editor.putString(DEVICE_TOKEN_PREFERENCE_KEY, refreshedToken);
|
||||||
editor.commit();
|
editor.commit();
|
||||||
|
|
||||||
|
|
||||||
if (this.userIsSubscribedToNotifications()) {
|
|
||||||
this.addPushDeviceUsingStoredToken();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//@TODO: Use preferences
|
//@TODO: Use preferences
|
||||||
|
|
@ -82,6 +85,14 @@ public class PushNotificationManager {
|
||||||
if (this.refreshedToken == null || this.refreshedToken.isEmpty()) {
|
if (this.refreshedToken == null || this.refreshedToken.isEmpty()) {
|
||||||
return;
|
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>();
|
Map<String, String> pushDeviceData = new HashMap<String, String>();
|
||||||
pushDeviceData.put("regId", this.refreshedToken);
|
pushDeviceData.put("regId", this.refreshedToken);
|
||||||
|
|
@ -97,6 +108,15 @@ public class PushNotificationManager {
|
||||||
.subscribe(aVoid -> {}, throwable -> {});
|
.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) {
|
public void displayNotification (RemoteMessage remoteMessage) {
|
||||||
String remoteMessageIdentifier = remoteMessage.getData().get("identifier");
|
String remoteMessageIdentifier = remoteMessage.getData().get("identifier");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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));
|
return (Math.round(value * Math.pow(10, n))) / (Math.pow(10, n));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PushNotificationManager pushNotificationManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getLayoutResId() {
|
protected int getLayoutResId() {
|
||||||
return R.layout.activity_main;
|
return R.layout.activity_main;
|
||||||
|
|
@ -222,8 +224,7 @@ public class MainActivity extends BaseActivity implements Action1<Throwable>, Ha
|
||||||
//Check if reminder alarm is set
|
//Check if reminder alarm is set
|
||||||
scheduleReminder(this);
|
scheduleReminder(this);
|
||||||
|
|
||||||
PushNotificationManager pushNotificationManager = PushNotificationManager.getInstance(this);
|
pushNotificationManager = PushNotificationManager.getInstance(this);
|
||||||
pushNotificationManager.addPushDeviceUsingStoredToken();
|
|
||||||
|
|
||||||
new Select().from(HabitRPGUser.class).where(Condition.column("id").eq(hostConfig.getUser())).async().querySingle(userTransactionListener);
|
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();
|
displayDeathDialogIfNeeded();
|
||||||
|
|
||||||
if (!fromLocalDb) {
|
if (!fromLocalDb) {
|
||||||
|
|
||||||
displayNewInboxMessagesBadge();
|
displayNewInboxMessagesBadge();
|
||||||
|
pushNotificationManager.setUser(user);
|
||||||
|
pushNotificationManager.addPushDeviceUsingStoredToken();
|
||||||
|
|
||||||
// Update the oldEntries
|
// Update the oldEntries
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
|
|
|
||||||
|
|
@ -99,6 +99,8 @@ public class HabitRPGUser extends BaseModel {
|
||||||
foreignColumnName = "user_id")})
|
foreignColumnName = "user_id")})
|
||||||
private Invitations invitations;
|
private Invitations invitations;
|
||||||
|
|
||||||
|
private List<PushDevice> pushDevices = new ArrayList<PushDevice>();
|
||||||
|
|
||||||
private Purchases purchased;
|
private Purchases purchased;
|
||||||
|
|
||||||
private TasksOrder tasksOrder;
|
private TasksOrder tasksOrder;
|
||||||
|
|
@ -297,6 +299,14 @@ public class HabitRPGUser extends BaseModel {
|
||||||
this.tasksOrder = tasksOrder;
|
this.tasksOrder = tasksOrder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<PushDevice> getPushDevices() {
|
||||||
|
return this.pushDevices;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPushDevices(List<PushDevice> pushDevices) {
|
||||||
|
this.pushDevices = pushDevices;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void save() {
|
public void save() {
|
||||||
// We need to set the user_id to all other objects
|
// We need to set the user_id to all other objects
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue