null is not an object (evaluating '_ExponentNotifications.default.cancelAllScheduledNotificationsAsync') React Native

Viewed 388

I am getting this error when evaluating Notifications.cancelAllScheduledNotificationsAsync() in React Native application and android emulator.

Error Stack

 TypeError: null is not an object (evaluating '_ExponentNotifications.default.cancelAllScheduledNotificationsAsync')
cancelAllScheduledNotificationsAsync@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:110086:44
_callee$
tryCatch@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26262:23
invoke@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26438:32
tryCatch@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26262:23
invoke@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26338:30
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26368:19
tryCallTwo@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:27627:9
doResolve@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:27791:25
Promise@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:27650:14
callInvokeWithMethodAndArg@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26367:33
enqueue@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26372:157
async@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:26389:69
tryCallOne@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:27618:16
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:27719:27
_callTimer@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:31173:17
_callImmediatesPass@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:31209:19
callImmediates@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:31428:33
callImmediates@[native code]
__callImmediates@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:3238:35
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:3015:34
__guard@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:3221:15
flushedQueue@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:3014:21
flushedQueue@[native code]
invokeCallbackAndReturnFlushedQueue@[native code]

Notifications.js

import {Notifications} from 'expo'
import * as Permissions from 'expo-permissions';
import {AsyncStorage} from "react-native";

const notificationsKey = "@questions-flash-cards-key:notifications";

export function clearLocalNotifications() {
    return AsyncStorage.removeItem(notificationsKey).then(() => Notifications.cancelAllScheduledNotificationsAsync())
}

const notification = {
    title: "You forgot solving quiz today.",
    body: "Answer quiz per day to improve yourself.",
    ios: {
        sound: true
    },
    android: {
        sound: true,
        vibrate: true,
    },
};


export async function setLocalNotification() {
    const {status} = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    if (status !== 'granted') {
        await Permissions.askAsync(Permissions.NOTIFICATIONS);
    }

    AsyncStorage.getItem(notificationsKey).then(JSON.parse).then(async (data) => {
        if (data === null) {
            Notifications.cancelAllScheduledNotificationsAsync().then(() => {
                let tomorrow = new Date(Date.now());
                tomorrow.setDate(tomorrow.getDate() + 1);
                tomorrow.setHours(12);
                tomorrow.setMinutes(0);
                Notifications.scheduleLocalNotificationAsync(notification, {
                    time: tomorrow, repeat: "day"
                })
            })
        }
    })
}

0 Answers
Related