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

CP-9566 Notifications doesn't make sound when app in background #2126

Merged
merged 8 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 7 additions & 12 deletions packages/core-mobile/app/services/fcm/FCMService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import messaging from '@react-native-firebase/messaging'
import Logger from 'utils/Logger'
import NotificationsService from 'services/notifications/NotificationsService'
import { ChannelId } from 'services/notifications/channels'
import { ACTIONS, PROTOCOLS } from 'contexts/DeeplinkContext/types'
import {
BalanceChangeEvents,
NotificationsBalanceChangeSchema
} from 'services/fcm/types'
import { audioFeedback, Audios } from 'utils/AudioFeedback'

type UnsubscribeFunc = () => void

Expand Down Expand Up @@ -39,21 +37,19 @@ class FCMService {
// skip showing notification if user just spent balance in app
return
}
if (result.data.data.event === BalanceChangeEvents.BALANCES_RECEIVED) {
audioFeedback(Audios.Receive)
}
const data = {
accountAddress: result.data.data.accountAddress,
chainId: result.data.data.chainId,
transactionHash: result.data.data.transactionHash,
url: `${PROTOCOLS.CORE}://${ACTIONS.OpenChainPortfolio}`
}
await NotificationsService.displayNotification({
channelId: ChannelId.BALANCE_CHANGES,
title: result.data.notification.title,
body: result.data.notification.body,
data
})
data,
sound: result.data.notification.sound,
channelId: result.data.notification.android?.channelId
}).catch(Logger.error)
})
}
/**
Expand All @@ -63,9 +59,7 @@ class FCMService {
listenForMessagesBackground = (): void => {
messaging().setBackgroundMessageHandler(async remoteMessage => {
Logger.info('A new FCM message arrived in background', remoteMessage)
const result = NotificationsBalanceChangeSchema.safeParse(
remoteMessage.data
)
const result = NotificationsBalanceChangeSchema.safeParse(remoteMessage)
atn4z7 marked this conversation as resolved.
Show resolved Hide resolved
if (!result.success) {
Logger.error(
`[FCMService.ts][listenForMessagesBackground:NotificationsBalanceChangeSchema]${result}`
Expand All @@ -80,9 +74,10 @@ class FCMService {
url: `${PROTOCOLS.CORE}://${ACTIONS.OpenChainPortfolio}`
}
await NotificationsService.displayNotification({
channelId: ChannelId.BALANCE_CHANGES,
channelId: result.data.notification.android?.channelId,
title: result.data.notification.title,
body: result.data.notification.body,
sound: result.data.notification.sound,
data
})
})
Expand Down
9 changes: 7 additions & 2 deletions packages/core-mobile/app/services/fcm/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { object, string } from 'zod'
import { object, string, nativeEnum } from 'zod'
import { ChannelId } from 'services/notifications/channels'

export const NotificationsBalanceChangeSchema = object({
data: object({
Expand All @@ -9,7 +10,11 @@ export const NotificationsBalanceChangeSchema = object({
}),
notification: object({
title: string(),
body: string()
body: string(),
sound: string().optional(),
android: object({
channelId: nativeEnum(ChannelId).optional()
}).optional()
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import notifee, {
Event,
EventDetail,
EventType,
Notification,
TimestampTrigger,
TriggerNotification,
TriggerType
Expand Down Expand Up @@ -291,31 +292,46 @@ class NotificationsService {
return notifee.createChannel(channel)
}

/**
* @param channelId For Android only
* @param title
* @param body
* @param sound For iOS only
* @param data
*/
displayNotification = async ({
channelId,
title,
body,
sound,
data
}: {
channelId: ChannelId
channelId?: ChannelId
title: string
body?: string
sound?: string
data?: NotificationData
}): Promise<void> => {
await notifee.displayNotification({
const notification: Notification = {
title,
body,
android: {
smallIcon: 'notification_icon',
channelId,
// pressAction is needed if you want the notification to open the app when pressed
pressAction: {
id: PressActionId.OPEN_PORTFOLIO,
launchActivity: LAUNCH_ACTIVITY
}
},
data
})
}
if (sound) {
notification.ios = { sound: sound }
}
atn4z7 marked this conversation as resolved.
Show resolved Hide resolved
if (channelId && notification.android) {
notification.android.channelId = channelId
}
await notifee.displayNotification(notification).catch(Logger.error)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const notificationChannels = [
vibration: false,
importance: AndroidImportance.DEFAULT,
title: 'Balance',
subtitle: 'Notifications when your balance changes'
subtitle: 'Notifications when your balance changes',
sound: 'core_receive'
atn4z7 marked this conversation as resolved.
Show resolved Hide resolved
} as AvaxAndroidChannel
]
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export const addNotificationsListeners = (
startListening({
actionCreator: turnOnNotificationsFor,
effect: async (action: AnyAction, listenerApi) => {
await handleTurnOnNotificationsFor(listenerApi, action.payload.channelId)
await handleTurnOnNotificationsFor(
listenerApi,
action.payload.channelId
).catch(Logger.error)
}
})

Expand Down
28 changes: 22 additions & 6 deletions packages/core-mobile/ios/AvaxWallet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
0B66CE2B2C91C7D70059E353 /* core_receive.wav in Resources */ = {isa = PBXBuildFile; fileRef = 0B66CE2A2C91C7D70059E353 /* core_receive.wav */; };
0B66CE2C2C91C7D70059E353 /* core_receive.wav in Resources */ = {isa = PBXBuildFile; fileRef = 0B66CE2A2C91C7D70059E353 /* core_receive.wav */; };
0B36FA522CD3DD170030C8B9 /* core_receive.wav in Resources */ = {isa = PBXBuildFile; fileRef = 0B36FA4F2CD3DD170030C8B9 /* core_receive.wav */; };
0B36FA532CD3DD170030C8B9 /* core_receive.wav in Resources */ = {isa = PBXBuildFile; fileRef = 0B36FA4F2CD3DD170030C8B9 /* core_receive.wav */; };
0B73DE0B2C80D72900DED1DF /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0B73DE0A2C80D72900DED1DF /* GoogleService-Info.plist */; };
0B73DE0C2C80D72900DED1DF /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0B73DE0A2C80D72900DED1DF /* GoogleService-Info.plist */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
Expand Down Expand Up @@ -124,7 +124,7 @@
/* Begin PBXFileReference section */
00E356F11AD99517003FC87E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
00E356F21AD99517003FC87E /* AvaxWalletTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AvaxWalletTests.m; sourceTree = "<group>"; };
0B66CE2A2C91C7D70059E353 /* core_receive.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = core_receive.wav; sourceTree = "<group>"; };
0B36FA4F2CD3DD170030C8B9 /* core_receive.wav */ = {isa = PBXFileReference; lastKnownFileType = audio.wav; path = core_receive.wav; sourceTree = "<group>"; };
0B73DE0A2C80D72900DED1DF /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
0BA5138A27D234C200A25188 /* AvaxWallet-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AvaxWallet-Bridging-Header.h"; sourceTree = "<group>"; };
13B07F961A680F5B00A75B9A /* AvaxWallet.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AvaxWallet.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -217,6 +217,22 @@
name = "Supporting Files";
sourceTree = "<group>";
};
0B36FA502CD3DD170030C8B9 /* Sounds */ = {
isa = PBXGroup;
children = (
0B36FA4F2CD3DD170030C8B9 /* core_receive.wav */,
);
path = Sounds;
sourceTree = "<group>";
};
0B36FA512CD3DD170030C8B9 /* Library */ = {
isa = PBXGroup;
children = (
0B36FA502CD3DD170030C8B9 /* Sounds */,
);
path = Library;
sourceTree = "<group>";
};
13B07FAE1A68108700A75B9A /* AvaxWallet */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -284,7 +300,7 @@
83CBB9F61A601CBA00E9B192 = {
isa = PBXGroup;
children = (
0B66CE2A2C91C7D70059E353 /* core_receive.wav */,
0B36FA512CD3DD170030C8B9 /* Library */,
0B73DE0A2C80D72900DED1DF /* GoogleService-Info.plist */,
501C50822C35C63800DAFF77 /* core_send.wav */,
13B07FAE1A68108700A75B9A /* AvaxWallet */,
Expand Down Expand Up @@ -502,7 +518,7 @@
8E80035F2CAAF3E400FFC562 /* Inter-Regular.ttf in Resources */,
8E8003602CAAF3E400FFC562 /* Inter-SemiBold.ttf in Resources */,
501C50832C35C63800DAFF77 /* core_send.wav in Resources */,
0B66CE2B2C91C7D70059E353 /* core_receive.wav in Resources */,
0B36FA522CD3DD170030C8B9 /* core_receive.wav in Resources */,
81ACC3932BD1B2860089FEBA /* BootSplash.storyboard in Resources */,
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
8EE9988B28B537A40088DE7C /* InfoRelease.plist in Resources */,
Expand Down Expand Up @@ -537,7 +553,7 @@
8E8003662CAAF3F100FFC562 /* Inter-Regular.ttf in Resources */,
8E8003672CAAF3F100FFC562 /* Inter-SemiBold.ttf in Resources */,
5084807F2C3C3E78008762B2 /* core_send.wav in Resources */,
0B66CE2C2C91C7D70059E353 /* core_receive.wav in Resources */,
0B36FA532CD3DD170030C8B9 /* core_receive.wav in Resources */,
81ACC3942BD1B2860089FEBA /* BootSplash.storyboard in Resources */,
8E858A5E28AFD47300236EF2 /* Images.xcassets in Resources */,
8EE9988C28B537A40088DE7C /* InfoRelease.plist in Resources */,
Expand Down
30 changes: 15 additions & 15 deletions packages/core-mobile/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PODS:
- AppAuth/Core (1.7.5)
- AppAuth/ExternalUserAgent (1.7.5):
- AppAuth/Core
- AppCheckCore (11.1.0):
- AppCheckCore (11.2.0):
atn4z7 marked this conversation as resolved.
Show resolved Hide resolved
- GoogleUtilities/Environment (~> 8.0)
- GoogleUtilities/UserDefaults (~> 8.0)
- PromisesObjC (~> 2.4)
Expand Down Expand Up @@ -87,16 +87,16 @@ PODS:
- FirebaseCore (~> 11.0)
- GoogleUtilities/Environment (~> 8.0)
- GoogleUtilities/UserDefaults (~> 8.0)
- FirebaseAppCheckInterop (11.3.0)
- FirebaseAppCheckInterop (11.4.0)
- FirebaseCore (11.2.0):
- FirebaseCoreInternal (~> 11.0)
- GoogleUtilities/Environment (~> 8.0)
- GoogleUtilities/Logger (~> 8.0)
- FirebaseCoreExtension (11.3.0):
- FirebaseCoreExtension (11.4.1):
- FirebaseCore (~> 11.0)
- FirebaseCoreInternal (11.3.0):
- FirebaseCoreInternal (11.4.2):
- "GoogleUtilities/NSData+zlib (~> 8.0)"
- FirebaseInstallations (11.3.0):
- FirebaseInstallations (11.4.0):
- FirebaseCore (~> 11.0)
- GoogleUtilities/Environment (~> 8.0)
- GoogleUtilities/UserDefaults (~> 8.0)
Expand Down Expand Up @@ -167,9 +167,9 @@ PODS:
- lottie-ios (= 4.5.0)
- RCT-Folly (= 2022.05.16.00)
- React-Core
- MMKV (1.3.9):
- MMKVCore (~> 1.3.9)
- MMKVCore (1.3.9)
- MMKV (2.0.0):
- MMKVCore (~> 2.0.0)
- MMKVCore (2.0.0)
- nanopb (3.30910.0):
- nanopb/decode (= 3.30910.0)
- nanopb/encode (= 3.30910.0)
Expand Down Expand Up @@ -1719,7 +1719,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
AppAuth: 501c04eda8a8d11f179dbe8637b7a91bb7e5d2fa
AppCheckCore: 85a8346f8b5d2f50ee1b9f55d8bcaaeafe904adb
AppCheckCore: cc8fd0a3a230ddd401f326489c99990b013f0c4f
boost: d3f49c53809116a5d38da093a8aa78bf551aed09
CatCrypto: a477899b6be4954e75be4897e732da098cc0a5a8
DatadogCore: a3429f62b7da0e715e179833b3daa0203ba2a5ad
Expand All @@ -1745,11 +1745,11 @@ SPEC CHECKSUMS:
FBReactNativeSpec: 40b791f4a1df779e7e4aa12c000319f4f216d40a
Firebase: 98e6bf5278170668a7983e12971a66b2cd57fc8c
FirebaseAppCheck: a6a1c1ca169d795212b9e70b5cfb880083a28e7c
FirebaseAppCheckInterop: 7789a8adfb09e905ce02a76540b94b059029ea81
FirebaseAppCheckInterop: 1b9643ae2f1ee214488caa2f8e32b7bc2f0f3735
FirebaseCore: a282032ae9295c795714ded2ec9c522fc237f8da
FirebaseCoreExtension: 30bb063476ef66cd46925243d64ad8b2c8ac3264
FirebaseCoreInternal: ac26d09a70c730e497936430af4e60fb0c68ec4e
FirebaseInstallations: 58cf94dabf1e2bb2fa87725a9be5c2249171cda0
FirebaseCoreExtension: f1bc67a4702931a7caa097d8e4ac0a1b0d16720e
FirebaseCoreInternal: 35731192cab10797b88411be84940d2beb33a238
FirebaseInstallations: 6ef4a1c7eb2a61ee1f74727d7f6ce2e72acf1414
FirebaseMessaging: c9ec7b90c399c7a6100297e9d16f8a27fc7f7152
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
Expand All @@ -1762,8 +1762,8 @@ SPEC CHECKSUMS:
libwebp: 1786c9f4ff8a279e4dac1e8f385004d5fc253009
lottie-ios: a881093fab623c467d3bce374367755c272bdd59
lottie-react-native: 0b16315d0369b5f78ded562fc3c7efd4298cd9e5
MMKV: 817ba1eea17421547e01e087285606eb270a8dcb
MMKVCore: af055b00e27d88cd92fad301c5fecd1ff9b26dd9
MMKV: f7d1d5945c8765f97f39c3d121f353d46735d801
MMKVCore: c04b296010fcb1d1638f2c69405096aac12f6390
nanopb: fad817b59e0457d11a5dfbde799381cd727c1275
OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c
OpenTelemetrySwiftApi: 657da8071c2908caecce11548e006f779924ff9c
Expand Down
Loading