Changeset View
Changeset View
Standalone View
Standalone View
native/push/android.js
// @flow | // @flow | ||||
import { NativeModules, NativeEventEmitter } from 'react-native'; | import { NativeModules, NativeEventEmitter } from 'react-native'; | ||||
import { mergePrefixIntoBody } from 'lib/shared/notif-utils.js'; | import { mergePrefixIntoBody } from 'lib/shared/notif-utils.js'; | ||||
type CommAndroidNotificationsModuleType = { | type CommAndroidNotificationsModuleType = { | ||||
+removeAllActiveNotificationsForThread: (threadID: string) => void, | +removeAllActiveNotificationsForThread: (threadID: string) => void, | ||||
+getInitialNotification: () => Promise<?AndroidForegroundMessage>, | +getInitialNotification: () => Promise<?AndroidMessage>, | ||||
+createChannel: ( | +createChannel: ( | ||||
channelID: string, | channelID: string, | ||||
name: string, | name: string, | ||||
importance: number, | importance: number, | ||||
description: ?string, | description: ?string, | ||||
) => void, | ) => void, | ||||
+getConstants: () => { +NOTIFICATIONS_IMPORTANCE_HIGH: number, ... }, | +getConstants: () => { +NOTIFICATIONS_IMPORTANCE_HIGH: number, ... }, | ||||
+setBadge: (count: number) => void, | +setBadge: (count: number) => void, | ||||
+removeAllDeliveredNotifications: () => void, | +removeAllDeliveredNotifications: () => void, | ||||
+hasPermission: () => Promise<boolean>, | +hasPermission: () => Promise<boolean>, | ||||
+getToken: () => Promise<string>, | +getToken: () => Promise<string>, | ||||
+requestNotificationsPermission: () => Promise<boolean>, | +requestNotificationsPermission: () => Promise<boolean>, | ||||
+canRequestNotificationsPermissionFromUser: () => Promise<boolean>, | +canRequestNotificationsPermissionFromUser: () => Promise<boolean>, | ||||
+NOTIFICATIONS_IMPORTANCE_HIGH: string, | +NOTIFICATIONS_IMPORTANCE_HIGH: string, | ||||
}; | }; | ||||
export type AndroidForegroundMessage = { | export type AndroidMessage = { | ||||
+body: string, | +body: string, | ||||
+title: string, | +title: string, | ||||
+threadID: string, | +threadID: string, | ||||
+prefix?: string, | +prefix?: string, | ||||
+messageInfos: ?string, | +messageInfos: ?string, | ||||
}; | }; | ||||
const { CommAndroidNotificationsEventEmitter } = NativeModules; | const { CommAndroidNotificationsEventEmitter } = NativeModules; | ||||
const CommAndroidNotifications: CommAndroidNotificationsModuleType = | const CommAndroidNotifications: CommAndroidNotificationsModuleType = | ||||
NativeModules.CommAndroidNotifications; | NativeModules.CommAndroidNotifications; | ||||
const androidNotificationChannelID = 'default'; | const androidNotificationChannelID = 'default'; | ||||
function handleAndroidMessage( | function handleAndroidMessage( | ||||
message: AndroidForegroundMessage, | message: AndroidMessage, | ||||
updatesCurrentAsOf: number, | updatesCurrentAsOf: number, | ||||
handleIfActive?: ( | handleIfActive?: ( | ||||
threadID: string, | threadID: string, | ||||
texts: { body: string, title: ?string }, | texts: { body: string, title: ?string }, | ||||
) => boolean, | ) => boolean, | ||||
) { | ) { | ||||
const { title, prefix, threadID } = message; | const { title, prefix, threadID } = message; | ||||
let { body } = message; | let { body } = message; | ||||
({ body } = mergePrefixIntoBody({ body, title, prefix })); | ({ body } = mergePrefixIntoBody({ body, title, prefix })); | ||||
if (handleIfActive) { | if (handleIfActive) { | ||||
const texts = { title, body }; | const texts = { title, body }; | ||||
const isActive = handleIfActive(threadID, texts); | const isActive = handleIfActive(threadID, texts); | ||||
if (isActive) { | if (isActive) { | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
function getCommAndroidNotificationsEventEmitter(): NativeEventEmitter<{ | function getCommAndroidNotificationsEventEmitter(): NativeEventEmitter<{ | ||||
commAndroidNotificationsToken: [string], | commAndroidNotificationsToken: [string], | ||||
commAndroidNotificationsForegroundMessage: [AndroidForegroundMessage], | commAndroidNotificationsMessage: [AndroidMessage], | ||||
commAndroidNotificationsNotificationOpened: [AndroidForegroundMessage], | commAndroidNotificationsNotificationOpened: [AndroidMessage], | ||||
}> { | }> { | ||||
return new NativeEventEmitter(CommAndroidNotificationsEventEmitter); | return new NativeEventEmitter(CommAndroidNotificationsEventEmitter); | ||||
} | } | ||||
export { | export { | ||||
androidNotificationChannelID, | androidNotificationChannelID, | ||||
handleAndroidMessage, | handleAndroidMessage, | ||||
getCommAndroidNotificationsEventEmitter, | getCommAndroidNotificationsEventEmitter, | ||||
CommAndroidNotifications, | CommAndroidNotifications, | ||||
}; | }; |