Skip to content

Commit

Permalink
move code to notifications manager
Browse files Browse the repository at this point in the history
Signed-off-by: Nitesh Balusu <84944042+niteshbalusu11@users.noreply.github.com>
  • Loading branch information
niteshbalusu11 committed Dec 1, 2023
1 parent 9fcc4dd commit f9facbe
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 45 deletions.
18 changes: 0 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,10 @@ import store from "./state/store";
import { clearApp } from "./storage/app";
import { PLATFORM } from "./utils/constants";
import "./i18n/i18n";
import {
getFcmToken,
notificationListener,
requestUserPermission,
} from "./utils/push-notification";

export default function App() {
const [debug, setDebug] = useState(__DEV__ ? true : false);

useEffect(() => {
const fetchToken = async () => {
const token = await getFcmToken();
if (token) {
console.log("Your Firebase Token is:", token);
}
};

fetchToken();
requestUserPermission();
notificationListener();
}, []);

useEffect(() => {
(async () => {
if (PLATFORM === "web") {
Expand Down
40 changes: 24 additions & 16 deletions src/state/NotificationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import PushNotification, { PushNotificationObject } from "react-native-push-noti

import { navigate } from "../utils/navigation";
import { IStoreModel } from "./index";
import { ANDROID_PUSH_NOTIFICATION_PUSH_CHANNEL_ID, ANDROID_PUSH_NOTIFICATION_PUSH_CHANNEL_NAME, PLATFORM } from "../utils/constants";
import { localNotification } from "../utils/push-notification";
import {
ANDROID_PUSH_NOTIFICATION_PUSH_CHANNEL_ID,
ANDROID_PUSH_NOTIFICATION_PUSH_CHANNEL_NAME,
PLATFORM,
} from "../utils/constants";
import { getFcmToken, localNotification, notificationListener } from "../utils/push-notification";
import { toast } from "../utils";

import logger from "./../utils/log";
Expand All @@ -19,8 +23,8 @@ interface ILocalNotificationPayload {
export interface INotificationManagerModel {
initialize: Thunk<INotificationManagerModel>;

localNotification: Thunk<INotificationManagerModel, ILocalNotificationPayload, any, IStoreModel>;
};
localNotification: Thunk<INotificationManagerModel, ILocalNotificationPayload, any, IStoreModel>;
}

export const notificationManager: INotificationManagerModel = {
initialize: thunk(async () => {
Expand All @@ -35,46 +39,53 @@ export const notificationManager: INotificationManagerModel = {
if (PLATFORM === "ios") {
const permissions = await PushNotification.requestPermissions(["alert", "sound", "badge"]);

if(!permissions.alert) {
if (!permissions.alert) {
log.w("Didn't get permissions to send push notifications.");
return;
}

await getFcmToken();
notificationListener();
} else if (PLATFORM === "android") {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
);
if (granted === "denied" || granted === "never_ask_again") {
log.w("Post notification permission was denied", [granted]);
} else {
await getFcmToken();
notificationListener();
return;
}
}

PushNotification.configure({
requestPermissions: false,
onNotification: ((notification) => {
onNotification: (notification) => {
log.i("onNotification", [notification]);

// TODO(hsjoberg): ios notification deeplinking
if (PLATFORM === "android") {
if (notification.message.toString().includes("on-chain")) {
log.i("Navigating to OnChainTransactionLog");
navigate("OnChain", { screen: "OnChainTransactionLog" });
}
else if (notification.message.toString().toLocaleLowerCase().includes("payment channel")) {
} else if (
notification.message.toString().toLocaleLowerCase().includes("payment channel")
) {
log.i("Navigating to LightningInfo");
navigate("LightningInfo");
}
}
}),
},
});

if (PLATFORM === "android") {
PushNotification.createChannel({
PushNotification.createChannel(
{
channelId: ANDROID_PUSH_NOTIFICATION_PUSH_CHANNEL_ID,
channelName: ANDROID_PUSH_NOTIFICATION_PUSH_CHANNEL_NAME,
},
() => {}
() => {},
);
}
} catch (error) {
Expand All @@ -90,10 +101,7 @@ export const notificationManager: INotificationManagerModel = {
localNotification: thunk((_, { message, importance }, { getStoreState }) => {
if (getStoreState().settings.pushNotificationsEnabled) {
if (PLATFORM !== "macos") {
localNotification(
message,
importance ?? "default"
);
localNotification(message, importance ?? "default");
} else {
toast(message);
}
Expand Down
3 changes: 3 additions & 0 deletions src/storage/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export enum StorageItem { // const enums not supported in Babel 7...
lightningBoxServer = "lightningBoxServer",
lightningBoxAddress = "lightningBoxAddress",
lightningBoxLnurlPayDesc = "lightningBoxLnurlPayDesc",
firebaseToken = "firebaseToken",
}

export const setItem = async (key: StorageItem, value: string) =>
Expand Down Expand Up @@ -192,6 +193,7 @@ export const clearApp = async () => {
removeItem(StorageItem.lightningBoxServer),
removeItem(StorageItem.lightningBoxAddress),
removeItem(StorageItem.lightningBoxLnurlPayDesc),
removeItem(StorageItem.firebaseToken),
]);
};

Expand Down Expand Up @@ -283,5 +285,6 @@ export const setupApp = async () => {
setItem(StorageItem.lightningBoxServer, DEFAULT_LIGHTNINGBOX_SERVER),
// setItem(StorageItem.lightningBoxAddress, ""),
setItem(StorageItem.lightningBoxLnurlPayDesc, DEFAULT_LIGHTNINGBOX_LNURLPDESC),
setItem(StorageItem.firebaseToken, ""),
]);
};
12 changes: 1 addition & 11 deletions src/utils/push-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,10 @@ export const notificationListener = () => {
export const getFcmToken = async () => {
try {
const newFcmToken = await firebase.messaging().getToken();
console.log("firebase token: ", newFcmToken);
return newFcmToken;
} catch (error) {
console.error("error fetching firebase token", error);
return null;
}
};

export const requestUserPermission = async () => {
const authStatus = await firebase.messaging().requestPermission();
const enabled =
authStatus === firebase.messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === firebase.messaging.AuthorizationStatus.PROVISIONAL;

if (enabled) {
console.log("Authorization status:", authStatus);
}
};

0 comments on commit f9facbe

Please sign in to comment.