-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added notification channels configurations
- Loading branch information
Gonzalo Lopez
committed
Jun 13, 2024
1 parent
9a70086
commit 3118a96
Showing
8 changed files
with
314 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import notifee, {AndroidImportance} from '@notifee/react-native'; | ||
import {isObject, isString} from '..'; | ||
|
||
export const parseChannelConfiguration = (params = {}) => { | ||
if (!params || !isObject(params)) return null; | ||
|
||
const {name, id = '', description = ''} = params; | ||
|
||
if (!name || !isString(name)) return null; | ||
|
||
const isValidDescription = !!description && isString(description); | ||
const hasValidId = !!id && isString(id); | ||
|
||
return { | ||
name, | ||
id: hasValidId ? id : name, | ||
importance: AndroidImportance.HIGH, | ||
...(isValidDescription && { | ||
description, | ||
}), | ||
}; | ||
}; | ||
|
||
export const parseNotificationChannel = (channel) => { | ||
const channelType = typeof channel; | ||
const allowedConfigs = ['string', 'object']; | ||
|
||
if (!allowedConfigs.includes(channelType)) return null; | ||
const channelData = channelType === 'string' ? {name: channel} : channel; | ||
|
||
const parsedChannel = parseChannelConfiguration(channelData); | ||
|
||
return parsedChannel; | ||
}; | ||
|
||
/* eslint-disable consistent-return */ | ||
export const makeNotificationChannel = async (channelConfig = {}) => { | ||
const {id, name} = channelConfig; | ||
|
||
if (!id || !name || !isString(id) || !isString(name)) return null; | ||
|
||
try { | ||
await notifee.createChannel(channelConfig); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}; | ||
|
||
/* eslint-disable consistent-return */ | ||
export const makeNotificationChannels = async (channelConfigs) => { | ||
try { | ||
await notifee.createChannels(channelConfigs); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}; | ||
|
||
/* eslint-disable consistent-return */ | ||
export const makeDefaultChannel = async () => { | ||
try { | ||
const parsedChannel = parseChannelConfiguration({ | ||
id: 'default_channel', | ||
name: 'Common notifications', | ||
description: 'Default channel to receive notifications', | ||
}); | ||
|
||
await makeNotificationChannel(parsedChannel); | ||
} catch (error) { | ||
return Promise.reject(error); | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.