Skip to content

Commit d03c905

Browse files
committed
feat: add delivery receipt support inside push notification
1 parent 48a8ede commit d03c905

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

examples/SampleApp/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'react-native-gesture-handler';
22
import { AppRegistry } from 'react-native';
33
import { enableScreens } from 'react-native-screens';
4+
import './src/utils/bootstrapBackgroundMessageHandler';
45

56
import App from './App';
67
import { name as appName } from './app.json';

examples/SampleApp/ios/SampleApp/Info.plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<string>$(PRODUCT_NAME) would like access to your photo gallery to share image in a message.</string>
4848
<key>RCTNewArchEnabled</key>
4949
<true/>
50+
<key>UIBackgroundModes</key>
51+
<array>
52+
<string>fetch</string>
53+
<string>remote-notification</string>
54+
</array>
5055
<key>UILaunchStoryboardName</key>
5156
<string>LaunchScreen</string>
5257
<key>UIRequiredDeviceCapabilities</key>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { LoginConfig } from '../types';
2+
import AsyncStore from './AsyncStore';
3+
import {
4+
FirebaseMessagingTypes,
5+
setBackgroundMessageHandler,
6+
} from '@react-native-firebase/messaging';
7+
import { DeliveredMessageConfirmation, StreamChat } from 'stream-chat';
8+
import notifee from '@notifee/react-native';
9+
import { getMessaging } from '@react-native-firebase/messaging';
10+
11+
const messaging = getMessaging();
12+
13+
const displayNotification = async (
14+
remoteMessage: FirebaseMessagingTypes.RemoteMessage,
15+
channelId: string,
16+
) => {
17+
const { stream, ...rest } = remoteMessage.data ?? {};
18+
const data = {
19+
...rest,
20+
...((stream as unknown as Record<string, string> | undefined) ?? {}), // extract and merge stream object if present
21+
};
22+
if (data.body && data.title) {
23+
await notifee.displayNotification({
24+
android: {
25+
channelId,
26+
pressAction: {
27+
id: 'default',
28+
},
29+
},
30+
body: data.body as string,
31+
title: data.title as string,
32+
data,
33+
});
34+
}
35+
};
36+
37+
setBackgroundMessageHandler(messaging, async (remoteMessage) => {
38+
try {
39+
const loginConfig = await AsyncStore.getItem<LoginConfig>(
40+
'@stream-rn-sampleapp-login-config',
41+
null,
42+
);
43+
if (!loginConfig) {
44+
return;
45+
}
46+
const chatClient = StreamChat.getInstance(loginConfig.apiKey);
47+
await chatClient._setToken({ id: loginConfig.userId }, loginConfig.userToken);
48+
49+
const notification = remoteMessage.data;
50+
51+
const deliverMessageConfirmation = [
52+
{
53+
cid: notification?.cid,
54+
id: notification?.id,
55+
},
56+
];
57+
58+
await chatClient?.markChannelsDelivered({
59+
latest_delivered_messages: deliverMessageConfirmation as DeliveredMessageConfirmation[],
60+
});
61+
// create the android channel to send the notification to
62+
const channelId = await notifee.createChannel({
63+
id: 'chat-messages',
64+
name: 'Chat Messages',
65+
});
66+
// display the notification
67+
await displayNotification(remoteMessage, channelId);
68+
} catch (error) {
69+
console.error(error);
70+
}
71+
});

0 commit comments

Comments
 (0)