我做了以下更改:
如何使用 firebase-admin 发送通知并获得成功结果:
admin.messaging().sendToDevice(
"token as string",
{
notification: {
title: 'title here',
body: 'body here'
}
},
{
priority: "high",
contentAvailable: true,
timeToLive: 2419200
});
React-native 代码,这是应用程序位于前台时的情况:
useEffect(() => {
checkToken().then((token) => {
console.log(token);
// write to db
});
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
console.log(remoteMessage);
});
return unsubscribe;
}, []);
我在ios文件夹中有GoogleService-Info.plist文件,我已经在firebase控制台+ teamid + appid中上传了APN。
AppDelegate.m 中的以下更改:
#import <Firebase.h>
....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[FIRApp configure];
当应用程序在前台运行时,我没有收到任何通知,我发送的方式一定有问题(可能我有错误的选项),或者我在react-native中的配置错误,不确定。我目前正在尝试仅针对 IOS 进行此操作。
任何信息或解决方案都会有帮助。
当应用程序位于前台时,React 本机 firebase 不会显示通知,所以我所做的是,我使用不同的库来处理前台通知。我正在使用 react-native-push-notification。\
适用于安卓
import PushNotification from 'react-native-push-notification';
import PushNotificationIos from '@react-native-community/push-notification-ios';
useEffect(() => {
checkToken().then((token) => {
console.log(token);
// write to db
});
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
PushNotification.localNotification({
message: remoteMessage.notification.body,
title: remoteMessage.notification.title,
bigPictureUrl: remoteMessage.notification.android.imageUrl,
smallIcon: remoteMessage.notification.android.imageUrl,
});
});
return unsubscribe;
}, []);
对于 iOS:
对于 iOS,您必须安装 @react-native-community/push-notification-ios
另请遵循文档中建议的所有本机安装步骤。
然后编写以下代码
PushNotification.configure({
onNotification: (notification) => {
if (notification) {
console.log(notification);
}
},
});
useEffect(() => {
// To display notification when app is in foreground
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
PushNotificationIos.addNotificationRequest({
message: remoteMessage.notification.body,
title: remoteMessage.notification.title,
bigPictureUrl: remoteMessage.notification.android.imageUrl,
smallIcon: remoteMessage.notification.android.imageUrl,
});
});
return unsubscribe;
}, []);
首先你需要获得消息发送权限
async function requestUserPermission() {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
}
}
然后你需要创建一个频道:
const createFCMChannel = async () => {
const channelId = await notifee.createChannel({
id: 'default',
name: 'Default Channel',
});
};
您只需调用此方法即可从notifee调用“displayNotification”
function onMessageReceived(message) {
notifee.displayNotification(message.notification);
}
您的应用程序启动时,您可以在 useEffect 中使用这些方法:
useEffect(() => {
createFCMChannel();
}, []);
useEffect(() => {
requestUserPermission();
messaging().onMessage(onMessageReceived);
messaging().setBackgroundMessageHandler(onMessageReceived);
}, []);
您可以直接从AppDelegate文件控制和打印通知数据。当您绕过应用程序逻辑并在第一个传入门处使用数据时,它允许更快的调试过程。这样,您将找出问题所在 - 服务器端(通知数据结构等)或应用程序逻辑/本地处理过程。 只需使用 Appdelegate.m 文件中的示例代码部分即可:
Messaging.messaging().appDidReceiveMessage(userInfo)
With swizzling disabled you must let Messaging know about the message, for Analytics
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
FIRMessaging.messaging().appDidReceiveMessage(userInfo);
Messaging.messaging().delegate = self
completionHandler(.NoData)
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
...
// Print full message.
NSLog(@"%@", userInfo);
根据经验,请从 Apple 开发者页面查看有关结构和参数的信息:https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application
有几个方法可以解决这个问题
我最终通过在实际设备上运行干净的运行来修复它。