-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat(kno-6559): add KnockPushNotificationProvider #252
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
ef352b7
Add new package directory for expo SDK
mattmikolay 7f2d7f8
Add tooling config
mattmikolay 8d205bc
Copy KnockExpoPushNotificationProvider to expo package
mattmikolay 8c6313f
Add root index.ts to expo package
mattmikolay 4bbf3dc
Re-export everything from react native package
mattmikolay 4c221de
Remove push code from react native SDK
mattmikolay 6c550a5
Remove peerDependenciesMeta from expo package.json
mattmikolay 1b6575f
Remove expo dependencies from react native sdk
mattmikolay 7a3947b
Remove unnecessary export
mattmikolay 286aaa6
Update sample expo app to use expo sdk
mattmikolay 228290a
Update more sample app imports
mattmikolay b891b4c
Use a type import in sample app
mattmikolay badb827
Add expo README
mattmikolay eef5435
Update rn sdk README
mattmikolay 94952fb
Update rn sdk vite config
mattmikolay 1eef956
Update README
mattmikolay ec11e9f
Merge branch 'main' into mattmik-kno-6559-react-native-sdk-ensure-exp…
mattmikolay 02bfff7
Update @typescript-eslint/parser
mattmikolay 6adc41d
Update version of expo package
mattmikolay f8fc1de
Add changeset
mattmikolay f13bde3
Update Expo README
mattmikolay 69c414d
Update RN SDK README
mattmikolay a652c6a
Update expo SDK README
mattmikolay 30f6c35
Set @knocklabs/expo version to 0.0.0
mattmikolay f6295c3
Add KnockPushNotificationProvider
mattmikolay 936bfc1
Merge branch 'main' into mattmik-generic-rn-push-provider
mattmikolay ab9cc9c
Remove outdated changeset
mattmikolay d651973
Move KnockPushNotificationProvider to react-native package
mattmikolay 6b9020d
Add missing export to react-native/src/index.ts
mattmikolay 3d9ea6c
Extend type KnockPushNotificationContextType in expo package
mattmikolay 298e47e
Use KnockPushNotificationProvider in KnockExpoPushNotificationProvider
mattmikolay 24eccfb
Add changeset
mattmikolay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@knocklabs/react-native": minor | ||
"@knocklabs/expo": minor | ||
--- | ||
|
||
Add KnockPushNotificationProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./modules/feed"; | ||
export * from "./modules/push"; | ||
export * from "@knocklabs/react-core"; | ||
export * from "./assets"; |
95 changes: 95 additions & 0 deletions
95
packages/react-native/src/modules/push/KnockPushNotificationProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { ChannelData } from "@knocklabs/client"; | ||
import { useKnockClient } from "@knocklabs/react-core"; | ||
import React, { createContext, useCallback, useContext } from "react"; | ||
|
||
export interface KnockPushNotificationContextType { | ||
registerPushTokenToChannel(token: string, channelId: string): Promise<void>; | ||
unregisterPushTokenFromChannel( | ||
token: string, | ||
channelId: string, | ||
): Promise<void>; | ||
} | ||
|
||
const KnockPushNotificationContext = createContext< | ||
KnockPushNotificationContextType | undefined | ||
>(undefined); | ||
|
||
export interface KnockPushNotificationProviderProps { | ||
children?: React.ReactElement; | ||
} | ||
|
||
export const KnockPushNotificationProvider: React.FC< | ||
KnockPushNotificationProviderProps | ||
> = ({ children }) => { | ||
const knockClient = useKnockClient(); | ||
|
||
const registerNewTokenDataOnServer = useCallback( | ||
async (tokens: string[], channelId: string): Promise<ChannelData> => { | ||
return knockClient.user.setChannelData({ | ||
channelId: channelId, | ||
channelData: { tokens: tokens }, | ||
}); | ||
}, | ||
[knockClient], | ||
); | ||
|
||
const registerPushTokenToChannel = useCallback( | ||
async (token: string, channelId: string): Promise<void> => { | ||
knockClient.user | ||
.getChannelData({ channelId: channelId }) | ||
.then((result: ChannelData) => { | ||
const tokens: string[] = result.data["tokens"]; | ||
if (!tokens.includes(token)) { | ||
tokens.push(token); | ||
return registerNewTokenDataOnServer(tokens, channelId); | ||
} | ||
knockClient.log("[Knock] registerPushTokenToChannel success"); | ||
}) | ||
.catch((_) => { | ||
// No data registered on that channel for that user, we'll create a new record | ||
return registerNewTokenDataOnServer([token], channelId); | ||
}); | ||
}, | ||
[knockClient, registerNewTokenDataOnServer], | ||
); | ||
|
||
const unregisterPushTokenFromChannel = useCallback( | ||
async (token: string, channelId: string): Promise<void> => { | ||
knockClient.user | ||
.getChannelData({ channelId: channelId }) | ||
.then((result: ChannelData) => { | ||
const tokens: string[] = result.data["tokens"]; | ||
const updatedTokens = tokens.filter( | ||
(channelToken) => channelToken !== token, | ||
); | ||
knockClient.log("unregisterPushTokenFromChannel success"); | ||
return registerNewTokenDataOnServer(updatedTokens, channelId); | ||
}) | ||
.catch((error) => { | ||
console.error( | ||
`[Knock] Error unregistering push token from channel:`, | ||
error, | ||
); | ||
}); | ||
}, | ||
[knockClient, registerNewTokenDataOnServer], | ||
); | ||
|
||
return ( | ||
<KnockPushNotificationContext.Provider | ||
value={{ registerPushTokenToChannel, unregisterPushTokenFromChannel }} | ||
> | ||
{children} | ||
</KnockPushNotificationContext.Provider> | ||
); | ||
}; | ||
|
||
export const usePushNotifications = (): KnockPushNotificationContextType => { | ||
const context = useContext(KnockPushNotificationContext); | ||
if (context === undefined) { | ||
throw new Error( | ||
"[Knock] usePushNotifications must be used within a KnockPushNotificationProvider", | ||
); | ||
} | ||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./KnockPushNotificationProvider"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it’s worth, this
KnockPushNotificationProvider
does not use any APIs specific to React Native. Alternatively, we could add this to@knocklabs/react-core
.@knocklabs/react-native
already re-exports everything from@knocklabs/react-core
, so it would still be available for use via our React Native SDK:javascript/packages/react-native/src/index.ts
Line 2 in 5d60ed0