在 Ionic + Angular 应用中使用 Firebase 推送通知
Web 框架:Angular 平台:iOS、Android
应用程序开发者向其用户提供的最常见功能之一是推送通知。在本教程中,我们将逐步介绍让 Firebase Cloud Messaging 在 iOS 和 Android 上工作所需的所有步骤。
为了注册和监控来自 Firebase 的推送通知,我们将在 Ionic + Angular 应用程序中使用 Capacitor 的 Push Notification API。
所需依赖项
使用 Capacitor 构建和部署 iOS 和 Android 应用程序需要一些设置。在继续之前,请 按照说明安装必要的 Capacitor 依赖项。
要在 iOS 上测试推送通知,Apple 要求您拥有 一个付费的 Apple Developer 帐户 和一台 物理 iOS 设备。
如果您遇到问题或控制台抛出有关过时或废弃包的警告,请确保您使用的是 Node、Android Studio 和 Xcode 的最新稳定版本。
另外,我们使用 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
在提示将您的新应用与 Capacitor 集成时,输入 y 并按回车。这会将 Capacitor 和 Capacitor CLI 添加到我们的新应用程序中。
成功创建应用程序后,切换到新创建的项目目录:
cd capApp/
最后运行 npx cap init,这将允许我们填写应用信息。
npx cap init
? App name: CapApp
? App Package ID: com.mydomain.myappname
构建应用和添加平台
在向此项目添加任何原生平台之前,必须至少构建一次应用。Web 构建会创建 Capacitor 所需的 web 资源目录(Ionic Angular 项目中的 www 文件夹)。
ionic build
接下来,让我们为应用添加 iOS 和 Android 平台。
npx cap add ios
npx cap add android
运行这些命令后,项目根目录下会创建 android 和 ios 文件夹。这些是完全独立的原生项目制品,应被视为您的 Ionic 应用的一部分(即,将它们检入版本控制)。
使用 Capacitor Push Notification API
在讨论 Firebase 之前,我们需要确保我们的应用程序可以通过使用 Capacitor Push Notification API 注册推送通知。我们还将添加一个 alert(您也可以使用 console.log 语句代替)来在通知到达且 应用在设备上打开时显示通知的负载。
在您的应用中,转到 home.page.ts 文件,添加 import 语句和 const 以使用 Capacitor Push API:
import {
Plugins,
PushNotification,
PushNotificationToken,
PushNotificationActionPerformed,
} from '@capacitor/core';
const { PushNotifications } = Plugins;
然后,添加 ngOnInit() 方法以及一些用于注册和监控推送通知的 API 方法。我们还将为其中一些事件添加 alert() 以监控正在发生的事情:
export class HomePage implements OnInit {
ngOnInit() {
console.log('Initializing HomePage');
// 请求使用推送通知的权限
// iOS 将提示用户并返回是否授予权限
// Android 将直接授予而不提示
PushNotifications.requestPermission().then( result => {
if (result.granted) {
// 向 Apple / Google 注册以通过 APNS/FCM 接收推送
PushNotifications.register();
} else {
// 显示一些错误
}
});
// 成功时,我们应该能够接收通知
PushNotifications.addListener('registration',
(token: PushNotificationToken) => {
alert('Push registration success, token: ' + token.value);
}
);
// 设置有问题,推送将无法工作
PushNotifications.addListener('registrationError',
(error: any) => {
alert('Error on registration: ' + JSON.stringify(error));
}
);
// 如果应用在设备上打开,向我们显示通知负载
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotification) => {
alert('Push received: ' + JSON.stringify(notification));
}
);
// 点击通知时调用的方法
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: PushNotificationActionPerformed) => {
alert('Push action performed: ' + JSON.stringify(notification));
}
);
}
以下是 home.page.ts 的完整实现:
import { Component, OnInit } from '@angular/core';
import {
Plugins,
PushNotification,
PushNotificationToken,
PushNotificationActionPerformed,
} from '@capacitor/core';
const { PushNotifications } = Plugins;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage implements OnInit {
ngOnInit() {
console.log('Initializing HomePage');
// 请求使用推送通知的权限
// iOS 将提示用户并返回是否授予权限
// Android 将直接授予而不提示
PushNotifications.requestPermission().then(result => {
if (result.granted) {
// 向 Apple / Google 注册以通过 APNS/FCM 接收推送
PushNotifications.register();
} else {
// 显示一些错误
}
});
PushNotifications.addListener(
'registration',
(token: PushNotificationToken) => {
alert('Push registration success, token: ' + token.value);
},
);
PushNotifications.addListener('registrationError', (error: any) => {
alert('Error on registration: ' + JSON.stringify(error));
});
PushNotifications.addListener(
'pushNotificationReceived',
(notification: PushNotification) => {
alert('Push received: ' + JSON.stringify(notification));
},
);
PushNotifications.addListener(
'pushNotificationActionPerformed',
(notification: PushNotificationActionPerformed) => {
alert('Push action performed: ' + JSON.stringify(notification));
},
);
}
}
之后,您需要生成新构建并让 Capacitor 知道这些更改。您可以通过以下方式完成:
ionic build
npx cap copy
在 Firebase 上为您的应用创建项目
在将 Firebase Cloud Messaging 连接到您的应用程序并发送推送通知之前,您需要在 Firebase 中启动一个项目。
转到 Firebase 控制台 并点击 Add project 按钮。
命名项目,接受 Firebase ToS 并点击 Create project 继续。项目 ID 将自动为您生成。