Bro*_*oel 9 notifications react-native expo
我正在使用EXPO和React Native为Android和iOS创建一个应用程序。此代码中有一个怪异的行为:它在Expo App上正常运行,但在实际设备(独立版本)中不运行(在iOS和Android上发生)。
这些本地通知的逻辑如下:目的是向用户显示3个本地通知:
我该怎么做?
第一次打开应用程序时,我问用户是否要查看通知。如果用户说是,我将该信息写入本地存储中,然后我请求USER_FACING_NOTIFICATIONS显示本地通知的权限。
我正在使用,USER_FACING_NOTIFICATIONS因为我不发送远程通知。
然后,用户第二次打开该应用程序,请执行以下操作:
在这里,部分代码:
componentDidMount() {
cancelAllScheduled()
.then(() => {
getUserNotificationsPermission().then(userWants => {
if (userWants) {
getDeviceNotificationsPermission().then(grantedByDevice => {
if (grantedByDevice) {
scheduleNotifications();
}
});
}
});
})
.catch(() => {
// :shrug:
});
}
Run Code Online (Sandbox Code Playgroud)
在这里,所有这些utils功能:
// Cancel all the notifications
const cancelAllScheduled = () => {
return ExpoNotifications.cancelAllScheduledNotificationsAsync();
}
// Check in local storage if the user WANTS to see notifications
const getUserNotificationsPermission = () => {
return AsyncStorage.getItem('userNotificationPermission').then(data => {
let isEnabled;
try {
isEnabled = JSON.parse(data);
} catch (error) {
isEnabled = false;
}
return isEnabled;
});
};
// Check if user/device ALLOW US to show notifications
const getDeviceNotificationsPermission = () => {
return Permissions.getAsync(Permissions.USER_FACING_NOTIFICATIONS).then(({ status }) => {
return status === 'granted';
});
};
// Add days
// https://stackoverflow.com/a/19691491/1815449
const _addDays = (date, days) => {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
};
// Schedule all the notifications
// One notification is going to be shown on 1 week
// Other notification in 1 month
// Other notification in 2 month
const scheduleNotifications = () => {
const now = new Date();
const oneWeek = {
title: 'We miss you!',
body: "Come to see what we've been up to and share your latest adventures with us!",
data: {
type: 'WEEKLY',
},
};
const oneWeekOptions = { time: _addDays(now, 7) };
ExpoNotifications.scheduleLocalNotificationAsync(oneWeek, oneWeekOptions);
const oneMonth = {
title: 'We miss you!',
body: "Come to see what we've been up to and share your latest adventures with us!",
data: {
type: 'MONTHLY',
},
};
const oneMonthOptions = { time: _addDays(now, 30) };
ExpoNotifications.scheduleLocalNotificationAsync(oneMonth, oneMonthOptions);
const twoMonth = {
title: 'We miss you!',
body: "Come to see what we've been up to and share your latest adventures with us!",
data: {
type: 'MONTHLY 2',
},
};
const twoMonthOptions = { time: _addDays(now, 60) };
ExpoNotifications.scheduleLocalNotificationAsync(twoMonth, twoMonthOptions);
};
Run Code Online (Sandbox Code Playgroud)
您看到任何有关为何无法正常工作的线索吗?我已经打开了该应用程序并授予了所有权限,然后杀死并再次打开该应用程序,接近1.000次。最近几个月,我没有打开该应用程序9天(我做了3次),但从未收到任何通知。但是,通知会正确显示在Expo Dev App中。
附注:我cancelAllScheduled每次用户打开应用程序时都会这样做,因为每次用户打开应用程序时都需要重置这些通知,因为我想在“上次打开应用程序”之后几周显示
PS2:这是我用来实现它的文档:
似乎这就是问题所在:https://github.com/expo/expo/issues/4121 通知有效,但由于我将它们安排为 1 周、1 个月或更长时间,因此未显示通知大多数设备在安排通知后重新启动。
我已经等了 1 周了,没有在 iOS 中再次打开应用程序,也没有重新启动设备,甚至没有升级 SO,我都可以看到通知。我还没有在Android 上测试过。我可能需要将 Android 手机保持连接状态 1 周,看看会发生什么。
| 归档时间: |
|
| 查看次数: |
594 次 |
| 最近记录: |