在 Ionic + Angular 应用中使用 Firebase 推送通知
Web 框架: Angular 平台: iOS, Android
应用开发者向用户提供的最常见功能之一就是推送通知。在本教程中,我们将逐步介绍在 iOS 和 Android 上实现 Firebase Cloud Messaging 所需的所有步骤。
为了注册和监听来自 Firebase 的推送通知,我们将在 Ionic + Angular 应用中使用 Capacitor 的推送通知 API。
所需依赖
使用 Capacitor 构建和部署 iOS 和 Android 应用需要进行一些设置。请按照这里的说明安装必要的 Capacitor 依赖,然后再继续。
要在 iOS 上测试推送通知,Apple 要求您拥有付费的 Apple 开发者账户。
此外,我们使用 Firebase 进行推送通知,因此如果您使用其他依赖 Firebase SDK 的 Cordova 插件,请确保它们使用的是最新版本。
准备 Ionic Capacitor 应用
如果您已有 Ionic 应用,请跳过此部分。如果没有,让我们先创建一个 Ionic 应用。
在您首选的终端中,安装最新版本的 Ionic CLI:
npm install -g @ionic/cli
接下来,让我们使用 CLI 基于空白入门项目创建一个新的 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 推送 API:
import { ActionPerformed, PushNotificationSchema, PushNotifications, Token } from '@capacitor/push-notifications';
然后,添加 ngOnInit() 方法以及一些用于注册和监听推送通知的 API 方法。我们还将添加一些事件监控的 alert() 来观察发生的情况:
export class HomePage implements OnInit {
ngOnInit() {
console.log('Initializing 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.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('Initializing HomePage');
// Request permission to use push notifications
// iOS will prompt user and return if they granted permission or not
// Android will just grant without prompting
PushNotifications.requestPermissions().then((result) => {
if (result.receive === 'granted') {
// Register with Apple / Google to receive push via APNS/FCM
PushNotifications.register();
} else {
// Show some error
}
});
PushNotifications.addListener('registration', (token: Token) => {
alert('Push registration success, token: ' + token.value);
});
PushNotifications.addListener('registrationError', (error: any) => {
alert('Error on registration: ' + JSON.stringify(error));
});
PushNotifications.addListener('pushNotificationReceived', (notification: PushNotificationSchema) => {
alert('Push received: ' + JSON.stringify(notification));
});
PushNotifications.addListener('pushNotificationActionPerformed', (notification: ActionPerformed) => {
alert('Push action performed: ' + JSON.stringify(notification));
});
}
}
完成此操作后,您需要生成新的构建并让 Capacitor 了解这些更改。您可以通过以下命令完成:
ionic build
npx cap copy
在 Firebase 中为您的应用创建项目
在将 Firebase Cloud Messaging 连接到您的应用并发送推送通知之前,您需要在 Firebase 中启动一个项目。
前往 Firebase 控制台,点击添加项目按钮。
为项目命名,接受 Firebase 服务条款,然后点击创建项目继续。系统应自动为您生成项目 ID。