Created
July 23, 2019 00:06
-
-
Save johanguse/7bde0ae9e21c0083e679a413b18a78d7 to your computer and use it in GitHub Desktop.
Test for Firebase Push Notification
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
/** | |
* Sample React Native App | |
* https://github.com/facebook/react-native | |
* | |
* @format | |
* @flow | |
*/ | |
import React, { Component } from 'react'; | |
import { Platform, StyleSheet, Text, View } from 'react-native'; | |
import firebase from 'react-native-firebase'; | |
import type { Notification, NotificationOpen } from 'react-native-firebase'; | |
const instructions = Platform.select({ | |
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu', | |
android: | |
'Double tap R on your keyboard to reload,\n' + | |
'Shake or press menu button for dev menu', | |
}); | |
firebase.messaging().getToken() | |
.then(fcmToken => { | |
if (fcmToken) { | |
// user has a device token | |
console.log(fcmToken); | |
} else { | |
// user doesn't have a device token yet | |
console.log('No token'); | |
} | |
}); | |
export default class AppAndroid extends Component { | |
componentDidMount = async () => { | |
const enabled = await firebase.messaging().hasPermission(); | |
if (enabled) { | |
// user has permissions | |
} else { | |
// user doesn't have permission | |
try { | |
await firebase.messaging().requestPermission(); | |
// User has authorised | |
} catch (error) { | |
// User has rejected permissions | |
alert('No permission for notification'); | |
} | |
} | |
const notificationOpen: NotificationOpen = await firebase.notifications().getInitialNotification(); | |
if (notificationOpen) { | |
// App was opened by a notification | |
// Get the action triggered by the notification being opened | |
const action = notificationOpen.action; | |
// Get information about the notification that was opened | |
const notification: Notification = notificationOpen.notification; | |
if (notification.body!==undefined) { | |
alert(notification.body); | |
} else { | |
var seen = []; | |
alert(JSON.stringify(notification.data, function(key, val) { | |
if (val != null && typeof val == "object") { | |
if (seen.indexOf(val) >= 0) { | |
return; | |
} | |
seen.push(val); | |
} | |
return val; | |
})); | |
} | |
firebase.notifications().removeDeliveredNotification(notification.notificationId); | |
} | |
const channel = new firebase.notifications.Android.Channel('test-channel', 'Test Channel', firebase.notifications.Android.Importance.Max) | |
.setDescription('My apps test channel'); | |
// Create the channel | |
firebase.notifications().android.createChannel(channel); | |
firebase.messaging().subscribeToTopic('news1'); | |
this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => { | |
// Process your notification as required | |
// ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification. | |
}); | |
this.notificationListener = firebase.notifications().onNotification((notification: Notification) => { | |
// Process your notification as required | |
console.log('get Message'); | |
console.log(notification); | |
notification | |
.android.setChannelId('test-channel') | |
.android.setSmallIcon('ic_launcher'); | |
firebase.notifications() | |
.displayNotification(notification); | |
if (Platform.OS === 'android') { | |
const localNotification = new firebase.notifications.Notification({ | |
sound: 'default', | |
show_in_foreground: true, | |
show_in_background: true | |
}) | |
.setNotificationId(notification.notificationId) | |
.setTitle(notification.title) | |
.setSubtitle(notification.subtitle) | |
.setBody(notification.body) | |
.setData(notification.data) | |
.android.setChannelId('test-channel') // e.g. the id you chose above | |
.android.setSmallIcon('ic_launcher') // create this icon in Android Studio | |
.android.setColor('#000000') // you can set a color here | |
.android.setPriority(firebase.notifications.Android.Priority.High); | |
firebase.notifications() | |
.displayNotification(localNotification) | |
.catch(err => console.error(err)); | |
} else if (Platform.OS === 'ios') { | |
const localNotification = new firebase.notifications.Notification() | |
.setNotificationId(notification.notificationId) | |
.setTitle(notification.title) | |
.setSubtitle(notification.subtitle) | |
.setBody(notification.body) | |
.setData(notification.data) | |
.ios.setBadge(notification.ios.badge); | |
firebase.notifications() | |
.displayNotification(localNotification) | |
.catch(err => console.error(err)); | |
} | |
}); thisotificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => { | |
// Get the action triggered by the notification being opened | |
const action = notificationOpen.action; | |
// Get information about the notification that was opened | |
const notification = new firebase.notifications.Notification({ | |
sound: 'default', | |
show_in_foreground: true, | |
show_in_background: true | |
}) | |
.setNotificationId(notificationOpen.notification.notificationId) | |
.setTitle(notificationOpen.notification.title) | |
.setSubtitle(notificationOpen.notification.subtitle) | |
.setBody(notificationOpen.notification.body) | |
.setData(notificationOpen.notification.data) | |
.android.setChannelId('test-channel') // e.g. the id you chose above | |
.android.setSmallIcon('ic_launcher') // create this icon in Android Studio | |
.android.setColor('#000000') // you can set a color here | |
.android.setPriority(firebase.notifications.Android.Priority.High); | |
if (notification.body!==undefined) { | |
alert(notification.body); | |
var seen = []; | |
alert(JSON.stringify(notification.data, function(key, val) { | |
if (val != null && typeof val == "object") { | |
if (seen.indexOf(val) >= 0) { | |
return; | |
} | |
seen.push(val); | |
} | |
return val; | |
})); | |
} else { | |
var seen = []; | |
alert(JSON.stringify(notification.data, function(key, val) { | |
if (val != null && typeof val == "object") { | |
if (seen.indexOf(val) >= 0) { | |
return; | |
} | |
seen.push(val); | |
} | |
return val; | |
})); | |
} | |
firebase.notifications().removeDeliveredNotification(notification.notificationId); | |
}); | |
} | |
componentWillUnmount() { | |
this.notificationDisplayedListener(); | |
this.notificationListener(); | |
this.notificationOpenedListener(); | |
} | |
render() { | |
return ( | |
<View style={styles.container}> | |
<Text style={styles.welcome}>Welcome to React Native Push!</Text> | |
<Text style={styles.instructions}>To get started, edit App.js</Text> | |
<Text style={styles.instructions}>{instructions}</Text> | |
</View> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: 'center', | |
alignItems: 'center', | |
backgroundColor: '#F5FCFF', | |
}, | |
welcome: { | |
fontSize: 20, | |
textAlign: 'center', | |
margin: 10, | |
}, | |
instructions: { | |
textAlign: 'center', | |
color: '#333333', | |
marginBottom: 5, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment