Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for opened notifications in iOS #5835

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions packages/pushnotification/src/PushNotification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default class PushNotification {
private _androidInitialized: boolean;
private _iosInitialized: boolean;

private _notificationOpenedHandlers: Function[];

constructor(config) {
if (config) {
this.configure(config);
Expand All @@ -48,13 +50,13 @@ export default class PushNotification {
this._checkIfOpenedByNotification = this._checkIfOpenedByNotification.bind(
this
);
this.addEventListenerForIOS = this.addEventListenerForIOS.bind(this);
this._currentState = AppState.currentState;
this._androidInitialized = false;
this._iosInitialized = false;

if (Platform.OS === 'ios') {
AppState.addEventListener('change', this._checkIfOpenedByNotification);
}
this._notificationOpenedHandlers = [];

Amplify.register(this);
}

Expand Down Expand Up @@ -102,10 +104,10 @@ export default class PushNotification {

onNotificationOpened(handler) {
if (typeof handler === 'function') {
// check platform
if (Platform.OS === 'android') {
this.addEventListenerForAndroid(REMOTE_NOTIFICATION_OPENED, handler);
}
this._notificationOpenedHandlers = [
...this._notificationOpenedHandlers,
handler,
];
}
}

Expand Down Expand Up @@ -167,14 +169,18 @@ export default class PushNotification {
REMOTE_NOTIFICATION_RECEIVED,
this.handleNotificationReceived
);
this.addEventListenerForIOS(
REMOTE_NOTIFICATION_OPENED,
this.handleNotificationOpened
);
}

/**
* This function handles the React Native AppState change event
* And checks if the app was launched by a Push Notification
* @param nextAppState The next state the app is changing to as part of the event
*/
_checkIfOpenedByNotification(nextAppState) {
_checkIfOpenedByNotification(nextAppState, handler) {
// the app is turned from background to foreground
if (
this._currentState.match(/inactive|background/) &&
Expand All @@ -183,7 +189,7 @@ export default class PushNotification {
PushNotificationIOS.getInitialNotification()
.then(data => {
if (data) {
this.handleNotificationOpened(data);
handler(data);
}
})
.catch(e => {
Expand Down Expand Up @@ -272,6 +278,10 @@ export default class PushNotification {
}

handleNotificationOpened(rawMessage) {
this._notificationOpenedHandlers.forEach(handler => {
handler(rawMessage);
});

logger.debug('handleNotificationOpened, raw data', rawMessage);
const { eventSource, eventSourceAttributes } = this.parseMessageData(
rawMessage
Expand Down Expand Up @@ -371,6 +381,12 @@ export default class PushNotification {
if (event === REMOTE_NOTIFICATION_RECEIVED) {
PushNotificationIOS.addEventListener('notification', handler);
}
if (event === REMOTE_NOTIFICATION_OPENED) {
PushNotificationIOS.addEventListener('localNotification', handler);
AppState.addEventListener('change', nextAppState =>
this._checkIfOpenedByNotification(nextAppState, handler)
);
}
}

parseMessagefromAndroid(message, from?) {
Expand Down