Viktoriia 9 mesiacov pred
rodič
commit
454b38d765
3 zmenil súbory, kde vykonal 69 pridanie a 7 odobranie
  1. 2 1
      .gitignore
  2. 49 1
      App.tsx
  3. 18 5
      src/components/MenuDrawer/index.tsx

+ 2 - 1
.gitignore

@@ -38,4 +38,5 @@ package-lock.json
 .vscode
 temp
 android
-ios
+ios
+google-services.json

+ 49 - 1
App.tsx

@@ -15,6 +15,8 @@ import { setupInterceptors } from 'src/utils/request';
 import { ErrorModal } from 'src/components';
 import { NotificationProvider } from 'src/contexts/NotificationContext';
 import React from 'react';
+import * as Notifications from 'expo-notifications';
+import { Platform } from 'react-native';
 
 const routingInstrumentation = new Sentry.ReactNavigationInstrumentation({
   enableTimeToInitialDisplay: true
@@ -24,10 +26,56 @@ Sentry.init({
   dsn: 'https://c9b37005f4be22a17a582603ebc17598@o4507781200543744.ingest.de.sentry.io/4507781253824592',
   integrations: [new Sentry.ReactNativeTracing({ routingInstrumentation })],
   debug: false,
-  ignoreErrors: ['Network Error', 'ECONNABORTED', 'timeout of 10000ms exceeded'],
+  ignoreErrors: ['Network Error', 'ECONNABORTED', 'timeout of 10000ms exceeded']
 });
 
 const App = () => {
+  async function setupNotificationChannels() {
+    if (Platform.OS === 'android') {
+      await Notifications.setNotificationChannelAsync('default', {
+        name: 'default',
+        importance: Notifications.AndroidImportance.MAX,
+        vibrationPattern: [0, 250, 250, 250],
+        lightColor: '#FF231F7C'
+      });
+    }
+  }
+
+  useEffect(() => {
+    setupNotificationChannels();
+  }, []);
+
+  useEffect(() => {
+    let notificationListener: any;
+    let responseListener: any;
+
+    const checkLastNotificationResponse = async () => {
+      const lastNotificationResponse = await Notifications.getLastNotificationResponseAsync();
+      if (lastNotificationResponse) {
+        const data = lastNotificationResponse.notification.request.content.data;
+        console.log('lastNotificationResponse', lastNotificationResponse.notification.request);
+        console.log('dataLast', data);
+      }
+    };
+
+    checkLastNotificationResponse();
+
+    notificationListener = Notifications.addNotificationReceivedListener((notification) => {
+      console.log('notification', notification.request);
+    });
+
+    responseListener = Notifications.addNotificationResponseReceivedListener((response) => {
+      const data = response.notification.request.content.data;
+      console.log('payload', response.notification.request.trigger?.payload);
+      console.log('data', data);
+    });
+
+    return () => {
+      if (notificationListener) notificationListener.remove();
+      if (responseListener) responseListener.remove();
+    };
+  }, []);
+
   return (
     <QueryClientProvider client={queryClient}>
       <NotificationProvider>

+ 18 - 5
src/components/MenuDrawer/index.tsx

@@ -1,4 +1,4 @@
-import React, { useState } from 'react';
+import React, { useEffect, useState } from 'react';
 import { View, Image, Linking, Text, Switch, Platform } from 'react-native';
 import { CommonActions, useNavigation } from '@react-navigation/native';
 import * as Notifications from 'expo-notifications';
@@ -81,17 +81,30 @@ export const MenuDrawer = (props: any) => {
         platform: deviceData.platform,
         n_token: deviceData.notificationToken
       });
+    }
+  };
+
+  useEffect(() => {
+    let notificationListener: any;
+    let responseListener: any;
 
-      Notifications.addNotificationReceivedListener((notification) => {
-        console.log('notification', notification);
+    if (isSubscribed) {
+      notificationListener = Notifications.addNotificationReceivedListener((notification) => {
+        console.log('notification', notification.request);
       });
 
-      Notifications.addNotificationResponseReceivedListener((response) => {
+      responseListener = Notifications.addNotificationResponseReceivedListener((response) => {
         const data = response.notification.request.content.data;
+        console.log('payload', response.notification.request.trigger?.payload);
         console.log('data', data);
       });
     }
-  };
+
+    return () => {
+      if (notificationListener) Notifications.removeNotificationSubscription(notificationListener);
+      if (responseListener) Notifications.removeNotificationSubscription(responseListener);
+    };
+  }, [isSubscribed]);
 
   const toggleSwitch = async () => {
     if (isSubscribed) {