Skip to content

Commit 0626908

Browse files
committed
fix: pn integration changes for new pn update
1 parent e41f73a commit 0626908

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

examples/SampleApp/src/ChatUsers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const USER_TOKENS: Record<string, string> = {
2323
vishal:
2424
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoidmlzaGFsIn0.HOlVh-ZyQnjyuL20G-67RTgKufBuAH-I-gbEELFlass',
2525
ivan5:
26-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoiaXZhbjUifQ.c3nq6rlnqyNeCRcwiW0VQ413nHl99pF72Ia2V_N84yU',
26+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3Byb250by5nZXRzdHJlYW0uaW8iLCJzdWIiOiJ1c2VyL2l2YW41IiwidXNlcl9pZCI6Iml2YW41IiwidmFsaWRpdHlfaW5fc2Vjb25kcyI6NjA0ODAwLCJpYXQiOjE3NDA1Njc4NTgsImV4cCI6MTc0MTE3MjY1OH0.91UVXG9jAghvNOrK93lwt0oCknHouBW_ft4reAnwsI0',
2727
rodolphe:
2828
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoicm9kb2xwaGUifQ.tLl-I8ADBhTKB-x5FB9jK4-am0dELLXgydM6VN9rTL8',
2929
};

examples/SampleApp/src/hooks/useChatClient.ts

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const requestNotificationPermission = async () => {
2020
};
2121

2222
messaging.setBackgroundMessageHandler(async (remoteMessage) => {
23+
if (remoteMessage.data?.type !== 'message.new') {
24+
// handled through the new push system, we don't need this
25+
return;
26+
}
2327
const messageId = remoteMessage.data?.id as string;
2428
if (!messageId) {
2529
return;
@@ -145,34 +149,51 @@ export const useChatClient = () => {
145149
});
146150
// show notifications when on foreground
147151
const unsubscribeForegroundMessageReceive = messaging.onMessage(async (remoteMessage) => {
148-
const messageId = remoteMessage.data?.id;
149-
if (!messageId) {
150-
return;
151-
}
152-
const message = await client.getMessage(messageId as string);
153-
if (message.message.user?.name && message.message.text) {
154-
// create the android channel to send the notification to
155-
const channelId = await notifee.createChannel({
156-
id: 'foreground',
157-
name: 'Foreground Messages',
158-
});
159-
// display the notification on foreground
160-
const { stream, ...rest } = remoteMessage.data ?? {};
161-
const data = {
162-
...rest,
163-
...((stream as unknown as Record<string, string> | undefined) ?? {}), // extract and merge stream object if present
164-
};
165-
await notifee.displayNotification({
166-
android: {
167-
channelId,
168-
pressAction: {
169-
id: 'default',
152+
const notificationType = remoteMessage.data?.type;
153+
const { stream, ...rest } = remoteMessage.data ?? {};
154+
const data = {
155+
...rest,
156+
...((stream as unknown as Record<string, string> | undefined) ?? {}), // extract and merge stream object if present
157+
};
158+
const channelId = await notifee.createChannel({
159+
id: 'foreground',
160+
name: 'Foreground Messages',
161+
});
162+
if (notificationType === 'message.new') {
163+
const messageId = remoteMessage.data?.id;
164+
if (!messageId) {
165+
return;
166+
}
167+
const message = await client.getMessage(messageId as string);
168+
if (message.message.user?.name && message.message.text) {
169+
// create the android channel to send the notification to
170+
// display the notification on foreground
171+
await notifee.displayNotification({
172+
android: {
173+
channelId,
174+
pressAction: {
175+
id: 'default',
176+
},
177+
},
178+
body: message.message.text,
179+
title: 'New message from ' + message.message.user.name,
180+
data,
181+
});
182+
}
183+
} else {
184+
if (data.body && data.title) {
185+
await notifee.displayNotification({
186+
android: {
187+
channelId,
188+
pressAction: {
189+
id: 'default',
190+
},
170191
},
171-
},
172-
body: message.message.text,
173-
data,
174-
title: 'New message from ' + message.message.user.name,
175-
});
192+
body: data.body as string,
193+
title: data.title as string,
194+
data,
195+
});
196+
}
176197
}
177198
});
178199

@@ -194,7 +215,7 @@ export const useChatClient = () => {
194215
try {
195216
if (userId) {
196217
await loginUser({
197-
apiKey: 'yjrt5yxw77ev',
218+
apiKey: 'pd67s34fzpgw',
198219
userId: USERS[userId].id,
199220
userImage: USERS[userId].image,
200221
userName: USERS[userId].name,

0 commit comments

Comments
 (0)