forked from RocketChat/Rocket.Chat.ReactNative
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Migrate notification/push to Typescript (RocketChat#3587)
- Loading branch information
Showing
10 changed files
with
181 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface INotification { | ||
message: string; | ||
style: string; | ||
ejson: string; | ||
collapse_key: string; | ||
notId: string; | ||
msgcnt: string; | ||
title: string; | ||
from: string; | ||
image: string; | ||
soundname: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import EJSON from 'ejson'; | ||
|
||
import store from '../../lib/createStore'; | ||
import { deepLinkingOpen } from '../../actions/deepLinking'; | ||
import { isFDroidBuild } from '../../constants/environment'; | ||
import PushNotification from './push'; | ||
import { INotification, SubscriptionType } from '../../definitions'; | ||
|
||
interface IEjson { | ||
rid: string; | ||
name: string; | ||
sender: { username: string; name: string }; | ||
type: string; | ||
host: string; | ||
messageType: string; | ||
messageId: string; | ||
} | ||
|
||
export const onNotification = (notification: INotification): void => { | ||
if (notification) { | ||
try { | ||
const { rid, name, sender, type, host, messageType, messageId }: IEjson = EJSON.parse(notification.ejson); | ||
|
||
const types: Record<string, string> = { | ||
c: 'channel', | ||
d: 'direct', | ||
p: 'group', | ||
l: 'channels' | ||
}; | ||
let roomName = type === SubscriptionType.DIRECT ? sender.username : name; | ||
if (type === SubscriptionType.OMNICHANNEL) { | ||
roomName = sender.name; | ||
} | ||
|
||
const params = { | ||
host, | ||
rid, | ||
messageId, | ||
path: `${types[type]}/${roomName}`, | ||
isCall: messageType === 'jitsi_call_started' | ||
}; | ||
// TODO REDUX MIGRATION TO TS | ||
store.dispatch(deepLinkingOpen(params)); | ||
} catch (e) { | ||
console.warn(e); | ||
} | ||
} | ||
}; | ||
|
||
export const getDeviceToken = (): string => PushNotification.getDeviceToken(); | ||
export const setBadgeCount = (count?: number): void => PushNotification.setBadgeCount(count); | ||
export const initializePushNotifications = (): Promise<INotification> | undefined => { | ||
if (!isFDroidBuild) { | ||
setBadgeCount(); | ||
return PushNotification.configure(onNotification); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// @ts-ignore | ||
// TODO BUMP LIB VERSION | ||
import NotificationsIOS, { NotificationAction, NotificationCategory, Notification } from 'react-native-notifications'; | ||
|
||
import reduxStore from '../../lib/createStore'; | ||
import I18n from '../../i18n'; | ||
import { INotification } from '../../definitions/INotification'; | ||
|
||
class PushNotification { | ||
onNotification: (notification: Notification) => void; | ||
deviceToken: string; | ||
|
||
constructor() { | ||
this.onNotification = () => {}; | ||
this.deviceToken = ''; | ||
|
||
NotificationsIOS.addEventListener('remoteNotificationsRegistered', (deviceToken: string) => { | ||
this.deviceToken = deviceToken; | ||
}); | ||
|
||
NotificationsIOS.addEventListener('notificationOpened', (notification: Notification, completion: () => void) => { | ||
// TODO REDUX MIGRATION TO TS | ||
const { background } = reduxStore.getState().app; | ||
if (background) { | ||
this.onNotification(notification?.getData()); | ||
} | ||
completion(); | ||
}); | ||
|
||
const actions = [ | ||
new NotificationCategory({ | ||
identifier: 'MESSAGE', | ||
actions: [ | ||
new NotificationAction({ | ||
activationMode: 'background', | ||
title: I18n.t('Reply'), | ||
textInput: { | ||
buttonTitle: I18n.t('Reply'), | ||
placeholder: I18n.t('Type_message') | ||
}, | ||
identifier: 'REPLY_ACTION' | ||
}) | ||
] | ||
}) | ||
]; | ||
NotificationsIOS.requestPermissions(actions); | ||
} | ||
|
||
getDeviceToken() { | ||
return this.deviceToken; | ||
} | ||
|
||
setBadgeCount = (count = 0) => { | ||
NotificationsIOS.setBadgesCount(count); | ||
}; | ||
|
||
async configure(onNotification: (notification: INotification) => void) { | ||
this.onNotification = onNotification; | ||
const initial = await NotificationsIOS.getInitialNotification(); | ||
return Promise.resolve(initial); | ||
} | ||
} | ||
export default new PushNotification(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// @ts-ignore | ||
// TODO BUMP LIB VERSION | ||
import { NotificationsAndroid, PendingNotifications, Notification } from 'react-native-notifications'; | ||
|
||
import { INotification } from '../../definitions/INotification'; | ||
|
||
class PushNotification { | ||
onNotification: (notification: Notification) => void; | ||
deviceToken: string; | ||
constructor() { | ||
this.onNotification = () => {}; | ||
this.deviceToken = ''; | ||
|
||
NotificationsAndroid.setRegistrationTokenUpdateListener((deviceToken: string) => { | ||
this.deviceToken = deviceToken; | ||
}); | ||
|
||
NotificationsAndroid.setNotificationOpenedListener((notification: Notification) => { | ||
this.onNotification(notification?.getData()); | ||
}); | ||
} | ||
|
||
getDeviceToken() { | ||
return this.deviceToken; | ||
} | ||
|
||
setBadgeCount = (_?: number) => {}; | ||
|
||
configure(onNotification: (notification: INotification) => void) { | ||
this.onNotification = onNotification; | ||
NotificationsAndroid.refreshToken(); | ||
return PendingNotifications.getInitialNotification(); | ||
} | ||
} | ||
|
||
export default new PushNotification(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters