Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to show notification when the background task is complete

$
0
0

I am trying to create an app that will scan for a specific wifi SSID in the background if the SSID is present it will show a notification even if the app is in the background.I am currently using https://www.npmjs.com/package/react-native-background-timer to run the background task and https://notifee.app/ to show the notification. The app should work without any internet connection.

const backgroundTask = async () => {    try {        let wifiList = await WifiManager.loadWifiList()        const current = await WifiManager.getCurrentWifiSSID()        console.log('current wifi id', current)        const ssid = await AsyncStorage.getItem('currentSSID')        console.log('ssid', ssid)        const index = wifiList.findIndex(item => item.SSID === ssid)        console.log(index)        if (index < 0) {            await handleNotify()        }    } catch (error) {        console.log('error', error)    }    //console.log(wifiList)}const handleNotify = async () => {    try {        // Request permissions (required for iOS)        await notifee.requestPermission()        // Create a channel (required for Android)        const channelId = await notifee.createChannel({            id: 'default',            name: 'Default Channel',        });        // Display a notification        await notifee.displayNotification({            title: 'Notification Title',            body: 'Main body content of the notification',            android: {                channelId,                sound: 'default',                smallIcon: 'ic_launcher', // optional, defaults to 'ic_launcher'.                // pressAction is needed if you want the notification to open the app when pressed                pressAction: {                    id: 'default',                },            },        });    } catch (error) {        console.log('error', error)    }}

In App.ts

/** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow strict-local */import React from "react";import AppContainer from "./app/container";import { Provider } from "react-redux";import store  from "./app/redux/store";import BackgroundTimer from 'react-native-background-timer';import { backgroundTask } from "./app/function/wifi";BackgroundTimer.start()BackgroundTimer.setInterval(backgroundTask,3000)const App = () => {  return (<Provider store={store}><AppContainer /></Provider>  );};export default App;

NB: The code work when the app is open


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>