-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c982b5e
commit c76f519
Showing
7 changed files
with
145 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import Constants from "expo-constants"; | ||
import * as Device from "expo-device"; | ||
import * as ExpoNotifications from "expo-notifications"; | ||
import { Platform } from "react-native"; | ||
import Toast from "react-native-toast-message"; | ||
import { errorToast } from "~/lib/errorToast"; | ||
import { registerWalletNotifications } from "~/lib/notifications"; | ||
import { useAppStore } from "~/lib/state/appStore"; | ||
|
||
export async function registerForPushNotificationsAsync(): Promise< | ||
boolean | null | ||
> { | ||
if (Platform.OS === "android") { | ||
ExpoNotifications.setNotificationChannelAsync("default", { | ||
name: "default", | ||
importance: ExpoNotifications.AndroidImportance.MAX, | ||
vibrationPattern: [0, 250, 250, 250], | ||
lightColor: "#FF231F7C", | ||
enableLights: true, | ||
enableVibrate: true, | ||
}); | ||
} | ||
|
||
if (!Device.isDevice) { | ||
errorToast("Must use physical device for push notifications"); | ||
return false; | ||
} | ||
|
||
const { status: existingStatus } = | ||
await ExpoNotifications.getPermissionsAsync(); | ||
let finalStatus = existingStatus; | ||
if (existingStatus !== "granted") { | ||
const { status } = await ExpoNotifications.requestPermissionsAsync(); | ||
finalStatus = status; | ||
} | ||
if (finalStatus === "undetermined") { | ||
return null; | ||
} | ||
if (finalStatus === "denied") { | ||
if (existingStatus === "denied") { | ||
errorToast(new Error("Enable app notifications in device settings")); | ||
} | ||
return false; | ||
} | ||
const projectId = | ||
Constants?.expoConfig?.extra?.eas?.projectId ?? | ||
Constants?.easConfig?.projectId; | ||
if (!projectId) { | ||
errorToast(new Error("Project ID not found")); | ||
} | ||
try { | ||
const pushToken = ( | ||
await ExpoNotifications.getExpoPushTokenAsync({ | ||
projectId, | ||
}) | ||
).data; | ||
|
||
useAppStore.getState().setExpoPushToken(pushToken); | ||
|
||
const wallets = useAppStore.getState().wallets; | ||
|
||
for (let i = 0; i < wallets.length; i++) { | ||
const wallet = wallets[i]; | ||
if (!(wallet.nwcCapabilities || []).includes("notifications")) { | ||
Toast.show({ | ||
type: "info", | ||
text1: `${wallet.name} does not have notifications capability`, | ||
}); | ||
continue; | ||
} | ||
await registerWalletNotifications( | ||
wallet.nostrWalletConnectUrl ?? "", | ||
i, | ||
wallet.name, | ||
); | ||
} | ||
|
||
return true; | ||
} catch (error) { | ||
errorToast(error); | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,19 @@ | ||
import Constants from "expo-constants"; | ||
import * as Device from "expo-device"; | ||
import * as ExpoNotifications from "expo-notifications"; | ||
import { Platform } from "react-native"; | ||
import Toast from "react-native-toast-message"; | ||
import { IS_EXPO_GO } from "~/lib/constants"; | ||
import { errorToast } from "~/lib/errorToast"; | ||
import { registerWalletNotifications } from "~/lib/notifications"; | ||
import { useAppStore } from "~/lib/state/appStore"; | ||
|
||
export async function registerForPushNotificationsAsync(): Promise< | ||
boolean | null | ||
> { | ||
if (Platform.OS === "android") { | ||
ExpoNotifications.setNotificationChannelAsync("default", { | ||
name: "default", | ||
importance: ExpoNotifications.AndroidImportance.MAX, | ||
vibrationPattern: [0, 250, 250, 250], | ||
lightColor: "#FF231F7C", | ||
enableLights: true, | ||
enableVibrate: true, | ||
}); | ||
} | ||
|
||
if (!Device.isDevice) { | ||
errorToast("Must use physical device for push notifications"); | ||
return false; | ||
} | ||
|
||
const { status: existingStatus } = | ||
await ExpoNotifications.getPermissionsAsync(); | ||
let finalStatus = existingStatus; | ||
if (existingStatus !== "granted") { | ||
const { status } = await ExpoNotifications.requestPermissionsAsync(); | ||
finalStatus = status; | ||
} | ||
if (finalStatus === "undetermined") { | ||
if (IS_EXPO_GO) { | ||
errorToast(new Error("Push notifications are disabled in Expo Go")); | ||
return null; | ||
} | ||
if (finalStatus === "denied") { | ||
if (existingStatus === "denied") { | ||
errorToast(new Error("Enable app notifications in device settings")); | ||
} | ||
return false; | ||
} | ||
const projectId = | ||
Constants?.expoConfig?.extra?.eas?.projectId ?? | ||
Constants?.easConfig?.projectId; | ||
if (!projectId) { | ||
errorToast(new Error("Project ID not found")); | ||
} | ||
try { | ||
const pushToken = ( | ||
await ExpoNotifications.getExpoPushTokenAsync({ | ||
projectId, | ||
}) | ||
).data; | ||
|
||
useAppStore.getState().setExpoPushToken(pushToken); | ||
|
||
const wallets = useAppStore.getState().wallets; | ||
|
||
for (let i = 0; i < wallets.length; i++) { | ||
const wallet = wallets[i]; | ||
if (!(wallet.nwcCapabilities || []).includes("notifications")) { | ||
Toast.show({ | ||
type: "info", | ||
text1: `${wallet.name} does not have notifications capability`, | ||
}); | ||
continue; | ||
} | ||
await registerWalletNotifications( | ||
wallet.nostrWalletConnectUrl ?? "", | ||
i, | ||
wallet.name, | ||
); | ||
} | ||
|
||
return true; | ||
try { | ||
const nativePushNotifications = require("~/native/notifications/service"); | ||
return await nativePushNotifications.registerForPushNotificationsAsync(); | ||
} catch (error) { | ||
errorToast(error); | ||
console.error("Error importing push notifications logic:", error); | ||
return null; | ||
} | ||
|
||
return false; | ||
} |