Wdrożenie powiadomień push w React Native używając firebase
Wdrożenie przez Komisję powiadomienia o zmianach w czasie i alarmach. Firebase Cloud Messaging (FCM) oferuje relieble i easy- to-integrate solution for management ing push notifications s across platforms. This guide walks you the steps to implement push notifications in React Native using Firebase.
Warunki wstępne
- A React Native project set up wigh Expo or React Native CLI
- A Firebase project created in the Firebase Console
- Basic knowndge of JavaScript andReact Native
- Firebase SDK installade in your project
Setting Up Firebase
First, create a Firebase project andd add your app to it. Download thee configuation files:
For Android
Download thee google- services. JSON file and place it in your project 's android / app directory. Make sure te add thee Firebase SDK to your project dependencies.
For iOS
Download thee GoogleService- Info.plist file and add it to your Xcode project. Install thee Firebase SDK via CocoaPods.
Installing Dependencies
Install thee necessary Firebase packages:
- Xi1; Xi1; FLT: 0 Xi3; Xi3; For React Native CLI: Xi1; Xi1; FLT: 1 Xi3; Xi3; Xi3;
Run: Xi1; Xi1; FLT: 0 Xi3; Xi3;
- Xi1; Xi1; FLT: 0 Xi3; Xi3; For Expo managed workflow: Xi1; Xi1; FLT: 1 Xi3; Xi3; Xi3;
Usie thee Expo Firebase SDK or eject to o bare workflow for nativie modules.
Requesting Permissions andGetting Token
Jeśli odpowiesz na pytanie Native, żądaj powiadomienia For i odzyskaj je, to musisz:
Egzamin:
import messaging from '@react-native-firebase/messaging';
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);
getFcmToken();
}
}
async function getFcmToken() {
const fcmToken = await messaging().getToken();
if (fcmToken) {
console.log('FCM Token:', fcmToken);
// Send token to your server for push notifications
}
}
requestUserPermission();
Handling Incoming Notifications
Ustawić listę osób, które zgłosiły się jako osoby niebędące członkami grupy i odpowiedzieć na to pytanie:
messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!', remoteMessage);
// Show local notification or update UI
});
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
// Handle background message
});
Sending Push Notifications
Usie Firebase Cloud Messaging API or Firebase Console to send notifications. Example payload:
{
"to": "",
"notification": {
"title": "Hello!",
"body": "This is a push notification."
}
}
Send thee message via Firebase Cloud Messaging REST API or Firebase Console.
Konkluzja
Integriting push notifications with Firebase in React Native involves setting up Firebase, requesting permissions, requesting device tokens, and handling messages. Proper implementation can improwise user engagement and keep your app dynamic and interactive.