mattermost-mobile/app/push_notifications/push_notifications.android.js
Chris Duarte e8712f9199 Cold startup js refactor (#1598)
* Switch to SingleDex and remove all locales

* WIP Mattermost Start Component for lazy loading modules

* Add files changed for native modules

* Add Entry component and app global object

* dispatch setStatusBarHeight for iOS

* Update screen imports

* Include Entry screen

* Refactor app to mattermost.android.js

* Override unnecessary java files

* Fix minor issues in changes

* Display empty state based on user credentials

Also, add proper background theme for empty loading screen

* Add native module constant cache support

* Fix startup theme regression

* Add Keychain support for credentials

* Fix Orientation regression

*  Fix SharedExtension regression

* Emit NATIVE_APP_LAUNCHED across bridge only once during cold start

* Add iOS Support

* Revert to previous implementation of i18n

* Fix styling issues

* Include listener for SERVER_VERSION_CHANGED

* Add SafeAreaView in Entry screen

* Register deviceToken early, in order to get iOS PN Support

* Include StartTimeModule

* Add ReplyFromPush support and remove NATIVE_APP_LAUNCHED listener

* Package native constants in StartTimeModule and avoid bridge calls

* Fix check-style errors

* Code cleanup

* Rename StartTimeModule to InitializationModule

* Remove NavigationApplication

* Documentation and minor changes

* Account for app opening after SharedExtension

* Refactor getIntl to getTranslations

* Move native module constants into it's own forked repos

* Include FetchBlob and DeviceInfo forked repos
2018-05-18 17:13:00 -04:00

118 lines
3.8 KiB
JavaScript

// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import {AppRegistry, AppState} from 'react-native';
import {NotificationsAndroid, PendingNotifications} from 'react-native-notifications';
import Notification from 'react-native-notifications/notification.android';
class PushNotification {
constructor() {
this.onRegister = null;
this.onNotification = null;
this.onReply = null;
this.deviceNotification = null;
this.deviceToken = null;
NotificationsAndroid.setRegistrationTokenUpdateListener((deviceToken) => {
this.deviceToken = deviceToken;
});
NotificationsAndroid.setNotificationReceivedListener((notification) => {
if (notification) {
const data = notification.getData();
this.handleNotification(data, false);
}
});
NotificationsAndroid.setNotificationOpenedListener((notification) => {
if (notification) {
const data = notification.getData();
this.handleNotification(data, true);
}
});
AppRegistry.registerHeadlessTask('notificationReplied', () => async (deviceNotification) => {
const notification = new Notification(deviceNotification);
const data = notification.getData();
if (this.onReply) {
this.onReply(data, data.text, parseInt(data.badge, 10) - parseInt(data.msg_count, 10));
} else {
this.deviceNotification = {
data,
text: data.text,
badge: parseInt(data.badge, 10) - parseInt(data.msg_count, 10),
};
}
});
}
handleNotification = (data, userInteraction) => {
const deviceNotification = {
data,
foreground: !userInteraction && AppState.currentState === 'active',
message: data.message,
userInfo: data.userInfo,
userInteraction,
};
if (this.onNotification) {
this.onNotification(deviceNotification);
}
};
configure(options) {
this.onRegister = options.onRegister;
this.onNotification = options.onNotification;
this.onReply = options.onReply;
if (this.onRegister && this.deviceToken) {
this.onRegister({token: this.deviceToken});
}
if (options.popInitialNotification) {
PendingNotifications.getInitialNotification().
then((notification) => {
if (notification) {
const data = notification.getData();
if (data) {
this.handleNotification(data, true);
}
}
}).
catch((err) => {
console.log('Android getInitialNotifiation() failed', err); //eslint-disable-line no-console
});
}
}
localNotificationSchedule(notification) {
if (notification.date) {
notification.fireDate = notification.date.getTime();
Reflect.deleteProperty(notification, 'date');
NotificationsAndroid.scheduleLocalNotification(notification);
}
}
localNotification(notification) {
NotificationsAndroid.localNotification(notification);
}
cancelAllLocalNotifications() {
NotificationsAndroid.cancelAllLocalNotifications();
}
setApplicationIconBadgeNumber(number) {
NotificationsAndroid.setBadgesCount(number);
}
getNotification() {
return this.deviceNotification;
}
resetNotification() {
this.deviceNotification = null;
}
}
export default new PushNotification();