推送通知设置
- 作者仓库星标 245
- 作者仓库 useful-ai-prompts
- 领域
- 通用
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @aj-geddes · 未声明许可证
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 即装即用
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- 未声明(默认跨平台)
- 运行环境要求
- 未检出明确要求
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
import messaging from "@react-native-firebase/messaging";
import { Platform } from "react-native";
export async function initializeFirebase() {
try {
if (Platform.OS === "ios") {
const permission = await messaging().requestPermission();
if (permission === messaging.AuthorizationStatus.AUTHORIZED) {
console.log("iOS notification permission granted");
}
}
const token = await messaging().getToken();
console.log("FCM Token:", token);
await saveTokenToBackend(token);
messaging().onTokenRefresh(async (newToken) => {
await saveTokenToBackend(newToken);
});
messaging().onMessage(async (remoteMessage) => {
console.log("Notification received:", remoteMessage);
showLocalNotification(remoteMessage);
});
// ... (see reference guides for full implementation)
# Push Notification Setup
## Table of Contents
- [Overview](#overview)
- [When to Use](#when-to-use)
- [Quick Start](#quick-start)
- [Reference Guides](#reference-guides)
- [Best Practices](#best-practices)
## Overview
Implement comprehensive push notification systems for iOS and Android applications using Firebase Cloud Messaging and native platform services.
## When to Use
- Sending real-time notifications to users
- Implementing user engagement features
- Deep linking from notifications to specific screens
- Handling silent/background notifications
- Tracking notification analytics
## Quick Start
Minimal working example:
```javascript
import messaging from "@react-native-firebase/messaging";
import { Platform } from "react-native";
export async function initializeFirebase() {
try {
if (Platform.OS === "ios") {
const permission = await messaging().requestPermission();
if (permission === messaging.AuthorizationStatus.AUTHORIZED) {
console.log("iOS notification permission granted");
}
}
const token = await messaging().getToken();
console.log("FCM Token:", token);
await saveTokenToBackend(token);
messaging().onTokenRefresh(async (newToken) => {
await saveTokenToBackend(newToken);
});
messaging().onMessage(async (remoteMessage) => {
console.log("Notification received:", remoteMessage);
showLocalNotification(remoteMessage);
});
// ... (see reference guides for full implementation)
```
## Reference Guides
Detailed implementations in the `references/` directory:
| Guide | Contents |
|---|---|
| [Firebase Cloud Messaging Setup](references/firebase-cloud-messaging-setup.md) | Firebase Cloud Messaging Setup |
| [iOS Native Setup with Swift](references/ios-native-setup-with-swift.md) | iOS Native Setup with Swift |
| [Android Setup with Kotlin](references/android-setup-with-kotlin.md) | Android Setup with Kotlin |
| [Flutter Implementation](references/flutter-implementation.md) | Flutter Implementation |
## Best Practices
### ✅ DO
- Request permission before sending notifications
- Implement token refresh handling
- Use different notification channels by priority
- Validate tokens regularly
- Track notification delivery
- Implement deep linking
- Handle notifications in all app states
- Use silent notifications for data sync
- Store tokens securely on backend
- Provide user notification preferences
- Test on real devices
### ❌ DON'T
- Send excessive notifications
- Send without permission
- Store tokens insecurely
- Ignore notification failures
- Send sensitive data in payload
- Use notifications for spam
- Forget to handle background notifications
- Make blocking calls in handlers
- Send duplicate notifications
- Ignore user preferences
章节与文件索引
按说明文件中的顺序排列。
目录
何时使用
参考指南
✅ 做
❌不要
文件/命令 -> references/ · silent/background · react-native-firebase/messaging · references/firebase-cloud-messaging-setup.md · references/ios-native-setup-with-swift.md · references/android-setup-with-kotlin.md · references/flutter-implementation.md
内容校验值 -> 27c377018185
当前原文的自动摘译,尚未人工审校。
目录
概述 何时使用 快速入门 参考指南 最佳实践
何时使用
向用户发送实时通知 实施用户参与功能 从通知到特定屏幕的深度链接 处理静默/后台通知 跟踪通知分析
参考指南
详细实现见
references/目录: 指南 · 目录 Firebase 云消息传递设置 · Firebase 云消息传递设置 使用 Swift 进行 iOS 原生设置 · 使用 Swift 进行 iOS 原生设置 使用 Kotlin 进行 Android 设置 · 使用 Kotlin 进行 Android 设置✅ 做
发送通知前请求许可 实施令牌刷新处理 按优先级使用不同的通知渠道 定期验证令牌 跟踪通知传递
❌不要
发送过多通知 未经许可发送 不安全地存储令牌 忽略通知失败 在负载中发送敏感数据