Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Push Subscription/Permission Getters to Async #1649

Merged
merged 7 commits into from Mar 12, 2024

Conversation

jennantilla
Copy link
Contributor

@jennantilla jennantilla commented Feb 13, 2024

Description

One Line Summary

Update Push Subscription/Permission Getters to Async

Details

Motivation

We received reports of listeners not behaving as expected; certain getters were returning null due to the value not being available until returned from the native bridge. Updating getter methods to async will address this issue.

Scope

New async methods were added while also marking old methods as deprecated. Also updated requestPermission and permssionNative to account for changes made to these getters.

⚠️ In order to align with other SDKs, there is a minor "breaking" API change where the push subscription observer will be passed nullable properties now. This will also be communicated in the release notes for 5.1.0.

Testing

Manual testing

Tested running and building app on Android 14 emulator and iOS 17.2 Emulator, to ensure new methods work as expected.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • If it is hard to explain how any codes changes are related to each other then it most likely needs to be more than one PR
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
    • Simplify with less code, followed by splitting up code into well named functions and variables, followed by adding comments to the code.
  • I have reviewed this PR myself, ensuring it meets each checklist item
    • WIP (Work In Progress) is ok, but explain what is still in progress and what you would like feedback on. Start the PR title with "WIP" to indicate this.

This change is Reviewable

- Create new async methods
- Mark old getters as deprecated
- Update permissionNative to access value from bridge vs. local
@jennantilla
Copy link
Contributor Author

@nan-li I Successfully reran test scenarios on Android 14 Emulator and iOS 17.2 Emulator:

  1. On a new app install, immediately call these new getters for push subscription properties and permission. See they are null and false. Which is correct at the beginning of a new installation.
LOG  Permission:  false
LOG  Push Id:  null
LOG  Token:  null
LOG  OptedIn Id:  false
  1. When changes to push subscription or permission occur, these new async getters return the correct values immediately, whereas the previous getters were still returning old data if called too early after the changes.

  2. On new cold starts (not first install), get these values immediately after initialization.See that the new async getters return the correct values.The old getters were returning false and undefined when called this early in the app startup process.

 LOG  Dev App OneSignal: push subscription changed: {"previous":{"id":"fe2f91d6-a55c-4f43-a279-51d3c1d5c239","token":"d4PnmTy4SVyvdDdeozX13Z:APA91bFVB1wnb5bZpWVUomGnC84Rh2Oty-PZ8WFBKkmTHb6HfvC7SEhosxSFIyGXMTcRzDazIihASY_kZaot4O0j2fomAFzmc1vFxVmMGdhx0tJgkpLKz-xOKzMel-CL4IGl-PwXPyTh","optedIn":true},"current":{"id":"fe2f91d6-a55c-4f43-a279-51d3c1d5c239","token":"d4PnmTy4SVyvdDdeozX13Z:APA91bFVB1wnb5bZpWVUomGnC84Rh2Oty-PZ8WFBKkmTHb6HfvC7SEhosxSFIyGXMTcRzDazIihASY_kZaot4O0j2fomAFzmc1vFxVmMGdhx0tJgkpLKz-xOKzMel-CL4IGl-PwXPyTh","optedIn":true}}
 LOG  pushSubscription.id: undefined
 LOG  pushSubscription.token: undefined
 LOG  pushSubscription.optedIn: undefined
 LOG  getTokenAsync: d4PnmTy4SVyvdDdeozX13Z:APA91bFVB1wnb5bZpWVUomGnC84Rh2Oty-PZ8WFBKkmTHb6HfvC7SEhosxSFIyGXMTcRzDazIihASY_kZaot4O0j2fomAFzmc1vFxVmMGdhx0tJgkpLKz-xOKzMel-CL4IGl-PwXPyTh
 LOG  getIdAsync: fe2f91d6-a55c-4f43-a279-51d3c1d5c239
 LOG  getOptedInAsync: true
  1. Confirm the old getters still "work" and are still being updated and supported. A few second after step 3 above, call those getters and see they do still provide the correct values when they are called later after app start.
WARN  OneSignal: This method has been deprecated. Use getIdAsync instead for getting push subscription id.
LOG  pushSubscription.id: 75250866-8aa3-4b88-bde5-c9d7b97aa2da
WARN  OneSignal: This method has been deprecated. Use getTokenAsync instead for getting push subscription token.
LOG  pushSubscription.token: fVdvkAYSS5GM3wiwury1fw:APA91bHGfpcha8_PuhpnCRqau9On0DYhQFfflEzpjvikPN_sqOU3rMKgcBiAFZCP2_oLubWiIsp1eh0QxJM_rfR54Uge1CSFWtIm5rIcjASvOh2k48ZMNcPTsS_hwUeLDzCNkMRuRy38
WARN  OneSignal: This method has been deprecated. Use getOptedInAsync instead for getting push subscription opted in status.
LOG  pushSubscription.optedIn: true
WARN  OneSignal: This method has been deprecated. Use getPermissionAsync instead for getting notification permission status.
LOG  Notifications.hasPermission: true
  1. However, we had actually been passing along the empty string "" or the string literal "nil". That is also fixed in this PR
 LOG  OneSignal: pushSub changed: {"previous":{"id":null,"token":null,"optedIn":false},"current":{"id":"fe2f91d6-a55c-4f43-a279-51d3c1d5c239","token":null,"optedIn":false}}


//Previous state
NSMutableDictionary *previousObject = [NSMutableDictionary new];
previousObject[@"token"] = (state.previous.token && ![state.previous.token isEqualToString:@""]) ? state.previous.token : [NSNull null];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines get pretty complex, we can consider refactoring these into getter helpers, that can also be used for the User State observer., once this PR and that one are combined / merged.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also update the PR description to callout the API "breaking-sorta" change that the push subscription observer will be passed nullable properties now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for flagging these! I've edited the PR description with the minor breaking change and will be sure to include it in release notes. Refactoring mentioned above will happen in a future PR.

@jennantilla jennantilla merged commit d789599 into major_release_5.0.0 Mar 12, 2024
4 checks passed
@jennantilla jennantilla mentioned this pull request Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants