Note: I am testings on a real device
I am trying to send push notifications to my react-native app using Firebase Cloud Functions.
Below is my Cloud Function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNewMessageNotification = functions.database.ref('users').onWrite(event => {
const getValuePromise = admin.database()
.ref('users')
.orderByKey()
.limitToLast(1)
.once('value');
return getValuePromise.then(snapshot => {
const payload = {
notification: {
title: 'Dot notification',
body: 'A new user has been added',
}
};
return admin.messaging()
.sendToTopic('secret-chatroom', payload);
});
});
The above cloud function is being executed without any error:
Below is my Notification listener in my app:
import * as Type from '../actions/types';
import FCM, { FCMEvent,
NotificationType,
WillPresentNotificationResult,
RemoteNotificationResult } from 'react-native-fcm';
import { Platform } from 'react-native';
import { takeLatest, put, call } from 'redux-saga/effects';
function* listenToNotifications() {
FCM.requestPermissions();
FCM.getFCMToken()
.then(token => {
console.log(token) //being logged
});
FCM.subscribeToTopic('secret-chatroom');
FCM.on(FCMEvent.Notification, async (notif) => {
console.log(notif); //not being logged
alert('Notification recieved');
if (Platform.OS === 'ios') {
switch (notif._notificationType) {
case NotificationType.Remote:
notif.finish(RemoteNotificationResult.NewData); //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
break;
case NotificationType.NotificationResponse:
notif.finish();
break;
case NotificationType.WillPresent:
notif.finish(WillPresentNotificationResult.All); //other types available: WillPresentNotificationResult.None
break;
}
}
});
FCM.on(FCMEvent.RefreshToken, token => {
console.log(token);
});
}
export default function* appNotificationsSaga() {
yield takeLatest(Type.LISTEN_TO_NOTIFICATIONS, listenToNotifications);
}
FCM.getFCMToken value is being logged, but I am not receiving any notifications when cloud function is executed. Can someone please tell what I am doing wrong?