在我使用 React-native 和 Laravel 的项目中,我在通知部分遇到了错误。虽然我可以在 Android 端接收通知,没有任何问题,但在 iOS 端,当我尝试使用相同的函数但仅更改 fcm_token 变量发送通知时,我遇到了此错误。有谁知道原因或者可以帮忙吗
Laravel 方面
function sendFirebaseNotification($to, $title, $body, $data = [])
{
try {
$factory = (new Factory)->withServiceAccount(config('firebase.credentials'));
$messaging = $factory->createMessaging();
$message = [
'token' => $to,
'notification' => [
'title' => $title,
'body' => $body,
],
'data' => $data,
];
try {
$messaging->send($message);
return true;
} catch (\Exception $e) {
Log::error('Firebase send error: ' . $e->getMessage());
return false;
}
} catch (\Exception $e) {
Log::error('Firebase configuration error: ' . $e->getMessage());
return false;
}
}
public function sendNotification()
{
$fcm_token = "eO38TzkHz0LqrfU31zWzI_:APA91bGh4PFzGRDsbFfeA5k1VRdiasy9z3sljxZkI81HXDyZM4egqFvZkPwb0Uwedya3jUsJ_s4bgYq_zMNghF966ihzyU54j1YSVDNdg4V9_TM8mPJS-zAl5mRcjckrx2aJfmfgFcnZ";
$title = "title";
$body = "body";
$data = [
'key' => 'value',
];
$response = $this->sendFirebaseNotification($fcm_token, $title, $body, $data);
Log::info($response);
if ($response) {
return response()->json(['success' => 'Notification sent successfully']);
} else {
return response()->json(['error' => 'Notification not sent'], 500);
}
return response()->json(['success' => 'Notification sent successfully']);
}
React-native
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import PushNotification from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import firebase from '@react-native-firebase/app';
import {
checkInitialNotification,
onBackgroundMessageHandler,
onMessageListener,
onNotificationOpenedApp,
registerAppWithFCM,
} from './src/messagingHandler';
import messaging from '@react-native-firebase/messaging';
AppRegistry.registerComponent(appName, () => App);
const firebaseConfig = {
apiKey: '**********',
authDomain: '********',
databaseURL: '**********',
projectId: '***********',
storageBucket: '**********',
messagingSenderId: '*********',
appId: '***********',
measurementId: '**********',
};
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
PushNotification.createChannel({
channelId: 'fcm_fallback_notification_channel',
channelName: 'Default Channel',
});
PushNotification.configure({
onNotification: function (notification) {
console.log('NOTIFICATION:', notification);
if (notification.userInteraction) {
navigationRef.current?.navigate(notification.data.stack.toString(), {
screen: notification.data.screen,
params: notification.data.params,
});
}
},
popInitialNotification: true,
requestPermissions: true,
});
AppRegistry.registerComponent(appName, () => App);
registerAppWithFCM();
onMessageListener(remoteMessage => {
console.log('Foreground message received:', remoteMessage);
PushNotification.localNotification({
channelId: 'fcm_fallback_notification_channel',
title: remoteMessage.notification.title,
message: remoteMessage.notification.body,
data: remoteMessage.data,
});
});
onNotificationOpenedApp();
checkInitialNotification();
onBackgroundMessageHandler();
我尝试了很多不同的方法,但没有一个有效。在 Laravel 方面,我在日志中收到此错误。
根据官方文档 https://firebase.google.com/docs/cloud-messaging/send-message,您必须使用
to
作为接收者设备令牌的密钥标识符,而在示例中您使用的是 token
代替。
注意: 不要在您的问题或任何公共论坛中发布机密数据,例如 FCM 令牌或密码,您可以用 3 个星号标记代替。