注意:我正在真实设备上的测试
我试图使用Firebase Cloud函数向我的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值正在记录,但是执行云功能时,我没有收到任何通知。有人可以告诉我我做错了什么吗?
首先,您将不会在模拟器上收到通知,因此请确保已通过Internet连接调试远程设备。
第二,请遵循反应 - fCM细节,有两种方法
POD方法:
确保您有Cocoapods版本> 1.0
配置项目:
cd ios && pod init
(如果是语法错误,请打开yourapp.xcodeproj/project.pbxproj并修复它们。(
编辑新创建的podfile,您可以在iOS文件夹中找到它 -> podfile将 行添加到文件
中# Pods for YOURAPP
+ pod 'Firebase/Messaging'
target 'YOURApp' do
+ pod 'react-native-fcm', :path => '../node_modules/react-native-fcm'
...
end
安装firebase/sessaging pod:
pod install
如果这种方法不起作用,则可以遵循
非cocoapod方法
- 从没有Cocoapods的集成下载Firebase SDK框架。导入库,添加功能(背景运行和推送通知(,上传APN等等等...
- 将框架放在iOS/Frameworks文件夹下
- 请按照自动化的方式链接框架(分析 消息传递(
共享步骤(这都适用于两种方法(
编辑appdelegate.h:
+ @import UserNotifications;
+
+ @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
- @interface AppDelegate : UIResponder <UIApplicationDelegate>
Edit AppDelegate.m:
+ #import "RNFIRMessaging.h"
//...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//...
+ [FIRApp configure];
+ [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
return YES;
}
+
+ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
+ {
+ [RNFIRMessaging willPresentNotification:notification withCompletionHandler:completionHandler];
+ }
+
+ #if defined(__IPHONE_11_0)
+ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
+ {
+ [RNFIRMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
+ }
+ #else
+ - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
+ {
+ [RNFIRMessaging didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
+ }
+ #endif
+
+ //You can skip this method if you don't want to use local notification
+ -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
+ [RNFIRMessaging didReceiveLocalNotification:notification];
+ }
+
+ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
+ [RNFIRMessaging didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
+ }
添加功能现在,当您想在XCode中构建应用程序时,请确保在capabilities
设置中打开推送通知,并将其与Apple Developer帐户集成在一起。然后尝试在设备上运行应用程序,并且应该运行良好。
Select your project Capabilities and enable:
Push Notifications
Background Modes > Remote notifications.
这应该使推动通知有效,它的长一步,但它完成了一次:(
您没有收到通知,这可能是因为Firebase未正确集成在您的应用程序中。正确集成的步骤如下:
反应新的 - 网络库库对我不起作用。整合firebase在接收推送通知时使用反应型FCM进行反应本机应用。
1.安装反应 - fCM: NPM安装react-native-fcm -save
- 来自Firebase控制台,对于Android:下载Google-services.json文件并将其放入Android/App目录中。对于iOS:下载googleservice-info.plist文件并将其放入/ios/yous your-project-name目录(临近您的info.plist(
3.将项目配置为:CD ios&amp;&amp;POD INIT
4.Edit新创建的Podfile:POD安装
5.Edit AppDelegate.h:
@import UserNotifications;
@interface AppDelegate:UIResponder<UIApplicationDelegate,UNUserNotificationCenterDelegate>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
6.Edit AppDelegate.M:
#import "RNFIRMessaging.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FIRApp configure];
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
return YES;
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^) .
(UNNotificationPresentationOptions))completionHandler
{
[RNFIRMessaging willPresentNotification:notification
withCompletionHandler:completionHandler];
}
#if defined(__IPHONE_11_0)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
[RNFIRMessaging didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
}
#else
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)())completionHandler
{
[RNFIRMessaging didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
}
#endif
//You can skip this method if you don't want to use local notification
-(void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
[RNFIRMessaging didReceiveLocalNotification:notification];
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo
fetchCompletionHandler:(nonnull void (^) .
(UIBackgroundFetchResult))completionHandler{
[RNFIRMessaging didReceiveRemoteNotification:userInfo
fetchCompletionHandler:completionHandler];
}
7.将标题文件的路径添加到xcode:
打开Xcode。按CMD 1或单击Xcode项目。转到构建设置,选择全部并组合。搜索标题搜索路径在搜索范围内。确保有$(srcroot(/../node_modules/react-native-native-native-fcm/ios的行。如果否,只需添加。
8. add googleservice-info.plist:
确保文件不仅移至文件夹中。您需要右键单击XCode中的项目文件夹,然后将文件添加到。选择文件时在"选项"页面中选择复制。
9.Shared库设置:
如果您使用的是POD安装方法,请确保在库文件夹下看到pods.xcodeproj。确保在库文件夹下看到rnformessing.xcodeproj。如果您看不到任何这些文件,请右键单击"库文件夹",然后将文件添加到以添加它们。pods.xcodeproj应在iOS/pods/和rnformessing.xcodeproj下,应在node_modules/react-native-native-native-fcm/ios下。确保您看到librnfirmessing.a在链接二进制中,构建阶段选项卡下的库。如果否,请将文件拖到rnformessging.xcodeproj下的库文件夹下的文件中。
10.ADD功能选择您的项目功能并启用:推送通知背景模式>远程通知。
firbaseappdelegateproxyenabs该指令假设您的FirebaseAppdelegateProxyenabled = Yes(默认(,以便firebase将在推送通知注册事件上挂钩。如果您关闭此标志,您将独自管理APNS令牌并与Firebase令牌链接。