-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: notification improvements #188
Open
im-adithya
wants to merge
14
commits into
feat/push-notifications
Choose a base branch
from
task-notifications
base: feat/push-notifications
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a572e10
chore: improve notifications support
im-adithya 184548a
feat: add notification service extension
im-adithya f4bf55d
feat: add messaging service for android notifications
im-adithya ba47b38
chore: changes
im-adithya 814959e
chore: add message service config plugin
im-adithya 979dfb2
chore: use our own modified package
im-adithya 4957141
chore: import shared preferences only in android
im-adithya 9b4a596
chore: add google services json to env
im-adithya 6a5ccc4
chore: further changes
im-adithya c84bb84
fix: handle linking hook
im-adithya 572c59a
chore: more changes
im-adithya 0db8b05
chore: add notification handling
im-adithya 5080ea3
chore: use transaction in deep link
im-adithya b9103c4
chore: add notification icon in android
im-adithya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,6 @@ yarn-error.* | |
|
||
# typescript | ||
*.tsbuildinfo | ||
|
||
ios | ||
android |
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 was deleted.
Oops, something went wrong.
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,5 @@ | ||
import { Notifications } from "../../../pages/settings/Notifications"; | ||
|
||
export default function Page() { | ||
return <Notifications />; | ||
} |
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,117 @@ | ||
#import "NotificationService.h" | ||
#import <CommonCrypto/CommonCryptor.h> | ||
|
||
@interface NotificationService () | ||
|
||
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver); | ||
@property (nonatomic, strong) UNNotificationRequest *receivedRequest; | ||
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; | ||
|
||
@end | ||
|
||
@implementation NotificationService | ||
|
||
// Helper function to convert hex string to NSData | ||
NSData* dataFromHexString(NSString *hexString) { | ||
NSMutableData *data = [NSMutableData data]; | ||
int idx; | ||
for (idx = 0; idx+2 <= hexString.length; idx+=2) { | ||
NSRange range = NSMakeRange(idx, 2); | ||
NSString *hexByte = [hexString substringWithRange:range]; | ||
unsigned int byte; | ||
if ([[NSScanner scannerWithString:hexByte] scanHexInt:&byte]) { | ||
[data appendBytes:&byte length:1]; | ||
} else { | ||
return nil; // invalid hex string | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { | ||
self.receivedRequest = request; | ||
self.contentHandler = contentHandler; | ||
self.bestAttemptContent = [request.content mutableCopy]; | ||
|
||
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.getalby.mobile.nse"]; | ||
NSDictionary *walletsDict = [sharedDefaults objectForKey:@"wallets"]; | ||
|
||
NSString *appPubkey = request.content.userInfo[@"body"][@"appPubkey"]; | ||
if (!appPubkey) { | ||
return; | ||
} | ||
|
||
NSDictionary *walletInfo = walletsDict[appPubkey]; | ||
if (!walletInfo) { | ||
return; | ||
} | ||
|
||
NSString *sharedSecretString = walletInfo[@"sharedSecret"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could the name be more clear, maybe |
||
NSString *walletName = walletInfo[@"name"]; | ||
if (!sharedSecretString) { | ||
return; | ||
} | ||
|
||
NSData *sharedSecretData = dataFromHexString(sharedSecretString); | ||
if (!sharedSecretData || sharedSecretData.length != kCCKeySizeAES256) { | ||
return; | ||
} | ||
|
||
NSString *encryptedContent = request.content.userInfo[@"body"][@"content"]; | ||
NSArray *parts = [encryptedContent componentsSeparatedByString:@"?iv="]; | ||
if (parts.count < 2) { | ||
return; | ||
} | ||
|
||
NSString *ciphertextBase64 = parts[0]; | ||
NSString *ivBase64 = parts[1]; | ||
|
||
NSData *ciphertextData = [[NSData alloc] initWithBase64EncodedString:ciphertextBase64 options:0]; | ||
NSData *ivData = [[NSData alloc] initWithBase64EncodedString:ivBase64 options:0]; | ||
|
||
if (!ciphertextData || !ivData || ivData.length != kCCBlockSizeAES128) { | ||
return; | ||
} | ||
|
||
// Prepare for decryption | ||
size_t decryptedDataLength = ciphertextData.length + kCCBlockSizeAES128; | ||
NSMutableData *plaintextData = [NSMutableData dataWithLength:decryptedDataLength]; | ||
|
||
size_t numBytesDecrypted = 0; | ||
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, sharedSecretData.bytes, sharedSecretData.length, ivData.bytes, ciphertextData.bytes, ciphertextData.length, plaintextData.mutableBytes, decryptedDataLength, &numBytesDecrypted); | ||
|
||
if (cryptStatus == kCCSuccess) { | ||
plaintextData.length = numBytesDecrypted; | ||
|
||
NSError *jsonError = nil; | ||
NSDictionary *parsedContent = [NSJSONSerialization JSONObjectWithData:plaintextData options:0 error:&jsonError]; | ||
|
||
if (!parsedContent || jsonError) { | ||
return; | ||
} | ||
|
||
NSString *notificationType = parsedContent[@"notification_type"]; | ||
if (![notificationType isEqualToString:@"payment_received"]) { | ||
return; | ||
} | ||
|
||
NSDictionary *notificationDict = parsedContent[@"notification"]; | ||
NSNumber *amountNumber = notificationDict[@"amount"]; | ||
if (!amountNumber) { | ||
return; | ||
} | ||
|
||
double amountInSats = [amountNumber doubleValue] / 1000.0; | ||
self.bestAttemptContent.title = walletName; | ||
self.bestAttemptContent.body = [NSString stringWithFormat:@"You just received %.0f sats ⚡️", amountInSats]; | ||
} | ||
|
||
self.contentHandler(self.bestAttemptContent); | ||
} | ||
|
||
- (void)serviceExtensionTimeWillExpire { | ||
self.bestAttemptContent.body = @"expired noitification"; | ||
self.contentHandler(self.bestAttemptContent); | ||
} | ||
|
||
@end |
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,55 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
import * as ExpoNotifications from "expo-notifications"; | ||
import { useAppStore } from "~/lib/state/appStore"; | ||
|
||
ExpoNotifications.setNotificationHandler({ | ||
handleNotification: async () => { | ||
return { | ||
shouldShowAlert: true, | ||
shouldPlaySound: true, | ||
shouldSetBadge: false, | ||
}; | ||
}, | ||
}); | ||
|
||
export const NotificationProvider = ({ children }: any) => { | ||
const notificationListener = useRef<ExpoNotifications.Subscription>(); | ||
const responseListener = useRef<ExpoNotifications.Subscription>(); | ||
const isNotificationsEnabled = useAppStore( | ||
(store) => store.isNotificationsEnabled, | ||
); | ||
|
||
useEffect(() => { | ||
if (!isNotificationsEnabled) { | ||
return; | ||
} | ||
|
||
notificationListener.current = | ||
ExpoNotifications.addNotificationReceivedListener((notification) => { | ||
// triggers when app is foregrounded | ||
console.info("received from server just now"); | ||
}); | ||
|
||
responseListener.current = | ||
ExpoNotifications.addNotificationResponseReceivedListener((response) => { | ||
// triggers when notification is clicked (only when foreground or background) | ||
// see https://docs.expo.dev/versions/latest/sdk/notifications/#notification-events-listeners | ||
// TODO: to also redirect when the app is killed, use useLastNotificationResponse | ||
// TODO: redirect the user to transaction page after switching to the right wallet | ||
}); | ||
|
||
return () => { | ||
notificationListener.current && | ||
ExpoNotifications.removeNotificationSubscription( | ||
notificationListener.current, | ||
); | ||
responseListener.current && | ||
ExpoNotifications.removeNotificationSubscription( | ||
responseListener.current, | ||
); | ||
}; | ||
}, [isNotificationsEnabled]); | ||
|
||
return children; | ||
}; |
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 |
---|---|---|
|
@@ -19,6 +19,10 @@ export const NAV_THEME = { | |
}, | ||
}; | ||
|
||
export const SUITE_NAME = "group.com.getalby.mobile.nse"; | ||
|
||
export const BACKGROUND_NOTIFICATION_TASK = "BACKGROUND-NOTIFICATION-TASK"; | ||
|
||
export const INACTIVITY_THRESHOLD = 5 * 60 * 1000; | ||
|
||
export const CURSOR_COLOR = "hsl(47 100% 72%)"; | ||
|
@@ -29,6 +33,7 @@ export const DEFAULT_CURRENCY = "USD"; | |
export const DEFAULT_WALLET_NAME = "Default Wallet"; | ||
export const ALBY_LIGHTNING_ADDRESS = "[email protected]"; | ||
export const ALBY_URL = "https://getalby.com"; | ||
export const NOSTR_API_URL = "https://api.getalby.com/nwc"; | ||
|
||
export const REQUIRED_CAPABILITIES: Nip47Capability[] = [ | ||
"get_balance", | ||
|
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,8 @@ | ||
import { secp256k1 } from "@noble/curves/secp256k1"; | ||
import { Buffer } from "buffer"; | ||
|
||
export function computeSharedSecret(pub: string, sk: string): string { | ||
const sharedSecret = secp256k1.getSharedSecret(sk, "02" + pub); | ||
const normalizedKey = sharedSecret.slice(1); | ||
return Buffer.from(normalizedKey).toString("hex"); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this only for iOS? should it be in a dedicated directory?