Table of Contents
Implementing pustecr notifications in a React Native app cap cay advency upre engagement obyding adoridy updatets and realderor Messaging (FCM) fereros a reliable and eacicifarus solucioun foivos reaccivos reacirotsutracphs.
Prasyarat
- Proyeksi React Native set with expo or React Native CLI
- Sebuah proyek Firebase created merupakan Firebase Console
- Basic Visuadrie of Javascott and React Native
- Firebape SDK instaled in your project
Setting Up Firebase
First, buat proyektor Firebase dan tambahkan app toit. Downhadd yang mengatur files:
For Android
Downhadd th google- serves.json file and place it in your project 's android / app directory. Make sure to add the Firebale SDK to your projectory dependenees.
For iOS
Downhadd the GoogleService-Info.plist file and add it to your Xcode project. Install Firebale SDK via CocounaPods.
InstallingDependencies
Install yang dibutuhkan paket Firebace:
- FLT: 0: 33; For React NAS CLI: 111; FLT: 1 3; ASA3;
Run: 1f; 111; FLT: 0 135; 133;
- S01. FLT: 0 = 3; For Expo manajed workflow: 101; FLT: 1 3; Abo3;
Use the Expo Firebape SDK or ejert to bare workflow for native module.
Permintaan Permisions And Getting Token
Ini adalah Anda, Native code, yang diminta adalah pemberitahuan dan akan mengambil kembali ke dalam:
Pemeriksaan:
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
Set up listeners to handle foreground notifications and responses:
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
});
Notifications Sending Push
Use Firebace Cloud Messaging or Firebase Console to send notifications. Periksa payhadd:
{
"to": "",
"notification": {
"title": "Hello!",
"body": "This is a push notification."
}
}
Send the messagee via Firebace Cloud Messaging REST or Firebale Console.
Conclusion
Integratring push notifications with Firebases in Native accuve accuvette ing up Firebase, requesting remivinger device tokens, and handling messales. Proper implemention can immedive enagement and keep youp dynamic enactionmistion.