Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function * getInitialNotification () {
try {
// console.log('getInitialNotification')
const message = yield call([firebase.notifications(), 'getInitialNotification'])
if (!message) return
yield handleMessage(message.notification.data)
} catch (error) {
console.log(error)
}
}
return async (dispatch: Function, getState: Function) => { // eslint-disable-line
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
checkForSupportAlert(notificationOpen.notification._data);
const { type, navigationParams } = processNotification(notificationOpen.notification._data) || {};
const notificationRoute = NOTIFICATION_ROUTES[type] || null;
updateNavigationLastScreenState({
lastActiveScreen: notificationRoute,
lastActiveScreenParams: navigationParams,
});
firebase.notifications().setBadge(0);
}
if (notificationsOpenerListener) return;
notificationsOpenerListener = firebase.notifications().onNotificationOpened((message) => {
checkForSupportAlert(message.notification._data);
firebase.notifications().setBadge(0);
const pathAndParams = getNavigationPathAndParamsState();
if (!pathAndParams) return;
async componentDidMount(){
// Build a channel
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);
registerAppListener(this.props.navigation);
firebase.notifications().getInitialNotification()
.then((notificationOpen: NotificationOpen) => {
if (notificationOpen) {
// Get information about the notification that was opened
const notif: Notification = notificationOpen.notification;
this.setState({
initNotif: notif.data
})
if(notif && notif.targetScreen === 'detail'){
setTimeout(()=>{
this.props.navigation.navigate('Detail')
}, 500)
}
}
createNotificationChannel = () => {
// Build a android notification channel
const channel = new firebase.notifications.Android.Channel(
'reminder',
'Reminders Channel',
firebase.notifications.Android.Importance.High
).setDescription('Used for getting reminder notification');
// Create the android notification channel
firebase.notifications().android.createChannel(channel);
};
console.log('PushManager: FCM: notification: ' + message.data);
LoginManager.getInstance().pushNotificationReceived(message.data);
});
firebase.messaging().getToken()
.then(token => {
console.log(token);
this.pushToken = token;
})
.catch(() => {
console.warn('PushManager android: failed to get FCM token');
});
const channel = new firebase.notifications.Android.Channel('voximplant_channel_id', 'Incoming call channel', firebase.notifications.Android.Importance.Max)
.setDescription('Incoming call received');
firebase.notifications().android.createChannel(channel);
} catch (e) {
console.warn('React Native Firebase is not set up. Enable google-services plugin at the bottom of the build.gradle file');
}
}
.setBody(customNotification.body)
.setData(message.data)
.android.setChannelId('main')
.android.setSmallIcon(customNotification.icon)
firebase.notifications().displayNotification(notification)
return Promise.resolve()
}
const channel = new firebase.notifications.Android.Channel(
'main',
'Main Channel',
firebase.notifications.Android.Importance.Max,
).setDescription('Main channel')
firebase.notifications().android.createChannel(channel)
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging)
const bgMessaging = async message => {
const customNotification = JSON.parse(message.data.custom_notification)
const notification = new firebase.notifications.Notification()
.setNotificationId(message.messageId)
.setTitle(customNotification.title)
.setBody(customNotification.body)
.setData(message.data)
.android.setChannelId('main')
.android.setSmallIcon(customNotification.icon)
firebase.notifications().displayNotification(notification)
return Promise.resolve()
}
scheduleLocalNotification() {
let notification = new firebase.notifications.Notification();
notification = notification.setNotificationId(new Date().valueOf().toString())
.setTitle( "Test Notification with action")
.setBody("Force touch to reply")
.setSound("bell.mp3")
.setData({
now: new Date().toISOString()
});
notification.android.setChannelId("test-channel")
notification.android.setPriority(firebase.notifications.Android.Priority.High)
notification.android.setSmallIcon("ic_launcher")
firebase.notifications().scheduleNotification(notification, { fireDate: new Date().getTime() + 5000 })
}
this.notificationListener = firebase.notifications().onNotification(notification => {
const route = Navigation.getActiveRouteName();
if (route === 'ConfirmRequest') {
const localNotification = new firebase.notifications.Notification()
.setTitle(notification.title)
.setBody(notification.body)
.setData({ ...notification.data, fromLocal: true });
firebase.notifications().displayNotification(localNotification);
} else {
const topic = get(notification, 'data.topic');
this.onPushNotificationOpened(topic, true);
}
});
if (user && user.id && loggedIn) {
this.onTokenRefreshListener = firebase.messaging().onTokenRefresh((fcmToken) => {
storeAppToken(fcmToken, getDeviceId());
});
firebase.messaging().getToken().then(appToken => storeAppToken(appToken, getDeviceId()));
}
const channel = new firebase.notifications.Android.Channel(
'skjuts-channel', 'Skjutsgruppen Channel', firebase.notifications.Android.Importance.Max,
)
.setDescription('Skjutsgruppen app channel');
firebase.notifications().removeAllDeliveredNotifications();
firebase.notifications().android.createChannel(channel);
this.notificationListener = firebase.notifications().onNotification((notification) => {
this.showLocalNotification(notification);
});
this.messageListener = firebase.messaging().onMessage((message) => {
const { _data: { custom_notification: customNotification } } = message;
this.scheduleLocalNotification(customNotification);
});
}