@capacitor/push-notifications
Push Notifications API 提供对原生推送通知的访问。
安装
npm install @capacitor/push-notifications
npx cap sync
iOS
在 iOS 上,您必须启用推送通知功能。请参阅 设置功能 了解如何启用该功能的说明。
启用推送通知功能后,将以下内容添加到您的应用的 AppDelegate.swift 中:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
NotificationCenter.default.post(name: .capacitorDidRegisterForRemoteNotifications, object: deviceToken)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
NotificationCenter.default.post(name: .capacitorDidFailToRegisterForRemoteNotifications, object: error)
}
Android
推送通知 API 使用 Firebase Cloud Messaging SDK 处理通知。请参阅 在 Android 上设置 Firebase Cloud Messaging 客户端应用 并按照说明创建 Firebase 项目和注册您的应用程序。无需向您的应用添加 Firebase SDK 或编辑应用清单 - 推送通知插件会为您提供这些。只需将 Firebase 项目的 google-services.json 文件添加到应用的模块(应用级)目录即可。
变量
此插件将使用以下项目变量(在您的应用的 variables.gradle 文件中定义):
$firebaseMessagingVersion版本:com.google.firebase:firebase-messaging(默认值:21.0.1)
推送通知图标
在 Android 上,应将适当名称的推送通知图标添加到 AndroidManifest.xml 文件中:
<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@mipmap/push_icon_name" />