-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add push notification support for expo (#191)
* feat: Add push notification support for expo * fix: remove wrong readme * chore: improve docs
- Loading branch information
1 parent
f148100
commit e1df100
Showing
6 changed files
with
169 additions
and
12 deletions.
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
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 |
---|---|---|
|
@@ -5568,6 +5568,11 @@ react-native-mmkv-storage@^0.9.1: | |
resolved "https://registry.yarnpkg.com/react-native-mmkv-storage/-/react-native-mmkv-storage-0.9.1.tgz#0db7e8c1726713dce68704bb8795dc64096c8cbb" | ||
integrity sha512-FzSx4PKxK2ocT/OuKGlaVziWZyQYHYLUx9595i1oXY263C5mG19PN5RiBgEGL2S5lK4VGUCzO85GAcsrNPtpOg== | ||
|
||
react-native-permissions@^4.1.5: | ||
version "4.1.5" | ||
resolved "https://registry.yarnpkg.com/react-native-permissions/-/react-native-permissions-4.1.5.tgz#db4d1ddbf076570043f4fd4168f54bb6020aec92" | ||
integrity sha512-r6VMRacASmtRHS+GZ+5HQCp9p9kiE+UU9magHOZCXZLTJitdTuVHWZRrb4v4oqZGU+zAp3mZhTQftuMMv+WLUg== | ||
|
||
[email protected]: | ||
version "0.73.6" | ||
resolved "https://registry.npmjs.org/react-native/-/react-native-0.73.6.tgz" | ||
|
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
ConfigPlugin, | ||
withAppDelegate, | ||
withInfoPlist, | ||
} from '@expo/config-plugins'; | ||
import type { IntercomPluginProps } from './@types'; | ||
import { | ||
addObjcImports, | ||
findObjcFunctionCodeBlock, | ||
insertContentsInsideObjcFunctionBlock, | ||
} from '@expo/config-plugins/build/ios/codeMod'; | ||
|
||
const appDelegate: ConfigPlugin<IntercomPluginProps> = (_config) => | ||
withAppDelegate(_config, (config) => { | ||
const pushCode = ` | ||
// START INTERCOM PUSH | ||
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; | ||
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) | ||
completionHandler:^(BOOL granted, NSError *_Nullable error) { | ||
}]; | ||
[[UIApplication sharedApplication] registerForRemoteNotifications]; | ||
// END INTERCOM PUSH | ||
`; | ||
|
||
const setDeviceTokenCode = '[IntercomModule setDeviceToken:deviceToken];'; | ||
|
||
let stringContents = config.modResults.contents; | ||
stringContents = addObjcImports(stringContents, [ | ||
'<UserNotifications/UserNotifications.h>', | ||
]); | ||
|
||
if (!stringContents.includes(pushCode.trim())) { | ||
stringContents = insertContentsInsideObjcFunctionBlock( | ||
stringContents, | ||
'application didFinishLaunchingWithOptions:', | ||
pushCode, | ||
{ position: 'tailBeforeLastReturn' } | ||
); | ||
} | ||
|
||
const didRegisterBlock = findObjcFunctionCodeBlock( | ||
stringContents, | ||
'application didRegisterForRemoteNotificationsWithDeviceToken:' | ||
); | ||
|
||
if (!didRegisterBlock?.code.includes(setDeviceTokenCode)) { | ||
stringContents = insertContentsInsideObjcFunctionBlock( | ||
stringContents, | ||
'application didRegisterForRemoteNotificationsWithDeviceToken:', | ||
setDeviceTokenCode, | ||
{ position: 'tailBeforeLastReturn' } | ||
); | ||
} | ||
|
||
config.modResults.contents = stringContents; | ||
return config; | ||
}); | ||
|
||
const infoPlist: ConfigPlugin<IntercomPluginProps> = (_config) => { | ||
const newConfig = withInfoPlist(_config, (config) => { | ||
const keys = { remoteNotification: 'remote-notification' }; | ||
|
||
if (!config.modResults.UIBackgroundModes) { | ||
config.modResults.UIBackgroundModes = []; | ||
} | ||
|
||
if ( | ||
config.modResults.UIBackgroundModes?.indexOf(keys.remoteNotification) === | ||
-1 | ||
) { | ||
config.modResults.UIBackgroundModes?.push(keys.remoteNotification); | ||
} | ||
|
||
return config; | ||
}); | ||
|
||
return newConfig; | ||
}; | ||
|
||
export const withIntercomPushNotification: ConfigPlugin<IntercomPluginProps> = ( | ||
config, | ||
props | ||
) => { | ||
let newConfig = config; | ||
newConfig = appDelegate(config, props); | ||
newConfig = infoPlist(config, props); | ||
return newConfig; | ||
}; |
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