在 Ionic + Angular 应用中使用 Firebase 推送通知
Web 框架:Angular 平台:iOS, Android
应用开发者向其用户提供的最常见功能之一是推送通知。在本教程中,我们将逐步介绍让 Firebase Cloud Messaging 在 iOS 和 Android 上工作所需的所有步骤。
为了从 Firebase 注册和监控推送通知,我们将在 Ionic + Angular 应用中使用 Capacitor 的推送通知 API。
所需的依赖项
使用 Capacitor 构建和部署 iOS 和 Android 应用需要一些设置。请在继续之前按照说明安装必要的 Capacitor 依赖项。
要在 iOS 上测试推送通知,Apple 要求你拥有一个付费的 Apple Developer 账户。
此外,我们使用 Firebase 进行推送通知,因此如果你使用其他使用 Firebase SDK 的 Cordova 插件,请确保它们是最新版本。
准备 Ionic Capacitor 应用
如果你已有现有的 Ionic 应用,请跳过本节。如果没有,让我们先创建一个 Ionic 应用。
在你喜欢的终端中,安装最新版本的 Ionic CLI:
npm install -g @ionic/cli
接下来,让我们使用 CLI 基于 blank 启动项目创建一个新的 Ionic Angular 应用,并将其命名为 capApp:
ionic start capApp blank --type=angular
应用创建成功后,切换到新创建的项目目录:
cd capApp/
最后,编辑 capacitor.config.ts 中的 appId。
const config: CapacitorConfig = {
- appId: 'io.ionic.starter',
+ appId: 'com.mydomain.myappnam',
appName: 'capApp',
webDir: 'www'
};
构建应用并添加平台
在向此项目添加任何原生平台之前,必须至少构建应用一次。Web 构建会创建 Capacitor 所需的 Web 资源目录(Ionic Angular 项目中的 www 文件夹)。
ionic build
接下来,让我们为应用添加 iOS 和 Android 平台。
ionic cap add ios
ionic cap add android
运行这些命令后,项目根目录下会创建 android 和 ios 文件夹。这些是完全独立的原生项目工件,应被视为你 Ionic 应用的一部分(即,将其检入到源代码管理中)。
使用 Capacitor 推送通知 API
首先,我们需要安装 Capacitor 推送通知插件。
npm install @capacitor/push-notifications
npx cap sync
然后,在我们进入 Firebase 之前,我们需要确保我们的应用可以通过使用 Capacitor 推送通知 API 注册推送通知。我们还会添加一个 alert(你也可以使用 console.log 语句)来显示通知到达且应用在设备上打开时的负载。
在你的应用中,进入 home.page.ts 文件,添加 import 语句和一个 const 来使用 Capacitor Push API:
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token } from '@capacitor/push-notifications';
然后,添加带有一些 API 方法的 ngOnInit() 方法来注册和监控推送通知。我们还会在一些事件中添加 alert() 来监控正在发生的事情:
export class HomePage implements OnInit {
ngOnInit() {
console.log('正在初始化 HomePage');
// 请求使用推送通知的权限
// iOS 将提示用户并返回是否授予权限
// Android 将直接授予而不提示
PushNotifications.requestPermissions().then((result) => {
if (result.receive === 'granted') {
// 向 Apple / Google 注册以通过 APNS/FCM 接收推送
PushNotifications.register();
} else {
// 显示一些错误信息
}
});
// 成功后,我们应该能够接收通知
PushNotifications.addListener('registration', (token: Token) => {
alert('推送注册成功,token:' + token.value);
});
// 我们的设置有问题,推送将无法工作
PushNotifications.addListener('registrationError', (error: any) => {
alert('注册错误:' + JSON.stringify(error));
});
// 如果应用在设备上打开,向我们显示通知负载
PushNotifications.addListener('pushNotificationReceived', (notification: PushNotificationSchema) => {
alert('收到推送:' + JSON.stringify(notification));
});
// 点击通知时调用的方法
PushNotifications.addListener('pushNotificationActionPerformed', (notification: ActionPerformed) => {
alert('推送操作已执行:' + JSON.stringify(notification));
});
}
}
以下是 home.page.ts 的完整实现:
import { Component, OnInit } from '@angular/core';
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token } from '@capacitor/push-notifications';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
ngOnInit() {
console.log('正在初始化 HomePage');
// 请求使用推送通知的权限
// iOS 将提示用户并返回是否授予权限
// Android 将 直接授予而不提示
PushNotifications.requestPermissions().then((result) => {
if (result.receive === 'granted') {
// 向 Apple / Google 注册以通过 APNS/FCM 接收推送
PushNotifications.register();
} else {
// 显示一些错误信息
}
});
PushNotifications.addListener('registration', (token: Token) => {
alert('推送注册成功,token:' + token.value);
});
PushNotifications.addListener('registrationError', (error: any) => {
alert('注册错误:' + JSON.stringify(error));
});
PushNotifications.addListener('pushNotificationReceived', (notification: PushNotificationSchema) => {
alert('收到推送:' + JSON.stringify(notification));
});
PushNotifications.addListener('pushNotificationActionPerformed', (notification: ActionPerformed) => {
alert('推送操作已执行:' + JSON.stringify(notification));
});
}
}
之后,你需要生成一个新的构建并让 Capacitor 知道这些更改。你可以通过以下方式完成:
ionic build
npx cap copy
在 Firebase 上为你的应用创建项目
在我们将 Firebase Cloud Messaging 连接到你的应用并发送推送通知之前,你需要在 Firebase 中启动一个项目。
转到 Firebase 控制台,点击 Add project 按钮。
为项目命名,接受 Firebase ToS,然后点击 Create project 继续。项目 ID 将自动为你生成。
Android
将 Firebase 与 Android 应用集成
本节或多或少与使用 Firebase 控制台文档设置 Firebase 类似。请参阅下面特定于 Capacitor 的说明。
转到你的 Firebase 项目的 Project Overview 页面,在顶部点击 Android 图标以添加一个新的 Android 应用。

下一个屏幕会要求你提供一些关于应用的信息。
- 你的 Android 包名 应与
capacitor.config.ts中的 appId 匹配 - 我们为此 Capacitor 应用 ID 使用了
com.mydomain.myappname,所以这就是我们用于此条目的内容。 - 昵称和调试签名证书是可选的
然后点击 Register app 按钮。
下载并使用 google-services.json 文件
下一个提示将要求你下载 google-services.json 文件。此文件包含你的 Capacitor 应用从 Android 连接到 Firebase 所需的信息。
将 google-services.json 文件下载到本地机器。然后将文件移动到你的 Capacitor Android 项目目录中,特别是 android/app/ 下。

我们不需要向项目_添加_任何依赖项,因为 @capacitor/push-notifications 在其 build.gradle 文件中自动包含了 firebase-messaging 的一个版本。
iOS
前提条件
iOS 推送通知的设置比 Android 复杂得多。你必须拥有一个付费的 Apple Developer 账户_并且_在能够使用你的 iOS 应用测试推送通知之前,需要处理以下事项:
- 在 Apple Developer Portal 中为你的 iOS 应用设置正确的开发或生产证书和配置文件
- 在 Apple Developer Portal 中为开发或生产创建 APNS 证书或密钥
- 在 Xcode 中确保已启用推送通知功能