How to use the react-native-firebase.messaging function in react-native-firebase

To help you get started, we’ve selected a few react-native-firebase examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github natuanorg / react-native-starter-kit / App.js View on Github external
async requestPermission() {
        try {
            await firebase.messaging().requestPermission();
            // User has authorised
            this.getToken();
        } catch (error) {
            // User has rejected permissions
            console.log('permission rejected');
        }
    }
github pillarwallet / pillarwallet / src / actions / notificationsActions.js View on Github external
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;
github voximplant / react-native-demo / src / manager / PushManager.android.js View on Github external
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);
github voximplant / react-native-demo / src / manager / PushManager.android.js View on Github external
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');
        }

    }
github Skjutsgruppen / skjutsgruppen-reactnative / app / services / firebase / pushNotification.js View on Github external
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);
    });
github uport-project / uport-mobile / lib / sagas / notifications.js View on Github external
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'))
}
github evollu / react-native-fcm / Examples / firebase-migration / app / Listeners.js View on Github external
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);
  });

}
github rainbow-me / rainbow / src / model / firebase.js View on Github external
export const registerTokenRefreshListener = () => firebase.messaging().onTokenRefresh(fcmToken => {
  saveLocal('rainbowFcmToken', { data: fcmToken });
});
github UCSD / campus-mobile / app / containers / pushNotificationContainer.js View on Github external
getNotificationToken = () => {
		firebase.messaging().getToken()
			.then((fcmToken) => {
				if (fcmToken) {
					/** Subscribe to topics **/
					this.props.refreshSubscriptions()
				}
			})
	}
github UCSD / campus-mobile / app / sagas / messagesSaga.js View on Github external
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' })
		}