-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationService.ts
More file actions
186 lines (165 loc) · 6.46 KB
/
notificationService.ts
File metadata and controls
186 lines (165 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import messaging from "@react-native-firebase/messaging";
import AsyncStorage from "@react-native-async-storage/async-storage";
import {
closeSDKSession,
handleProactiveLink,
handlePush,
registerPushToken
} from "helpshift-plugin-sdkx-react-native";
import {
Notification,
Notifications,
Registered,
RegistrationError
} from "react-native-notifications";
import {Alert} from "react-native";
export const requestNotificationPermission = async (): Promise<string> => {
const notificationStatus = await messaging().requestPermission();
const enabled =
notificationStatus === messaging.AuthorizationStatus.AUTHORIZED ||
notificationStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
return getFcmToken();
} else {
return "";
}
};
const getFcmToken = async (): Promise<string> => {
const oldToken = await AsyncStorage.getItem("FCMTOKEN");
if (oldToken) return oldToken;
try {
const newToken = await messaging().getToken();
AsyncStorage.setItem("FCMTOKEN", newToken);
return newToken;
} catch (error) {
console.log("not able to get fcm token");
return "";
}
};
export const notificationListenerAndroid = async (): Promise<[() => void, () => void]> => {
const token = await requestNotificationPermission();
console.log("syncing token is", token);
if (token !== "") {
registerPushToken(token);
}
const onOpenUnsubscribe = messaging().onNotificationOpenedApp((remoteMessage) => {
const notificationData = remoteMessage.data;
if (notificationData !== undefined) {
const isHandleProactiveLink = !!notificationData?.helpshift_proactive_link;
const doesNotificationDataHaveOrigin = notificationData?.origin === "helpshift";
if (isHandleProactiveLink) {
let proactiveLink = handleProactiveLink(
(notificationData?.helpshift_proactive_link as string) || "",
{}
);
} else if (doesNotificationDataHaveOrigin) {
handlePush(remoteMessage?.data || {}, false);
} else {
// handle your notification
}
}
});
const onMessageUnsubscribe = messaging().onMessage(async (remoteMessage) => {
const notificationData = remoteMessage.data;
if (notificationData !== undefined) {
const shoudlCloseSDKSession = !!notificationData?.close_session;
const isHandleProactiveLink = !!notificationData?.helpshift_proactive_link;
const doesNotificationDataHaveOrigin = notificationData?.origin === "helpshift";
if (shoudlCloseSDKSession) {
closeSDKSession();
} else if (isHandleProactiveLink) {
proactiveAlert((notificationData?.helpshift_proactive_link as string) || "");
} else if (doesNotificationDataHaveOrigin) {
handlePush(remoteMessage?.data || {}, false);
} else {
// handle your notification
}
}
});
return [onMessageUnsubscribe, onOpenUnsubscribe];
};
const proactiveAlert = (data: string) =>
Alert.alert("Helpshift Notify", "Proactive alert", [
{
text: "Cancel",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"
},
{text: "OK", onPress: () => handleProactiveLink(data, {})}
]);
export const notificationListeneriOS = () => {
Notifications.registerRemoteNotifications();
Notifications.events().registerRemoteNotificationsRegistered((event: Registered) => {
registerPushToken(event.deviceToken);
AsyncStorage.setItem("FCMTOKEN", event.deviceToken);
});
Notifications.events().registerRemoteNotificationsRegistrationFailed(
(event: RegistrationError) => {
console.error(event);
}
);
Notifications.events().registerNotificationReceivedForeground(
(notification: Notification, completion) => {
const notificationData = notification.payload;
console.log("registerNotificationReceivedForeground");
const doesNotificationDataHaveProactiveLink = !!notificationData?.helpshift_proactive_link;
const doesNotificationDataHaveOrigin = notificationData?.origin === "helpshift";
if (doesNotificationDataHaveProactiveLink) {
console.log("helpshift_proactive_link push notification receive");
completion({alert: true, sound: false, badge: false});
} else if (doesNotificationDataHaveOrigin) {
console.log("doesNotificationDataHaveOrigin");
handlePush(notificationData, false);
completion({alert: false, sound: false, badge: false});
} else {
completion({alert: true, sound: true, badge: false});
}
}
);
Notifications.events().registerNotificationOpened(
(notification: Notification, completion: () => void) => {
console.log("Notification opened", JSON.stringify(notification.payload));
const notificationData = notification.payload;
console.log(
"registerNotificationOpened: registerNotificationReceivedBackground",
notificationData
);
const doesNotificationDataHaveProactiveLink = !!notificationData?.helpshift_proactive_link;
const doesNotificationDataHaveOrigin = notificationData?.origin === "helpshift";
const doesNotificationDataHaveCloseSession = !!notificationData?.close_session;
if (doesNotificationDataHaveCloseSession) {
closeSDKSession();
console.log("Close Session SDK");
} else if (doesNotificationDataHaveProactiveLink) {
handleProactiveLink(notificationData.helpshift_proactive_link, {
proactive: 1,
link: 2
});
} else if (doesNotificationDataHaveOrigin) {
handlePush(notificationData, false);
} else {
console.log("registerNotificationOpened: customer-app notification");
}
completion();
}
);
Notifications.getInitialNotification()
.then((notification) => {
if (!notification) return;
const notificationData = notification.payload;
console.log("getInitialNotification called", notificationData);
const doesNotificationDataHaveProactiveLink = !!notificationData?.helpshift_proactive_link;
const doesNotificationDataHaveOrigin = notificationData?.origin === "helpshift";
if (doesNotificationDataHaveProactiveLink) {
handleProactiveLink(notificationData.helpshift_proactive_link, {
proactive: 1,
link: 2
});
} else if (doesNotificationDataHaveOrigin) {
handlePush(notificationData, true);
} else {
console.log("getInitialNotification: customer-app notification");
}
})
.catch((err) => console.error("getInitialNotifiation() failed", err));
};