Skip to content

Commit

Permalink
Make notifications work on android
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Nov 20, 2024
1 parent 7879992 commit cef7dcb
Show file tree
Hide file tree
Showing 9 changed files with 740 additions and 19 deletions.
19 changes: 15 additions & 4 deletions apps/wallet-mobile/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id 'com.google.gms.google-services'
}

apply plugin: "com.android.application"
apply plugin: "com.facebook.react"

Expand Down Expand Up @@ -186,11 +190,20 @@ android {

dependencies {

implementation platform('com.google.firebase:firebase-bom:33.6.0')
implementation project(':react-native-notifications')

implementation platform('com.google.firebase:firebase-bom:32.2.0')
implementation "com.google.firebase:firebase-messaging"

// implementation platform('com.google.firebase:firebase-bom:33.6.0')
androidTestImplementation('com.wix:detox:+')
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.firebase:firebase-core:21.1.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'

// implementation("com.google.firebase:firebase-messaging")

// implementation 'com.google.firebase:firebase-core:21.1.1'
// The version of react-native is set by the React Native Gradle Plugin
implementation("com.facebook.react:react-android")

Expand Down Expand Up @@ -219,8 +232,6 @@ dependencies {
exclude group:'com.squareup.okhttp3', module:'okhttp'
}

implementation project(':react-native-notifications')

debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")
if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet-mobile/android/app/google-services.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1028338182097:android:9cb2ca05daa41397bca2f0",
"mobilesdk_app_id": "1:1028338182097:android:19a81ded2005af76bca2f0",
"android_client_info": {
"package_name": "com.emurgo"
"package_name": "com.emurgo.dev"
}
},
"oauth_client": [],
Expand Down
29 changes: 29 additions & 0 deletions apps/wallet-mobile/android/app/src/dev/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"project_info": {
"project_number": "1028338182097",
"project_id": "yoroi-mobile-dev-1",
"storage_bucket": "yoroi-mobile-dev-1.firebasestorage.app"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1028338182097:android:19a81ded2005af76bca2f0",
"android_client_info": {
"package_name": "com.emurgo.dev"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyABpYF2WWAlu66c_1UmyCA9VcBgg8RcLRA"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": []
}
}
}
],
"configuration_version": "1"
}
29 changes: 29 additions & 0 deletions apps/wallet-mobile/android/app/src/main/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"project_info": {
"project_number": "1028338182097",
"project_id": "yoroi-mobile-dev-1",
"storage_bucket": "yoroi-mobile-dev-1.firebasestorage.app"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1028338182097:android:19a81ded2005af76bca2f0",
"android_client_info": {
"package_name": "com.emurgo.dev"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyABpYF2WWAlu66c_1UmyCA9VcBgg8RcLRA"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": []
}
}
}
],
"configuration_version": "1"
}
2 changes: 1 addition & 1 deletion apps/wallet-mobile/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ buildscript {
classpath "org.mozilla.rust-android-gradle:plugin:0.9.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlinVersion"
classpath 'com.google.gms:google-services:4.3.8'
classpath 'com.google.gms:google-services:4.3.15'
}
}

Expand Down
2 changes: 2 additions & 0 deletions apps/wallet-mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
"@react-native-community/clipboard": "^1.5.1",
"@react-native-community/datetimepicker": "^7.1.0",
"@react-native-community/slider": "^4.4.2",
"@react-native-firebase/app": "^21.5.0",
"@react-native-firebase/messaging": "^21.5.0",
"@react-native-masked-view/masked-view": "^0.2.9",
"@react-navigation/bottom-tabs": "^6.4.3",
"@react-navigation/material-top-tabs": "^6.4.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React from 'react'
import {PermissionsAndroid} from 'react-native'
import {Notifications, Registered, RegistrationError} from 'react-native-notifications'

Check failure on line 3 in apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts

View workflow job for this annotation

GitHub Actions / check

'Registered' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 3 in apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts

View workflow job for this annotation

GitHub Actions / check

'RegistrationError' is defined but never used. Allowed unused vars must match /^_/u
import {NotificationBackgroundFetchResult} from 'react-native-notifications'
import messaging from '@react-native-firebase/messaging'

import {notificationManager} from './notification-manager'
import {parseNotificationId} from './notifications'
import {displayNotificationEvent, parseNotificationId, sendNotification} from './notifications'

Check failure on line 8 in apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts

View workflow job for this annotation

GitHub Actions / check

'displayNotificationEvent' is defined but never used. Allowed unused vars must match /^_/u
import {usePrimaryTokenPriceChangedNotification} from './primary-token-price-changed-notification'
import {useRewardsUpdatedNotifications} from './rewards-updated-notification'
import {useTransactionReceivedNotifications} from './transaction-received-notification'
Expand All @@ -15,12 +16,13 @@ const init = () => {
if (initialized) return
initialized = true
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
Notifications.registerRemoteNotifications()
Notifications.events().registerNotificationReceivedForeground((_notification, completion) => {
console.log('Notification received in foreground')
completion({alert: true, sound: true, badge: true})
})

Notifications.events().registerNotificationReceivedBackground((_notification, completion) => {
console.log('Notification received in background')
completion(NotificationBackgroundFetchResult.NEW_DATA)
})

Expand All @@ -47,14 +49,43 @@ export const useInitNotifications = ({enabled}: {enabled: boolean}) => {
}

const usePushNotifications = ({enabled}: {enabled: boolean}) => {
console.log('usePushNotifications', enabled)
React.useEffect(() => {
if (!enabled) return
Notifications.events().registerRemoteNotificationsRegistered((event: Registered) => {
// TODO: Send the token to my server so it could send back push notifications...
console.log('Device Token Received', event.deviceToken)
})
Notifications.events().registerRemoteNotificationsRegistrationFailed((event: RegistrationError) => {
console.error('Failed to register for remote notifications', event)
// Notifications.getInitialNotification().then((notification) => {
// console.log('Initial notification', notification)
// })
//
// Notifications.events().registerRemoteNotificationsRegistrationDenied(() => {
// console.log('NOTIFICATION PUSH: User denied remote notifications')
// })
// Notifications.events().registerRemoteNotificationsRegistered((event: Registered) => {
// // TODO: Send the token to my server so it could send back push notifications...
// console.log('NOTIFICATION PUSH: Device Token Received', event.deviceToken)
// })
// Notifications.events().registerRemoteNotificationsRegistrationFailed((event: RegistrationError) => {
// console.log('NOTIFICATION PUSH: Failed to register for remote notifications', event)
// })
Notifications.registerRemoteNotifications({})
const s1 = messaging().onMessage(async (remoteMessage) => {

Check failure on line 70 in apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts

View workflow job for this annotation

GitHub Actions / check

Async arrow function has no 'await' expression
console.log('A new FCM message arrived!', JSON.stringify(remoteMessage))
})

messaging()
.getToken()
.then((token) => {
console.log('FCM Token:', token)
})

return () => {
s1()
}
}, [enabled])
}

messaging().setBackgroundMessageHandler(async function (remoteMessage) {

Check failure on line 86 in apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts

View workflow job for this annotation

GitHub Actions / check

Async function has no 'await' expression
// self.registration.showNotification('Title', {body: 'Body', icon: '/icon.png'})
console.log('Message handled in the background!', JSON.stringify(remoteMessage), (self as any).registration)

Check warning on line 88 in apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts

View workflow job for this annotation

GitHub Actions / check

Unexpected any. Specify a different type
sendNotification({title: 'tiiiitle', id: 123, body: 'boooody'})
// self.registration.showNotification('Title', {body: 'Body', icon: '/icon.png'});
})
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const displayNotificationEvent = async (notificationEvent: NotificationTy
}
}

const sendNotification = (options: {title: string; body: string; id: number}) => {
export const sendNotification = (options: {title: string; body: string; id: number}) => {
const notification = new Notification({
title: options.title,
body: options.body,
Expand Down
Loading

0 comments on commit cef7dcb

Please sign in to comment.