Я использую реагировать-нативные уведомления - библиотека версии 2.1.7 для получения уведомлений в мобильном приложении реагировать-нативное . Я не хочу настраивать связанных с уведомлениями слушателей , пока пользователь не предоставит разрешение на получение уведомлений.
Q1. В документации сказано, что настоятельно рекомендуется сохранять регистрацию слушателей в глобальном масштабе, а не в пределах экрана Какие проблемы мне следует ожидать, если я настрою слушателей на экране, , при котором пользователю предлагается предоставить разрешение?
Q2. Слушатель токена устройства NotificationsAndroid.setRegistrationTokenUpdateListener()
НЕ работает, если он находится внутри обещания. Что мне здесь не хватает? Пожалуйста, посмотрите мой код ниже.
// This function is called when the user clicks on the button "Provide permission to receive notifications."
const _requestPermissionNotification = async () => {
let hasPermission = false;
try {
hasPermission = await NotificationsAndroid.isRegisteredForRemoteNotifications();
}
catch (error) {
Alert.alert(
"Notification",
"To utilise the full functionality of this app, Permission to receive notifications is required.",
[{ text: "Ok." }]
);
} // end of: try/catch
if (hasPermission) {
// A. Register Token
// THIS LISTENER DOES NOT SEEM TO WORK UNLESS IT IS SET UP OUTSIDE THE COMPONENT!
NotificationsAndroid.setRegistrationTokenUpdateListener((deviceToken) => {
console.log("PermissionsScreen - setRegistrationTokenUpdateListener - deviceToken:", deviceToken);
});
// B. Pending Notifications
PendingNotifications.getInitialNotification()
.then((notification) => {
console.log("PermissionsScreen - getInitialNotification - notification:", notification);
})
.catch((err) => console.error("getInitialNotifiation failed", err));
// C. Notification Opened
NotificationsAndroid.setNotificationOpenedListener((notification) => {
console.log("PermissionsScreen - setNotificationOpenedListener - :data", notification.getData());
});
// D.a Notification Received
NotificationsAndroid.setNotificationReceivedListener((notification) => {
console.log("PermissionsScreen - setNotificationReceivedListener - data:", notification.getData());
});
// D.b Notification Received "IN FOREGROUND"
NotificationsAndroid.setNotificationReceivedInForegroundListener((notification) => {
console.log("PermissionsScreen - setNotificationReceivedInForegroundListener (foreground)", notification.getData());
});
} // end of: if()
}; // end of: _requestPermissionNotification()