在移动应用开发中,通知是一个非常重要的功能,它允许应用向用户发送消息或提醒,以保持用户的参与度。React Native 提供了一些库和方法来实现通知功能,使得开发者能够轻松地集成通知到他们的应用中。
1. 本地通知
本地通知是应用在设备上预先设置的通知,不需要网络连接。React Native 社区提供了一些库来处理本地通知,比如 react-native-push-notification 和 react-native-notifications。这里我们主要介绍 react-native-push-notification 库的使用。
安装库
首先,你需要安装 react-native-push-notification 库:
npm install react-native-push-notification --save
然后,根据你的平台(iOS 或 Android),按照文档中的说明进行相应的设置。
配置库
在应用启动时配置通知服务。你可以创建一个文件,如 notification.js,并在其中初始化通知服务:
// www.javascriptcn.com code example
import PushNotification from "react-native-push-notification";
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: function(token) {
console.log("TOKEN:", token);
},
// (required) Called when a remote or local notification is opened or received
onNotification: function(notification) {
console.log("NOTIFICATION:", notification);
// (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
// (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
onRegistrationError: function(err) {
console.error(err.message, err);
},
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
* - if you are not using remote notification or do not have Firebase installed, use this:
* requestPermissions: Platform.OS === 'ios'
*/
requestPermissions: true,
});
// (optional) Called when Token is generated (iOS and Android)
PushNotification.onRegister(function (token) {
console.log("TOKEN:", token);
});发送本地通知
现在你可以使用 PushNotification.localNotification() 方法来发送本地通知了。例如:
PushNotification.localNotification({
title: "My Notification",
message: "Hello World", // (required)
playSound: true,
soundName: 'default',
largeIcon: 'ic_launcher', // (optional) Icon for your notification large image size
smallIcon: 'ic_notification', // (optional) Icon for your notification that is small image size
});2. 远程通知
远程通知需要服务器端的支持,并且需要与推送服务提供商(如 Apple 的 APNs 或 Google 的 FCM)进行交互。为了接收远程通知,你需要注册应用以接收推送通知。
在 iOS 上启用推送通知
- 打开 Xcode 并选择你的项目。
- 选择你的目标,然后进入 "Signing & Capabilities" 选项卡。
- 点击 "+" 按钮添加一个新的能力,并选择 "Push Notifications"。
- 在项目的 entitlements 文件中添加
aps-environment属性,其值应为development或production。
在 Android 上启用推送通知
对于 Android,你需要在 Firebase 控制台创建一个项目,并将应用与该 Firebase 项目关联起来。这通常涉及到在应用的 google-services.json 文件中添加 Firebase 配置信息。
使用 FCM 接收远程通知
一旦配置好推送服务,你就可以开始接收和处理远程通知了。你需要在应用中初始化 FCM,并监听通知事件。下面是一个简单的示例:
// www.javascriptcn.com code example
import messaging from '@react-native-firebase/messaging';
messaging().getToken().then((fcmToken) => {
console.log('FCM Token:', fcmToken);
});
// 前台接收远程通知
messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!', JSON.stringify(remoteMessage));
});
// 后台接收远程通知
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', JSON.stringify(remoteMessage));
});通过以上步骤,你就可以在 React Native 应用中实现本地和远程通知的功能了。希望这些内容能帮助你在项目中成功地集成通知功能!