Skip to content

Commit a058ab7

Browse files
authored
fix: pn integration changes for new pn update (#2984)
* fix: pn integration changes for new pn update * fix: revert different shard changes * fix: remove console.log * chore: always rely on notification payload
1 parent e41f73a commit a058ab7

File tree

1 file changed

+18
-70
lines changed

1 file changed

+18
-70
lines changed

examples/SampleApp/src/hooks/useChatClient.ts

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,6 @@ const requestNotificationPermission = async () => {
1919
console.log('Permission Status', { authStatus, isEnabled });
2020
};
2121

22-
messaging.setBackgroundMessageHandler(async (remoteMessage) => {
23-
const messageId = remoteMessage.data?.id as string;
24-
if (!messageId) {
25-
return;
26-
}
27-
const config = await AsyncStore.getItem<LoginConfig | null>(
28-
'@stream-rn-sampleapp-login-config',
29-
null,
30-
);
31-
if (!config) {
32-
return;
33-
}
34-
35-
const client = StreamChat.getInstance(config.apiKey);
36-
37-
const user = {
38-
id: config.userId,
39-
image: config.userImage,
40-
name: config.userName,
41-
};
42-
43-
await client._setToken(user, config.userToken);
44-
const message = await client.getMessage(messageId);
45-
46-
// create the android channel to send the notification to
47-
const channelId = await notifee.createChannel({
48-
id: 'chat-messages',
49-
name: 'Chat Messages',
50-
});
51-
52-
if (message.message.user?.name && message.message.text && !remoteMessage.notification) {
53-
const { stream, ...rest } = remoteMessage.data ?? {};
54-
const data = {
55-
...rest,
56-
...((stream as unknown as Record<string, string> | undefined) ?? {}), // extract and merge stream object if present
57-
};
58-
await notifee.displayNotification({
59-
android: {
60-
channelId,
61-
pressAction: {
62-
id: 'default',
63-
},
64-
},
65-
title: 'New message from ' + message.message.user.name,
66-
body: message.message.text,
67-
data,
68-
});
69-
}
70-
});
71-
7222
const requestAndroidPermission = async () => {
7323
if (Platform.OS === 'android' && Platform.Version >= 33) {
7424
const result = await PermissionsAndroid.request(
@@ -133,7 +83,6 @@ export const useChatClient = () => {
13383
providerNameOverride && providerNameOverride?.length > 0
13484
? providerNameOverride
13585
: (provider?.name ?? 'rn-fcm');
136-
console.log(id, name);
13786
const token = id === 'firebase' ? firebaseToken : (apnsToken ?? firebaseToken);
13887
await client.addDevice(token, id as PushProvider, client.userID, name);
13988

@@ -145,33 +94,32 @@ export const useChatClient = () => {
14594
});
14695
// show notifications when on foreground
14796
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-
};
97+
const { stream, ...rest } = remoteMessage.data ?? {};
98+
const data = {
99+
...rest,
100+
...((stream as unknown as Record<string, string> | undefined) ?? {}), // extract and merge stream object if present
101+
};
102+
const channelId = await notifee.createChannel({
103+
id: 'foreground',
104+
name: 'Foreground Messages',
105+
});
106+
// create the android channel to send the notification to
107+
// display the notification on foreground
108+
const notification = remoteMessage.notification ?? {};
109+
const body = (data.body ?? notification.body) as string;
110+
const title = (data.title ?? notification.title) as string;
111+
112+
if (body && title) {
165113
await notifee.displayNotification({
166114
android: {
167115
channelId,
168116
pressAction: {
169117
id: 'default',
170118
},
171119
},
172-
body: message.message.text,
120+
body,
121+
title,
173122
data,
174-
title: 'New message from ' + message.message.user.name,
175123
});
176124
}
177125
});

0 commit comments

Comments
 (0)