
FCM 控制器
@capacitor-community/fcm
启用 FCM 能力的电容社区插件
维护者
| 维护者 | GitHub 图像 | 社会 |
|---|---|---|
| 斯特万·席尔瓦 | 炖鱼 | @stewones (英语) |
通知
我们从一个正式的Ong开始 如果您使用上一个 npm 软件包 capacitor-fcm请更新您的软件包 @capacitor-community/fcm检查 更改日志 需要更多信息.
安装
使用 npm :
npm install @capacitor-community/fcm
使用线条 :
yarn add @capacitor-community/fcm
同步本地文件 :
npx cap sync
兼容性
| 插件 | 电容 | iOS 部署目标 | 机器人 minSdkVersion |
|---|---|---|---|
| 8.1.0+ (单位:千美元) | 第8条 | 15.0 (韩语) | 24个 |
| 7.x - 8.0.x (中文(简体) ) | 第7条 | 14.0 (韩语) | 第23条 |
| 6.x (单位:千美元) | 6个 | 13.0 (韩语) | 22个 |
| 4.x - 5.x (英语) | 页:1 | 13.0 (韩语) | 22个 |
| 3.x 运算 | 页:1 | 13.0 (韩语) | 22个 |
通知
此插件旨在与 Capacitor API 结合使用 推动通知。 Capacitor 仅提供 APN 令牌,而这个插件提供了与 FCM 令牌和更多符号合作的可能性.
API 密码
| 方法 | 资讯 | 平台 |
|---|---|---|
subscribeTo | 订阅 fcm 话题 | ios/类固醇 |
unsubscribeFrom | 从 fcm 话题取消订阅 | ios/类固醇 |
getToken | 从服务器获取 fcm 令牌最终使用 | ios/类固醇 |
refreshToken | 刷新 fcm 令牌以获得新的 | ios/类固醇 |
deleteInstance | 完全删除本地 fcm 实例 | ios/类固醇 |
setAutoInit | 启用库的自动初始化 | ios/类固醇 |
isAutoInitEnabled | 检查是否启用了自动初始化 | ios/类固醇 |
| 事件 | 资讯 | 平台 |
|---|---|---|
tokenReceived | 随火而起 { token } 当 FCM 发行或旋转符号时。 在Android上,听Push Notifics的 registration 事件; 其值为 FCM 符号 | 欧斯 |
使用量
import { FCM } from '@capacitor-community/fcm';
import { PushNotifications } from '@capacitor/push-notifications';
// external required step
// register for push
await PushNotifications.requestPermissions();
await PushNotifications.register();
// now you can subscribe to a specific topic
FCM.subscribeTo({ topic: 'test' })
.then(r => alert(`subscribed to topic`))
.catch(err => console.log(err));
// Unsubscribe from a specific topic
FCM.unsubscribeFrom({ topic: 'test' })
.then(() => alert(`unsubscribed from topic`))
.catch(err => console.log(err));
// Get FCM token instead of the APN one returned by Capacitor
FCM.getToken()
.then(r => alert(`Token ${r.token}`))
.catch(err => console.log(err));
// Delete the old FCM token and get a new one
FCM.refreshToken()
.then(r => alert(`Token ${r.token}`))
.catch(err => console.log(err));
// Remove FCM instance
FCM.deleteInstance()
.then(() => alert(`Token deleted`))
.catch(err => console.log(err));
// Enable the auto initialization of the library
FCM.setAutoInit({ enabled: true }).then(() => alert(`Auto init enabled`));
// Check the auto initialization status
FCM.isAutoInitEnabled().then(r => {
console.log('Auto init is ' + (r.enabled ? 'enabled' : 'disabled'));
});
添加 Google 配置文件
导航您在 Firebase 上的应用程序的工程设置页面 .
iOS系统
下载 GoogleService-Info.plist 文档。 在 Xcode 中右键单击名为“ App” 的黄色文件夹,并选择 Add files to "App"。 。 。 .
提示:如果拖放文件到此位置, Xcode 可能找不到它 .
机器人
下载 google-services.json 存档并复制到 android/app/ 您的电容器项目目录 .
证书
- 苹果
- 创建应用程序标识符( apple site)
- 添加按键通知
- 添加签名请求( e)https://developer.apple.com/help/count/certificates/create-a-certificate-signing-request 页面存档备份,存于互联网档案馆 (简体中文)(中文(简体) )
- 生成 APN 密钥,然后在显示的ID下注出。 也下载 p8 文件 (https://fluffy.es/p8-push- notification/ (中文(简体) )(中文(简体) )
- 创建应用程序标识符( apple site)
- 火场
- 将已下载的 p8 文件添加到 Firebase 设置中, 并带有已注解的密钥 ID 和账户团队 ID
iOS 设置
- 安装本地人 (一次)
brew install cocoapods(一次性)ionic start my-cap-app --capacitorcd my-cap-appmkdir www && touch www/index.htmlnpx cap add iosnpm install --save @capacitor-community/fcmnpx cap sync ios(插件安装后总是同步)npx cap open ios
- 在 xcode( 一般标签) 签名您的应用程序
- 启用远程通知能力
- 添加
GoogleService-Info.plist到 xcode 中的应用程序文件夹
// (optional) turn off `swizzling` in the `info.plist`
<key>FirebaseAppDelegateProxyEnabled</key>
<string>NO</string>
提示:每次您更改本地代码时, 您可能需要清理缓存( Product > Clean building文件夹) , 然后再次运行应用程序 .
防止自动初始化
如果您需要执行 option- in 行为, 您可以通过遵循 火场文件。 。 。 .
SPM设置(iOS 15.0+)
首先确保您的所有依赖关系与 SPM 兼容 。 否则,坚持Cocoapods安装步骤.
您也可以使用 Swift 包管理器( SPM) 而不是 CocoaPods 安装插件 . 要做到这一点,你不需要安装CocoaPods(步骤1和2),但需要配置电容器来使用SPM:
- 添加 iOS 目标 :
npx cap add ios --packagemanager SPM - 迁移现有目标:
npx cap spm-migration-assistant(用电容器 7.4.0+).
你可以在官网上找到更多的信息 电容器文件。 。 。 .
机器人设置
ionic start my-cap-app --capacitorcd my-cap-appmkdir www && touch www/index.htmlnpx cap add androidnpm install --save @capacitor-community/fcmnpx cap sync android(插件安装后总是同步)npx cap open android- 添加
google-services.json给您的android/app文件夹
现在你该走了 尝试使用 ionic cap run android --livereload。 。 。 .
提示:每次您更改本地代码, 您可能需要清理缓存( Build > Clean Project QQ Build > Rebuilt Project) , 然后再次运行应用程序 .
变量
此插件将使用以下工程变量( 在您的应用程序中定义) variables.gradle 文件名 :
$firebaseMessagingVersion版本com.google.firebase:firebase-messaging(默认 :25.0.1(中文(简体) )
防止自动初始化
如果您需要执行 option- in 行为, 您可以通过遵循 火场文件。 。 。 .
示例
许可证
麻省理工学院
贡献者
感谢这些伟大的人emoji 密钥:
本项目遵循 全部贡献者 规格。 任何捐款都欢迎!