Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for firebase notifications #1390

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move code to notifications manager
Signed-off-by: Nitesh Balusu <[email protected]>
niteshbalusu11 committed Dec 1, 2023
commit f9facbe555ee72e7ccb8f83ec354db9fc6286ea4
18 changes: 0 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -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") {
40 changes: 24 additions & 16 deletions src/state/NotificationManager.ts
Original file line number Diff line number Diff line change
@@ -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";
@@ -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 () => {
@@ -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) {
@@ -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);
}
3 changes: 3 additions & 0 deletions src/storage/app.ts
Original file line number Diff line number Diff line change
@@ -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) =>
@@ -192,6 +193,7 @@ export const clearApp = async () => {
removeItem(StorageItem.lightningBoxServer),
removeItem(StorageItem.lightningBoxAddress),
removeItem(StorageItem.lightningBoxLnurlPayDesc),
removeItem(StorageItem.firebaseToken),
]);
};

@@ -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
@@ -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);
}
};