Skip to content

Commit

Permalink
chore(background-notifications): decrypt & display notification while…
Browse files Browse the repository at this point in the history
… app is in background
  • Loading branch information
Cali93 committed Sep 6, 2023
1 parent 3879421 commit a692885
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 22 deletions.
4 changes: 3 additions & 1 deletion dapps/web3inbox/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ yarn-error.log
# testing
/coverage

.env
.env

/android/app/google-services.json
11 changes: 5 additions & 6 deletions dapps/web3inbox/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ const projectId = ENV_PROJECT_ID as string;
export default function Native() {
const {provider, isConnected, address, open} = useWalletConnectModal();
const [isVisible, setIsVisible] = useState(true);

const toggleWeb3InboxModal = useCallback(
() => setIsVisible(isCurrentlyVisible => !isCurrentlyVisible),
[],
);

const handleSign = useCallback(
async (message: string) => {
if (!provider || !address) {
Expand All @@ -53,6 +47,11 @@ export default function Native() {
[provider, address],
);

const toggleWeb3InboxModal = useCallback(
() => setIsVisible(isCurrentlyVisible => !isCurrentlyVisible),
[],
);

return (
<View style={styles.container}>
<Text style={styles.header}>Web3Inbox React Native Webview</Text>
Expand Down
1 change: 1 addition & 0 deletions dapps/web3inbox/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: "com.android.application"
apply plugin: "com.facebook.react"
apply plugin: 'com.google.gms.google-services'

/**
* This is the configuration block to customize your React Native Android app.
Expand Down
2 changes: 1 addition & 1 deletion dapps/web3inbox/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.walletconnect.w3i.rnexample">

<uses-permission android:name="android.permission.INTERNET" />

Expand Down
1 change: 1 addition & 0 deletions dapps/web3inbox/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ buildscript {
dependencies {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("com.google.gms:google-services:4.3.15")
}
}
92 changes: 90 additions & 2 deletions dapps/web3inbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
* @format
*/

import {AppRegistry} from 'react-native';
import {AppRegistry, PermissionsAndroid} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import crypto from 'react-native-quick-crypto';
import messaging from '@react-native-firebase/messaging';
import notifee, {AndroidVisibility, EventType} from '@notifee/react-native';
import {NotifyClient} from '@walletconnect/notify-client';
import {Core} from '@walletconnect/core';

const polyfillDigest = async (algorithm, data) => {
const algo = algorithm.replace('-', '').toLowerCase();
Expand All @@ -19,4 +23,88 @@ globalThis.crypto.subtle = {
digest: polyfillDigest,
};

AppRegistry.registerComponent(appName, () => App);
messaging()
.getToken()
.then(async token => {
console.log({token});
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
console.log({enabled});

if (!enabled) {
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
);
}
messaging().setAutoInitEnabled(true);
});

messaging().setBackgroundMessageHandler(async remoteMessage => {
const projectId = process.env.ENV_PROJECT_ID;
const relayUrl = process.env.ENV_RELAY_URL;
const core = new Core({
projectId,
relayUrl,
});
const notifyClient = await NotifyClient.init({
core,
projectId,
relayUrl,
});
console.log({blob: remoteMessage.data?.blob});
if (!remoteMessage.data?.blob || !notifyClient) {
return;
}
const decryptedMessage = await notifyClient?.decryptMessage({
topic: remoteMessage.data?.topic,
encryptedMessage: remoteMessage.data?.blob,
});
console.log(
'A new background FCM message arrived!',
JSON.stringify(decryptedMessage),
);

const channelId = await notifee.createChannel({
id: 'default',
name: 'Default Channel',
});

// Display a notification
await notifee.displayNotification({
title: decryptedMessage.title,
body: decryptedMessage.body,
android: {
channelId,
visibility: AndroidVisibility.PUBLIC,
smallIcon: 'ic_launcher', // optional, defaults to 'ic_launcher'.
// pressAction is needed if you want the notification to open the app when pressed
pressAction: {
id: 'mark-as-read',
},
},
});
});

notifee.onBackgroundEvent(async ({type, detail}) => {
const {notification, pressAction} = detail;

// Check if the user pressed the "Mark as read" action
if (type === EventType.ACTION_PRESS && pressAction.id === 'mark-as-read') {
// Remove the notification
await notifee.cancelNotification(notification.id);
}
});

function HeadlessCheck({isHeadless}) {
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return null;
}

// Render the app component on foreground launch
return <App />;
}

AppRegistry.registerComponent(appName, () => HeadlessCheck);
8 changes: 6 additions & 2 deletions dapps/web3inbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android --appId=com.w3idapp",
"android": "react-native run-android --appId=com.walletconnect.w3i.rnexample",
"android:build": "cd android && ./gradlew clean && ./gradlew assembleRelease",
"ios": "react-native run-ios",
"lint": "eslint .",
Expand All @@ -13,12 +13,16 @@
"dependencies": {
"@craftzdog/react-native-buffer": "^6.0.5",
"@json-rpc-tools/utils": "1.7.6",
"@notifee/react-native": "^7.8.0",
"@react-native-async-storage/async-storage": "1.19.1",
"@react-native-community/netinfo": "^9.4.1",
"@react-native-firebase/app": "^18.3.2",
"@react-native-firebase/messaging": "^18.3.2",
"@react-navigation/native": "6.1.6",
"@react-navigation/native-stack": "6.9.12",
"@walletconnect/core": "^2.10.0",
"@walletconnect/modal-react-native": "1.0.0-rc.9",
"@walletconnect/notify-client": "^0.12.0",
"@walletconnect/react-native-compat": "^2.10.0",
"@walletconnect/types": "^2.10.0",
"@walletconnect/web3inbox-webview": "^0.0.7",
Expand All @@ -33,7 +37,7 @@
"react-native-safe-area-context": "4.5.3",
"react-native-screens": "3.20.0",
"react-native-svg": "13.9.0",
"react-native-webview": "13.3.1",
"react-native-webview": "^13.5.1",
"stream-browserify": "^3.0.0",
"text-encoding": "0.7.0"
},
Expand Down
97 changes: 87 additions & 10 deletions dapps/web3inbox/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1899,6 +1899,11 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@notifee/react-native@^7.8.0":
version "7.8.0"
resolved "https://registry.yarnpkg.com/@notifee/react-native/-/react-native-7.8.0.tgz#2990883753990f3585aa0cb5becc5cbdbcd87a43"
integrity sha512-sx8h62U4FrR4pqlbN1rkgPsdamDt9Tad0zgfO6VtP6rNJq/78k8nxUnh0xIX3WPDcCV8KAzdYCE7+UNvhF1CpQ==

"@pedrouid/environment@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@pedrouid/environment/-/environment-1.0.1.tgz#858f0f8a057340e0b250398b75ead77d6f4342ec"
Expand Down Expand Up @@ -2080,6 +2085,19 @@
resolved "https://registry.yarnpkg.com/@react-native-community/netinfo/-/netinfo-9.4.1.tgz#7b880758adca65fe47ee866cf7b00416b9dcc192"
integrity sha512-dAbY5mfw+6Kas/GJ6QX9AZyY+K+eq9ad4Su6utoph/nxyH3whp5cMSgRNgE2VhGQVRZ/OG0qq3IaD3+wzoqJXw==

"@react-native-firebase/app@^18.3.2":
version "18.3.2"
resolved "https://registry.yarnpkg.com/@react-native-firebase/app/-/app-18.3.2.tgz#c08da9f01b3642c3f4c10832fbd078012f65e736"
integrity sha512-2yX1PtpcVQirLDfnZiBU44vRr4jeIuvAf2oAAaf+/zBcDApU5cwPfyFoCCANfqR8W3w8zs9h9UyhRYdnksM4sQ==
dependencies:
opencollective-postinstall "^2.0.1"
superstruct "^0.6.2"

"@react-native-firebase/messaging@^18.3.2":
version "18.3.2"
resolved "https://registry.yarnpkg.com/@react-native-firebase/messaging/-/messaging-18.3.2.tgz#f61d10648afed567a488ef37ac6af8242509daa0"
integrity sha512-An8cRsYjpUdE5S9NU1d/8+pc8mGxqsPEB7yGbRps2JzaJsII8I63LdT/j2P2QYMx4a3RVQN4Nauw4Y07rrVVhA==

"@react-native/assets-registry@^0.72.0":
version "0.72.0"
resolved "https://registry.yarnpkg.com/@react-native/assets-registry/-/assets-registry-0.72.0.tgz#c82a76a1d86ec0c3907be76f7faf97a32bbed05d"
Expand Down Expand Up @@ -3832,6 +3850,16 @@ cliui@^8.0.1:
strip-ansi "^6.0.1"
wrap-ansi "^7.0.0"

clone-deep@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/clone-deep/-/clone-deep-2.0.2.tgz#00db3a1e173656730d1188c3d6aced6d7ea97713"
integrity sha512-SZegPTKjCgpQH63E+eN6mVEEPdQBOUzjyJm5Pora4lrwWRFS8I0QAxV/KD6vV/i0WuijHZWQC1fMsPEdxfdVCQ==
dependencies:
for-own "^1.0.0"
is-plain-object "^2.0.4"
kind-of "^6.0.0"
shallow-clone "^1.0.0"

clone-deep@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/clone-deep/-/clone-deep-4.0.1.tgz#c19fd9bdbbf85942b4fd979c84dcf7d5f07c2387"
Expand Down Expand Up @@ -4927,6 +4955,23 @@ for-each@^0.3.3:
dependencies:
is-callable "^1.1.3"

for-in@^0.1.3:
version "0.1.8"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-0.1.8.tgz#d8773908e31256109952b1fdb9b3fa867d2775e1"
integrity sha512-F0to7vbBSHP8E3l6dCjxNOLuSFAACIxFy3UehTUlG7svlXi37HHsDkyVcHo0Pq8QwrE+pXvWSVX3ZT1T9wAZ9g==

for-in@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
integrity sha512-7EwmXrOjyL+ChxMhmG5lnW9MPt1aIeZEwKhQzoBUdTV0N3zuwWDZYVJatDvZ2OyzPUvdIAZDsCetk3coyMfcnQ==

for-own@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/for-own/-/for-own-1.0.0.tgz#c63332f415cedc4b04dbfe70cf836494c53cb44b"
integrity sha512-0OABksIGrxKK8K4kynWkQ7y1zounQxP+CWnyclVwj81KW3vlLlGUx57DKGcP/LH216GzqnstnPocF16Nxs0Ycg==
dependencies:
for-in "^1.0.1"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
Expand Down Expand Up @@ -5355,6 +5400,11 @@ is-directory@^0.3.1:
resolved "https://registry.yarnpkg.com/is-directory/-/is-directory-0.3.1.tgz#61339b6f2475fc772fd9c9d83f5c8575dc154ae1"
integrity sha512-yVChGzahRFvbkscn2MlwGismPO12i9+znNruC5gVEntG3qu0xQMzsGg/JFbrsqDOHtHFPci+V5aP5T9I+yeKqw==

is-extendable@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89"
integrity sha512-5BMULNob1vgFX6EjQw5izWDxrecWK9AM72rugNr0TFldMOi0fj6Jk+zeKIt0xGj4cEfQIJth4w3OKWOJ4f+AFw==

is-extglob@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
Expand Down Expand Up @@ -6137,7 +6187,12 @@ keyvaluestorage-interface@^1.0.0:
resolved "https://registry.yarnpkg.com/keyvaluestorage-interface/-/keyvaluestorage-interface-1.0.0.tgz#13ebdf71f5284ad54be94bd1ad9ed79adad515ff"
integrity sha512-8t6Q3TclQ4uZynJY9IGr2+SsIGwK9JHcO6ootkHCGA0CrQCRy+VkouYNO2xicET6b9al7QKzpebNow+gkpCL8g==

kind-of@^6.0.2:
kind-of@^5.0.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-5.1.0.tgz#729c91e2d857b7a419a1f9aa65685c4c33f5845d"
integrity sha512-NGEErnH6F2vUuXDh+OlbcKW7/wOcfdRHaZ7VWtqCztfHri/++YKmP51OdWeGPuqCOba6kk2OTe5d02VmTB80Pw==

kind-of@^6.0.0, kind-of@^6.0.1, kind-of@^6.0.2:
version "6.0.3"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-6.0.3.tgz#07c05034a6c349fa06e24fa35aa76db4580ce4dd"
integrity sha512-dcS1ul+9tmeD95T+x28/ehLgd9mENa3LsvDTtzm3vyBEO7RPptvAD+t44WVXaUjTBRcrpFeFlC8WCruUR456hw==
Expand Down Expand Up @@ -6912,6 +6967,14 @@ minimist@^1.2.6:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

mixin-object@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/mixin-object/-/mixin-object-2.0.1.tgz#4fb949441dab182540f1fe035ba60e1947a5e57e"
integrity sha512-ALGF1Jt9ouehcaXaHhn6t1yGWRqGaHkPFndtFVHfZXOvkIZ/yoGaSi0AHVTafb3ZBGg4dr/bDwnaEKqCXzchMA==
dependencies:
for-in "^0.1.3"
is-extendable "^0.1.1"

mkdirp@^0.5.1:
version "0.5.6"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.6.tgz#7def03d2432dcae4ba1d611445c48396062255f6"
Expand Down Expand Up @@ -7152,6 +7215,11 @@ open@^6.2.0:
dependencies:
is-wsl "^1.1.0"

opencollective-postinstall@^2.0.1:
version "2.0.3"
resolved "https://registry.yarnpkg.com/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz#7a0fff978f6dbfa4d006238fbac98ed4198c3259"
integrity sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==

optionator@^0.9.3:
version "0.9.3"
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.9.3.tgz#007397d44ed1872fdc6ed31360190f81814e2c64"
Expand Down Expand Up @@ -7641,15 +7709,7 @@ react-native-url-polyfill@^2.0.0:
dependencies:
whatwg-url-without-unicode "8.0.0-3"

[email protected]:
version "13.3.1"
resolved "https://registry.yarnpkg.com/react-native-webview/-/react-native-webview-13.3.1.tgz#b368c3fb6a28c81e8ffe74208c899c95ee633ce7"
integrity sha512-jTRC7bZB1gedAbZGGzQUnsaU+avZcZrKLT8g+RR0bsGW80tUOvwezwznKvQ+m1xXOpGFTD1B3+AazUP7+jy4mg==
dependencies:
escape-string-regexp "2.0.0"
invariant "2.2.4"

react-native-webview@^13.2.2:
react-native-webview@^13.2.2, react-native-webview@^13.5.1:
version "13.5.1"
resolved "https://registry.yarnpkg.com/react-native-webview/-/react-native-webview-13.5.1.tgz#1b8e41cea1bdb075c3cc2fde16b0a2208527d124"
integrity sha512-SZQJqFUMxYYj1xYWy1Z48WcHpqOGvbXKS5R1cnaLQY/JxefS+1NOVMqWxy1Zwmc128vqtRklES8l9Jus8tKSLg==
Expand Down Expand Up @@ -8073,6 +8133,15 @@ sha.js@^2.4.0, sha.js@^2.4.8:
inherits "^2.0.1"
safe-buffer "^5.0.1"

shallow-clone@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/shallow-clone/-/shallow-clone-1.0.0.tgz#4480cd06e882ef68b2ad88a3ea54832e2c48b571"
integrity sha512-oeXreoKR/SyNJtRJMAKPDSvd28OqEwG4eR/xc856cRGBII7gX9lvAqDxusPm0846z/w/hWYjI1NpKwJ00NHzRA==
dependencies:
is-extendable "^0.1.1"
kind-of "^5.0.0"
mixin-object "^2.0.1"

shallow-clone@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/shallow-clone/-/shallow-clone-3.0.1.tgz#8f2981ad92531f55035b01fb230769a40e02efa3"
Expand Down Expand Up @@ -8346,6 +8415,14 @@ sudo-prompt@^9.0.0:
resolved "https://registry.yarnpkg.com/sudo-prompt/-/sudo-prompt-9.2.1.tgz#77efb84309c9ca489527a4e749f287e6bdd52afd"
integrity sha512-Mu7R0g4ig9TUuGSxJavny5Rv0egCEtpZRNMrZaYS1vxkiIxGiGUwoezU3LazIQ+KE04hTrTfNPgxU5gzi7F5Pw==

superstruct@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/superstruct/-/superstruct-0.6.2.tgz#c5eb034806a17ff98d036674169ef85e4c7f6a1c"
integrity sha512-lvA97MFAJng3rfjcafT/zGTSWm6Tbpk++DP6It4Qg7oNaeM+2tdJMuVgGje21/bIpBEs6iQql1PJH6dKTjl4Ig==
dependencies:
clone-deep "^2.0.1"
kind-of "^6.0.1"

supports-color@^5.3.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"
Expand Down

0 comments on commit a692885

Please sign in to comment.