mcpField
P
push-notification-setup

push-notification-setup

github.com/secondsky开发242427 次安装5 周前更新
代码仓库

关于 push-notification-setup

为移动端和 Web 开发者提供推送通知实现指导,涵盖 React Native、iOS 原生 Swift 和 Android Kotlin 的权限、令牌与消息处理。

主要功能

  1. 01FCM 推送设置
  2. 02iOS 原生授权与注册
  3. 03Android 消息接收处理
  4. 04令牌刷新与通知渠道建议
  5. 05深度链接及负载安全建议

适用场景

  1. 01为 React Native 应用申请通知权限并上报令牌
  2. 02在 iOS 应用注册远程通知
  3. 03在 Android 服务中处理推送消息

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/secondsky/claude-skills --skill push-notification-setup

安装后在 Claude Code 中输入/push-notification-setup确认已加载。

详细介绍

推送通知设置

在移动端和 Web 平台上实现推送通知。

Firebase Cloud Messaging(React Native)

import messaging from '@react-native-firebase/messaging';

// Request permission
async function requestPermission() {
  const status = await messaging().requestPermission();
  if (status === messaging.AuthorizationStatus.AUTHORIZED) {
    const token = await messaging().getToken();
    await sendTokenToServer(token);
  }
}

// Handle foreground messages
messaging().onMessage(async message => {
  console.log('Foreground message:', message);
  showLocalNotification(message);
});

// Handle background/quit messages
messaging().setBackgroundMessageHandler(async message => {
  console.log('Background message:', message);
});

// Handle token refresh
messaging().onTokenRefresh(token => {
  sendTokenToServer(token);
});

iOS 原生(Swift)

import UserNotifications

func requestAuthorization() {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
        if granted {
            DispatchQueue.main.async {
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    sendTokenToServer(token)
}

Android(Kotlin)

class MyFirebaseService : FirebaseMessagingService() {
    override fun onNewToken(token: String) {
        sendTokenToServer(token)
    }

    override fun onMessageReceived(message: RemoteMessage) {
        message.notification?.let {
            showNotification(it.title, it.body)
        }
    }

    private fun showNotification(title: String?, body: String?) {
        val channelId = "default"
        val notification = NotificationCompat.Builder(this, channelId)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_notification)
            .build()

        NotificationManagerCompat.from(this).notify(0, notification)
    }
}

最佳实践

  • 在适当的时机请求权限
  • 处理令牌刷新
  • 支持通知渠道(Android)
  • 实现深度链接
  • 切勿在载荷中发送敏感数据
  • 在真实设备上测试

评论

支持 Markdown