我需要显示一个简单的本地通知,以了解其工作原理。为此,我编写了以下代码:
askPermissions = async () => {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
return finalStatus === 'granted';
};
sendNotificationImmediately = async () => {
const notificationId = await Notifications.presentLocalNotificationAsync({
title: 'This is header',
body: 'This is body',
});
console.log(notificationId);
};
并以这种方式调用此方法:
componentDidMount() {
if (this.askPermissions()) {
console.log('Permission presents');
this.sendNotificationImmediately();
} else {
console.log('Denied');
}
}
要编写此代码,我使用了this教程。但是,当我运行应用程序时,未显示通知,但存在权限。那么,问题的原因是什么,我该如何解决?
答案 0 :(得分:0)
您可以尝试一下,
askPermissions = async () => {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
} else if (finalStatus === 'granted') {
Notifications.presentLocalNotificationAsync({
title: 'This is header',
body: 'This is body',
});
}
componentDidMount() {
this.askPermissions();
}
希望这会有所帮助。