|
@@ -7,6 +7,10 @@ import { NAVIGATION_PAGES } from 'src/types';
|
|
import { usePostSetSettingsMutation } from '@api/notifications';
|
|
import { usePostSetSettingsMutation } from '@api/notifications';
|
|
import { useMessagesStore } from 'src/stores/unreadMessagesStore';
|
|
import { useMessagesStore } from 'src/stores/unreadMessagesStore';
|
|
import { useFriendsNotificationsStore } from 'src/stores/friendsNotificationsStore';
|
|
import { useFriendsNotificationsStore } from 'src/stores/friendsNotificationsStore';
|
|
|
|
+import {
|
|
|
|
+ registerBackgroundNotificationTask,
|
|
|
|
+ unregisterBackgroundNotificationTask
|
|
|
|
+} from 'src/utils/pushNotificationTask';
|
|
|
|
|
|
const PushNotificationContext = createContext<{
|
|
const PushNotificationContext = createContext<{
|
|
isSubscribed: boolean;
|
|
isSubscribed: boolean;
|
|
@@ -124,8 +128,24 @@ export const PushNotificationProvider = ({ children }: { children: React.ReactNo
|
|
}
|
|
}
|
|
}, [lastNotificationResponse]);
|
|
}, [lastNotificationResponse]);
|
|
|
|
|
|
|
|
+ const checkNotificationPermissions = async () => {
|
|
|
|
+ const { status } = await Notifications.getPermissionsAsync();
|
|
|
|
+ return status;
|
|
|
|
+ };
|
|
|
|
+
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
|
+ const getPermissionsStatus = async () => {
|
|
|
|
+ const status = await checkNotificationPermissions();
|
|
|
|
+ if (status !== 'granted' && isSubscribed) {
|
|
|
|
+ await unsubscribeFromNotifications();
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
if (isSubscribed) {
|
|
if (isSubscribed) {
|
|
|
|
+ getPermissionsStatus();
|
|
|
|
+
|
|
Notifications.setNotificationHandler({
|
|
Notifications.setNotificationHandler({
|
|
handleNotification: async () => ({
|
|
handleNotification: async () => ({
|
|
shouldShowAlert: true,
|
|
shouldShowAlert: true,
|
|
@@ -134,14 +154,14 @@ export const PushNotificationProvider = ({ children }: { children: React.ReactNo
|
|
})
|
|
})
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+ registerBackgroundNotificationTask();
|
|
|
|
+
|
|
const notificationListener = Notifications.addNotificationReceivedListener((notification) => {
|
|
const notificationListener = Notifications.addNotificationReceivedListener((notification) => {
|
|
updateNotificationStatus();
|
|
updateNotificationStatus();
|
|
updateUnreadMessagesCount();
|
|
updateUnreadMessagesCount();
|
|
});
|
|
});
|
|
|
|
|
|
const responseListener = Notifications.addNotificationResponseReceivedListener((response) => {
|
|
const responseListener = Notifications.addNotificationResponseReceivedListener((response) => {
|
|
- console.log('Notification response received');
|
|
|
|
-
|
|
|
|
let screenName;
|
|
let screenName;
|
|
let url;
|
|
let url;
|
|
let parentScreen;
|
|
let parentScreen;
|
|
@@ -232,6 +252,8 @@ export const PushNotificationProvider = ({ children }: { children: React.ReactNo
|
|
notificationListener.remove();
|
|
notificationListener.remove();
|
|
responseListener.remove();
|
|
responseListener.remove();
|
|
};
|
|
};
|
|
|
|
+ } else {
|
|
|
|
+ unregisterBackgroundNotificationTask();
|
|
}
|
|
}
|
|
}, [isSubscribed]);
|
|
}, [isSubscribed]);
|
|
|
|
|
|
@@ -253,6 +275,7 @@ export const PushNotificationProvider = ({ children }: { children: React.ReactNo
|
|
storage.remove('deviceToken');
|
|
storage.remove('deviceToken');
|
|
storage.set('subscribed', false);
|
|
storage.set('subscribed', false);
|
|
setIsSubscribed(false);
|
|
setIsSubscribed(false);
|
|
|
|
+ unregisterBackgroundNotificationTask();
|
|
};
|
|
};
|
|
|
|
|
|
const toggleSubscription = async () => {
|
|
const toggleSubscription = async () => {
|