【发布时间】:2018-02-15 04:49:07
【问题描述】:
注意:我是在真机上测试
我正在尝试使用 Firebase Cloud Functions 向我的 react-native 应用发送推送通知。
下面是我的云函数:
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);
});
});
上面的云函数正在执行,没有任何错误:
下面是我应用中的通知监听器:
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 值,但在执行云功能时我没有收到任何通知。有人可以告诉我我做错了什么吗?
【问题讨论】:
标签: firebase react-native redux firebase-cloud-messaging redux-saga