expo#Notifications TypeScript Examples

The following examples show how to use expo#Notifications. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Notifications.ts    From companion-kit with MIT License 6 votes vote down vote up
async getToken() {
        if (this.hasPermission && ExpoConstants.isDevice) {
            if (this._tokenCached) {
                return this._tokenCached;
            }

            try {
                const token = await Notifications.getExpoPushTokenAsync();
                if (!token) {
                    logger.error('Notifications.getExpoPushTokenAsync() returned `null` while `this._currentStatus` =', this._currentStatus);
                } else {
                    this._tokenCached = token;
                }
                return token;
            } catch (err) {
                logger.log('Failed to get notifications token');
                logger.error(err);
            }
        }

        return null;
    }
Example #2
Source File: Notifications.ts    From companion-kit with MIT License 6 votes vote down vote up
private async scheduleRetentionNotifications(time: NotificationTime, startDateMS: number) {
        const settings = { name: this.user.firstName };
        const result: NotificationResult[] = [];

        const messages = time === NotificationTime.ExactTime
            ? getMessagesForExactTime(startDateMS, SCHEDULE_DAYS_COUNT, settings)
            : getRandomUniqMessages(time, SCHEDULE_DAYS_COUNT, settings);

        for (let i = 0; i < messages.length; i++) {
            const m = messages[i];
            const date = addDaysToDate(startDateMS, i);
            const schedulingOptions: NotificationSchedulingOptions = { time: date };

            const data = {
                type: NotificationTypes.Retention,
            };

            const notification: LocalNotification = {
                title: Localization.Current.MobileProject.projectName,
                data,
                body: m,
                ios: { sound: true },
                android: Platform.OS === 'android' ? { channelId: AndroidChannels.Default } : null,
            };

            await Notifications.scheduleLocalNotificationAsync(notification, schedulingOptions);
            logger.log('scheduleNotifications with message:', m, '| notification time is:', date);

            const dateStr = new Date(schedulingOptions.time).toUTCString();
            result.push({ body: notification.body, date: dateStr });
        }

        return result;
    }
Example #3
Source File: Notifications.ts    From companion-kit with MIT License 6 votes vote down vote up
constructor(private readonly user: IUserNameProvider) {
        if (!user) {
            throw new Error('IUserController is required');
        }
        this._notificationsSubscription = Notifications.addListener(this._onNotificationReceived);

        // TEST
        // setTimeout(() => {

        //     const data: NotificationData = {
        //         type: NotificationTypes.CustomPrompt,
        //         originalText: 'bla-bla',
        //         promptId: '123',
        //         // phrasePrompt: 'You mentioned "life sucks". Can you tell me more about that?',
        //         // phrase: 'life sucks',
        //         // promptId: '2e3bdb9a-89f3-49bb-ac33-06308e368969',
        //         // originalText: 'Is there anything that feels frustrating right now?',
        //     };

        //     this._onNotificationReceived({
        //         isMultiple: false,
        //         remote: true,
        //         origin: 'selected',
        //         data,
        //     });
        // }, 7000);
    }
Example #4
Source File: Notifications.ts    From companion-kit with MIT License 5 votes vote down vote up
async createAndroidChannel() {
        await Notifications.createChannelAndroidAsync(AndroidChannels.Default, {
            name:  Localization.Current.MobileProject.projectName,
            sound: true,
            vibrate: true,
        });
    }
Example #5
Source File: Notifications.ts    From companion-kit with MIT License 5 votes vote down vote up
async deleteAndroidChannel() {
        await Notifications.deleteChannelAndroidAsync(AndroidChannels.Default);
    }
Example #6
Source File: Notifications.ts    From companion-kit with MIT License 5 votes vote down vote up
public resetSchedule = async () => {
        await Notifications.cancelAllScheduledNotificationsAsync();

        if (Platform.OS === 'android') {
            await this.deleteAndroidChannel();
        }
    }