Skip to content

Commit 8e4235a

Browse files
committed
fix missing token updates
PushToken values can change and this wasn't being account for in checkAndTriggerSubscriptionChanged(). This fixes a bug where the backend would not get the new token when toggling notification push off and on then refreshing the page. Also logging instead of throwing when there is no push sub yet when updatePushSubscriptionNotificationTypes is called. This isn't an error as the value is simply populated when the sub is created.
1 parent e9134ee commit 8e4235a

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/shared/helpers/EventHelper.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ export default class EventHelper {
3232

3333
const appState = await Database.getAppState();
3434
const { lastKnownPushEnabled, lastKnownPushId, lastKnownPushToken, lastKnownOptedIn } = appState;
35+
36+
const currentPushToken = await MainHelper.getCurrentPushToken();
37+
38+
const pushModel = await OneSignal.coreDirector.getPushSubscriptionModel();
39+
const pushSubscriptionId = pushModel?.data?.id;
40+
3541
const didStateChange = (
3642
lastKnownPushEnabled === null ||
37-
isPushEnabled !== lastKnownPushEnabled
43+
isPushEnabled !== lastKnownPushEnabled ||
44+
currentPushToken !== lastKnownPushToken ||
45+
pushSubscriptionId !== lastKnownPushId
3846
);
3947
if (!didStateChange) {
4048
return;
4149
}
42-
Log.info(
43-
`The user's subscription state changed from ` +
44-
`${lastKnownPushEnabled === null ? '(not stored)' : lastKnownPushEnabled}${isPushEnabled}`
45-
);
4650

4751
// update notification_types via core module
48-
await context.subscriptionManager.updateNotificationTypes();
49-
50-
const currentPushToken = await MainHelper.getCurrentPushToken();
51-
const pushModel: OSModel<SubscriptionModel> = await OneSignal.coreDirector.getCurrentPushSubscriptionModel();
52-
const pushSubscriptionId = pushModel.data?.id;
52+
await context.subscriptionManager.updateNotificationTypes()
5353

5454
await Database.setIsPushEnabled(isPushEnabled);
5555
appState.lastKnownPushEnabled = isPushEnabled;
@@ -71,7 +71,7 @@ export default class EventHelper {
7171
optedIn: isOptedIn,
7272
}
7373
};
74-
74+
Log.info("Push Subscription state changed: ", change);
7575
EventHelper.triggerSubscriptionChanged(change);
7676
}
7777

src/shared/managers/SubscriptionManager.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,8 @@ export class SubscriptionManager {
209209
async updatePushSubscriptionNotificationTypes(notificationTypes: SubscriptionStateKind): Promise<void> {
210210
const pushModel = await OneSignal.coreDirector.getPushSubscriptionModel();
211211
if (!pushModel) {
212-
throw new OneSignalError(
213-
`Cannot update notification_types for push subscription model because it does not exist.`
214-
);
212+
Log.info("No Push Subscription yet to update notification_types.");
213+
return;
215214
}
216215

217216
pushModel.set("notification_types", notificationTypes);

0 commit comments

Comments
 (0)