-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at a push notification container
- Loading branch information
1 parent
a45fa26
commit 306336f
Showing
1 changed file
with
129 additions
and
0 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,129 @@ | ||
import React from 'react'; | ||
import { | ||
Alert, | ||
Platform, | ||
} from 'react-native'; | ||
|
||
import { connect } from 'react-redux'; | ||
import FCM, { | ||
FCMEvent, | ||
} from 'react-native-fcm'; | ||
import Permissions from 'react-native-permissions'; | ||
|
||
class PushNotificationContainer extends React.Component { | ||
componentDidMount() { | ||
this.checkPermission(); | ||
|
||
// if we launch with a notification waiting for us | ||
FCM.getInitialNotification().then((notif) => { | ||
console.log('launched w/notification waiting for us', notif); | ||
}); | ||
|
||
this.notificationListener = FCM.on(FCMEvent.Notification, (notif) => { | ||
if (!notif) return; // make sure we get something | ||
|
||
console.log('got notification', notif); | ||
|
||
// there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload | ||
if (notif.local_notification) { | ||
// this is a local notification | ||
return; | ||
} | ||
if (notif.opened_from_tray) { | ||
// app is open/resumed because user clicked banner | ||
return; | ||
} | ||
|
||
// if app is already open, show a local notification with the same info | ||
this.showLocalNotification(notif); | ||
}); | ||
this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => { | ||
this.updateServerToken(token); | ||
// fcm token may not be available on first load, catch it here | ||
}); | ||
} | ||
|
||
componentWillUnmount() { | ||
// stop listening for events | ||
this.notificationListener.remove(); | ||
this.refreshTokenListener.remove(); | ||
} | ||
|
||
getNotificationToken = () => { | ||
FCM.getFCMToken().then((token) => { | ||
this.updateServerToken(token); | ||
}); | ||
} | ||
|
||
getSoftPermission = () => { | ||
Alert.alert( | ||
'Allow this app to send you notifications?', | ||
'We need access so we can keep you informed.', | ||
[ | ||
{ text: 'No', onPress: () => {} }, | ||
{ text: 'Yes', onPress: this.getPermission } | ||
] | ||
); | ||
} | ||
|
||
getPermission = () => { | ||
Permissions.requestPermission('notification') | ||
.then((response) => { | ||
// if authorized, act | ||
if (response === 'authorized') { | ||
this.getNotificationToken(); | ||
} | ||
}) | ||
.catch(logger.error); | ||
} | ||
|
||
showLocalNotification = (notif) => { | ||
// NOTE: will not work until we configure local notifications | ||
if (notif && notif.notification) { | ||
FCM.presentLocalNotification({ | ||
title: notif.notification.title, | ||
body: notif.notification.body, | ||
priority: 'high', | ||
click_action: notif.notification.click_action, | ||
show_in_foreground: true, | ||
local: true | ||
}); | ||
} | ||
} | ||
|
||
checkPermission = () => { | ||
if (Platform.OS === 'ios') { | ||
// check for permission and either ask for it | ||
// or if we already have it then update the device tokens | ||
Permissions.getPermissionStatus('notification') | ||
.then((response) => { | ||
if (response === 'undetermined') { | ||
this.getPermission(); | ||
} else if (response === 'authorized') { | ||
this.getNotificationToken(); | ||
} | ||
}); | ||
} else { | ||
// android always has permissions, just go get the notification token | ||
this.getNotificationToken(); | ||
} | ||
} | ||
|
||
// Set device information along with app push ID token | ||
updateServerToken = (token) => { | ||
console.log('push token', token); | ||
|
||
// TODO: send token to server so we can push notifications here | ||
} | ||
|
||
render() { | ||
return null; // TODO: render error message if user does not allow location | ||
} | ||
} | ||
|
||
function mapStateToProps(state, props) { | ||
return { | ||
}; | ||
} | ||
|
||
module.exports = connect(mapStateToProps)(PushNotificationContainer); |