From 8764c0f075099a6eb0a498de9ed8f93c0e1c2d65 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Wed, 7 Jun 2023 22:57:16 -0700 Subject: [PATCH] handle unified push notifications --- .../src/main/java/com/databag/CustomReceiver.java | 12 +++++++----- app/mobile/src/api/setAccountAccess.js | 2 +- app/mobile/src/api/setLogin.js | 2 +- app/mobile/src/context/useAppContext.hook.js | 9 ++++++++- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/mobile/android/app/src/main/java/com/databag/CustomReceiver.java b/app/mobile/android/app/src/main/java/com/databag/CustomReceiver.java index c621c015..7152ebe4 100644 --- a/app/mobile/android/app/src/main/java/com/databag/CustomReceiver.java +++ b/app/mobile/android/app/src/main/java/com/databag/CustomReceiver.java @@ -16,6 +16,7 @@ import android.os.Build; import android.net.Uri; import android.media.RingtoneManager; import androidx.core.app.NotificationCompat; +import java.nio.charset.StandardCharsets; import com.facebook.react.ReactApplication; import com.facebook.react.ReactInstanceManager; @@ -71,6 +72,9 @@ public class CustomReceiver extends MessagingReceiver { return; } + String strMessage = new String(message, StandardCharsets.UTF_8); + + Intent intent = new Intent(context, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 /* Request code */, intent, @@ -81,21 +85,19 @@ public class CustomReceiver extends MessagingReceiver { NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId) .setSmallIcon(R.mipmap.ic_launcher) - .setContentTitle("FCM Message").setContentText("ROLO?").setAutoCancel(true).setSound( + .setContentTitle(strMessage).setAutoCancel(true).setSound( defaultSoundUri).setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE); -if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel); - } + } notificationManager.notify(0, notificationBuilder.build()); - - // Called when a new message is received. The message contains the full POST body of the push message } } diff --git a/app/mobile/src/api/setAccountAccess.js b/app/mobile/src/api/setAccountAccess.js index d7949e7d..344f414e 100644 --- a/app/mobile/src/api/setAccountAccess.js +++ b/app/mobile/src/api/setAccountAccess.js @@ -1,7 +1,7 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; export async function setAccountAccess(server, token, appName, appVersion, platform, deviceToken, notifications) { - let access = await fetchWithTimeout(`https://${server}/account/access?token=${token}&appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}`, { method: 'PUT', body: JSON.stringify(notifications) }) + let access = await fetchWithTimeout(`https://${server}/account/access?token=${token}&appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}&pushType=up`, { method: 'PUT', body: JSON.stringify(notifications) }) checkResponse(access) return await access.json() } diff --git a/app/mobile/src/api/setLogin.js b/app/mobile/src/api/setLogin.js index 3487da97..cca101f6 100644 --- a/app/mobile/src/api/setLogin.js +++ b/app/mobile/src/api/setLogin.js @@ -4,7 +4,7 @@ import base64 from 'react-native-base64' export async function setLogin(username, server, password, appName, appVersion, platform, deviceToken, notifications) { let headers = new Headers() headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password)); - let login = await fetchWithTimeout(`https://${server}/account/apps?appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}`, { method: 'POST', body: JSON.stringify(notifications), headers: headers }) + let login = await fetchWithTimeout(`https://${server}/account/apps?appName=${appName}&appVersion=${appVersion}&platform=${platform}&deviceToken=${deviceToken}&pushType=up`, { method: 'POST', body: JSON.stringify(notifications), headers: headers }) checkResponse(login) return await login.json() } diff --git a/app/mobile/src/context/useAppContext.hook.js b/app/mobile/src/context/useAppContext.hook.js index 7abcda13..c741619b 100644 --- a/app/mobile/src/context/useAppContext.hook.js +++ b/app/mobile/src/context/useAppContext.hook.js @@ -43,7 +43,7 @@ export function useAppContext() { useEffect(() => { DeviceEventEmitter.addListener('unifiedPushURL', (e) => { - Alert.alert("PUSH ENDPOINT", JSON.stringify(e)); + deviceToken.current = e.endpoint; }); (async () => { @@ -81,6 +81,13 @@ export function useAppContext() { } const notifications = [ + { event: 'contact.addCard', messageTitle: 'New Contact Request' }, + { event: 'contact.updateCard', messageTitle: 'Contact Update' }, + { event: 'content.addChannel.superbasic', messageTitle: 'New Topic' }, + { event: 'content.addChannel.sealed', messageTitle: 'New Topic' }, + { event: 'content.addChannelTopic.superbasic', messageTitle: 'New Topic Message' }, + { event: 'content.addChannelTopic.sealed', messageTitle: 'New Topic Message' }, + { event: 'ring', messageTitle: 'Incoming Call' }, ]; const actions = {