Skip to content

Commit

Permalink
Initialize push notifications as disabled
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <jamiehalebc@gmail.com>
  • Loading branch information
jamshale committed Sep 7, 2023
1 parent 8fcbd2a commit 1df3c29
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
"react-native-inappbrowser-reborn": "^3.7.0",
"react-native-keychain": "^7.0.0",
"react-native-localize": "^2.2.4",
"react-native-permissions": "3.6.1",
"react-native-permissions": "^3.6.1",
"react-native-qrcode-svg": "6.1.1",
"react-native-reanimated": "2.2.4",
"react-native-safe-area-context": "3.3.2",
Expand Down
6 changes: 3 additions & 3 deletions app/src/components/PushNotifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useAgent } from '@aries-framework/react-hooks'
import React, { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'

import { isMediatorCapable, registered, setup, userDenied } from '../helpers/PushNotificationsHelper'
import { isMediatorCapable, isRegistered, setup, isUserDenied } from '../helpers/PushNotificationsHelper'
import PushNotificationsModal from '../modals/PushNotificationsModal'

const PushNotifications = () => {
Expand All @@ -12,12 +12,12 @@ const PushNotifications = () => {

const setupPushNotifications = async () => {
setInfoModalVisible(false)
if (!agent || (await userDenied())) return
if (!agent || (await isUserDenied())) return
setup(agent)
}

const initializeCapabilityRequest = async () => {
if (!agent || !(await isMediatorCapable(agent)) || (await registered())) return
if (!agent || !(await isMediatorCapable(agent)) || (await isRegistered())) return
setInfoModalVisible(true)
}

Expand Down
11 changes: 6 additions & 5 deletions app/src/helpers/PushNotificationsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const _getMediatorConnection = async (agent: Agent<any>): Promise<ConnectionReco
* Checks wether the user denied permissions on the info modal
* @returns {Promise<boolean>}
*/
const userDenied = async (): Promise<boolean> => {
const isUserDenied = async (): Promise<boolean> => {
return (await AsyncStorage.getItem('userDeniedPushNotifications')) === 'true'
}

Expand Down Expand Up @@ -109,7 +109,7 @@ const isMediatorCapable = async (agent: Agent<any>): Promise<boolean | undefined
* @param token - If defined will use this token instead of fetching with firebase
* @returns {Promise<boolean>}
*/
const registered = async (token?: string): Promise<boolean> => {
const isRegistered = async (token?: string): Promise<boolean> => {
const authorized = (await messaging().hasPermission()) === messaging.AuthorizationStatus.AUTHORIZED

// Need to register for push notification capability on iOS
Expand All @@ -132,7 +132,7 @@ const setDeviceInfo = async (agent: Agent<any>, blankDeviceToken = false): Promi
if (blankDeviceToken) token = ''
else token = await messaging().getToken()

if ((await registered(token)) && !blankDeviceToken) return
if ((await isRegistered(token)) && !blankDeviceToken) return

const mediator = await _getMediatorConnection(agent)
if (!mediator) return
Expand All @@ -155,10 +155,11 @@ const setDeviceInfo = async (agent: Agent<any>, blankDeviceToken = false): Promi
* @returns {Promise<void>}
*/
const setup = async (agent: Agent<any>): Promise<void> => {
setDeviceInfo(agent)
// FIXME: Currently set the token to blank (disabled) on initialization.
setDeviceInfo(agent, true)
_backgroundHandler()
_foregroundHandler()
_requestPermission(agent)
}

export { registered, isMediatorCapable, userDenied, setDeviceInfo, setup }
export { isRegistered, isMediatorCapable, isUserDenied, setDeviceInfo, setup }
15 changes: 5 additions & 10 deletions app/src/screens/Developer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import { Modal, StyleSheet, Switch, Text, Pressable, View, ScrollView } from 're
import { SafeAreaView } from 'react-native-safe-area-context'
import Icon from 'react-native-vector-icons/MaterialIcons'

import {
isMediatorCapable as isMediatorPushNotificationCapable,
registered as pushNotificationsRegistered,
setDeviceInfo as setPushNotificationDeviceInfo,
setup as setupPushNotifications,
} from '../helpers/PushNotificationsHelper'
import * as PushNotificationHelper from '../helpers/PushNotificationsHelper'
import { BCState } from '../store'

import IASEnvironment from './IASEnvironment'
Expand Down Expand Up @@ -178,18 +173,18 @@ const Settings: React.FC = () => {

const getPushNotificationCapable = async () => {
if (!agent) return
if ((await isMediatorPushNotificationCapable(agent)) === true) setPushNotificationCapable(true)
if ((await PushNotificationHelper.isMediatorCapable(agent)) === true) setPushNotificationCapable(true)
else setPushNotificationCapable(false)
}

const initializePushNotificationsToggle = async () => {
setEnablePushNotifications(await pushNotificationsRegistered())
setEnablePushNotifications(await PushNotificationHelper.isRegistered())
}

const toggleDevPushNotificationsSwitch = () => {
if (!pushNotificationCapable || !agent) return
if (enablePushNotifications) setPushNotificationDeviceInfo(agent, true)
else setupPushNotifications(agent)
if (enablePushNotifications) PushNotificationHelper.setDeviceInfo(agent, true)
else PushNotificationHelper.setup(agent)
setEnablePushNotifications(!enablePushNotifications)
}

Expand Down
16 changes: 1 addition & 15 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7461,7 +7461,7 @@ __metadata:
react-native-inappbrowser-reborn: ^3.7.0
react-native-keychain: ^7.0.0
react-native-localize: ^2.2.4
react-native-permissions: 3.6.1
react-native-permissions: ^3.6.1
react-native-qrcode-svg: 6.1.1
react-native-reanimated: 2.2.4
react-native-safe-area-context: 3.3.2
Expand Down Expand Up @@ -18719,20 +18719,6 @@ __metadata:
languageName: node
linkType: hard

"react-native-permissions@npm:3.6.1":
version: 3.6.1
resolution: "react-native-permissions@npm:3.6.1"
peerDependencies:
react: ">=16.13.1"
react-native: ">=0.63.3"
react-native-windows: ">=0.62.0"
peerDependenciesMeta:
react-native-windows:
optional: true
checksum: 41920565e1a850c5bdcda94ee2415be0277ab64ea86ea4daf9f0b90eb7857e3a7a336d159495785d14d3de89dd09bb198f4d1afef6811773b5c2b0148f99d583
languageName: node
linkType: hard

"react-native-permissions@npm:^3.6.1":
version: 3.8.0
resolution: "react-native-permissions@npm:3.8.0"
Expand Down

0 comments on commit 1df3c29

Please sign in to comment.