Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async requestPermission() {
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log('permission rejected');
}
}
return async (dispatch: Function, getState: Function) => {
const {
wallet: { data: wallet },
assets: { data: assets },
contacts: { data: contacts },
} = getState();
let enabled = await firebase.messaging().hasPermission();
if (!enabled) {
try {
await firebase.messaging().requestPermission();
await firebase.messaging().getToken();
enabled = true;
} catch (err) { } // eslint-disable-line
}
if (!enabled) {
dispatch(fetchAllNotificationsAction());
disabledPushNotificationsListener = setInterval(() => {
dispatch(fetchAllNotificationsAction());
}, 30000);
return;
}
if (notificationsListener) return;
notificationsListener = firebase.notifications().onNotification(debounce(message => {
if (!message._data || !Object.keys(message._data).length) return;
init() {
try {
firebase.messaging().onTokenRefresh((token) => {
console.log('Refresh token: ' + token);
});
firebase.messaging().onMessage(async (message) => {
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);
init() {
try {
firebase.messaging().onTokenRefresh((token) => {
console.log('Refresh token: ' + token);
});
firebase.messaging().onMessage(async (message) => {
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');
}
}
async componentDidMount() {
const { storeAppToken, user, loggedIn } = this.props;
try {
await firebase.messaging().requestPermission();
} catch (error) {
console.warn(error);
}
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);
});
function * registerDeviceForNotificationsAndroid () {
yield put(startWorking('push'))
try {
console.log('Registering FCM')
yield put(saveMessage('push', 'registering'))
const deviceToken = yield call([firebase.messaging(), 'getToken'])
console.log(`FCM deviceToken: ${deviceToken}`)
if (deviceToken) {
yield receiveDeviceToken({deviceToken})
} else {
yield put(skipPushNotifications())
}
} catch (error) {
console.log(error)
}
yield put(stopWorking('push'))
}
firebase.notifications().displayNotification(notification);
})
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
const notif: Notification = notificationOpen.notification;
if(notif.data.targetScreen === 'detail'){
setTimeout(()=>{
navigation.navigate('Detail')
}, 500)
}
setTimeout(()=>{
alert(`User tapped notification\n${notif.notificationId}`)
}, 500)
});
this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(token => {
console.log("TOKEN (refreshUnsubscribe)", token);
});
this.messageListener = firebase.messaging().onMessage((message: RemoteMessage) => {
displayNotificationFromCustomData(message);
});
}
export const registerTokenRefreshListener = () => firebase.messaging().onTokenRefresh(fcmToken => {
saveLocal('rainbowFcmToken', { data: fcmToken });
});
getNotificationToken = () => {
firebase.messaging().getToken()
.then((fcmToken) => {
if (fcmToken) {
/** Subscribe to topics **/
this.props.refreshSubscriptions()
}
})
}
function* unregisterToken(action) {
const { accessToken } = action
const fcmToken = yield firebase.messaging().getToken()
try {
yield put({ type: 'POST_TOKEN_REQUEST' })
const { response, timeout } = yield race({
response: call(MessagesService.DeletePushToken, fcmToken, accessToken),
timeout: call(delay, MESSAGING_TTL)
})
if (timeout) {
const e = new Error('Request timed out.')
throw e
} else if (response) {
yield put({ type: 'CONFIRM_DEREGISTRATION' })
yield put({ type: 'POST_TOKEN_SUCCESS' })
}