gpt4 book ai didi

angular - 如何读取 firebase 推送通知内容并触发 ionic2 中的方法?

转载 作者:搜寻专家 更新时间:2023-10-30 21:15:31 24 4
gpt4 key购买 nike

是否可以在 ionic 2 中访问推送通知内容并在通知到达或事件触发时执行一堆代码?

最佳答案

我建议使用 cordova-plugin-firebase反而。你可以看看this repo查看如何使用该插件。

请注意,您需要先配置 firebase 控制台,然后下载 .json/.plist 文件并将它们添加到 Ionic 应用程序的根文件夹中。然后你就可以开始使用这个插件了。

在演示中,一切都在 app.component.ts 文件中完成,但我建议创建一个 PushNotificationService 以保持一切井井有条。

另请注意,该演示使用了主题功能,因此设备可以订阅特定主题,然后我们可以使用这些主题向某些用户群发送通知(仅限android或ios,仅限特定用户,应用程序中的所有用户......):

this.firebase.subscribe('firebase-app'),    // Subscribe to the entire app
this.firebase.subscribe('android'), // Subscribe to android users topic
this.firebase.subscribe('userid-1') // Subscribe using the user id (hardcoded in this example)

这是所有相关代码:

// Angular
import { Component } from '@angular/core';

// Ionic
import { Platform, AlertController } from 'ionic-angular';

// Ionic Native
import { Firebase } from '@ionic-native/firebase';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

// Pages
import { HomePage } from '../pages/home/home';

export class NotificationModel {
public body: string;
public title: string;
public tap: boolean
}

@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage: any = HomePage;

constructor(private platform: Platform,
private alertCtrl: AlertController,
private firebase: Firebase,
private statusBar: StatusBar,
private splashScreen: SplashScreen) {

this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();

if (this.platform.is('cordova')) {
// Initialize push notification feature
this.platform.is('android') ? this.initializeFireBaseAndroid() : this.initializeFireBaseIos();
} else {
console.log('Push notifications are not enabled since this is not a real device');
}
});
}

private initializeFireBaseAndroid(): Promise<any> {
return this.firebase.getToken()
.catch(error => console.error('Error getting token', error))
.then(token => {

console.log(`The token is ${token}`);

Promise.all([
this.firebase.subscribe('firebase-app'), // Subscribe to the entire app
this.firebase.subscribe('android'), // Subscribe to android users topic
this.firebase.subscribe('userid-1') // Subscribe using the user id (hardcoded in this example)
]).then((result) => {
if (result[0]) console.log(`Subscribed to FirebaseDemo`);
if (result[1]) console.log(`Subscribed to Android`);
if (result[2]) console.log(`Subscribed as User`);

this.subscribeToPushNotificationEvents();
});
});
}

private initializeFireBaseIos(): Promise<any> {
return this.firebase.grantPermission()
.catch(error => console.error('Error getting permission', error))
.then(() => {
this.firebase.getToken()
.catch(error => console.error('Error getting token', error))
.then(token => {

console.log(`The token is ${token}`);

Promise.all([
this.firebase.subscribe('firebase-app'),
this.firebase.subscribe('ios'),
this.firebase.subscribe('userid-2')
]).then((result) => {

if (result[0]) console.log(`Subscribed to FirebaseDemo`);
if (result[1]) console.log(`Subscribed to iOS`);
if (result[2]) console.log(`Subscribed as User`);

this.subscribeToPushNotificationEvents();
});
});
})

}

private saveToken(token: any): Promise<any> {
// Send the token to the server
console.log('Sending token to the server...');
return Promise.resolve(true);
}

private subscribeToPushNotificationEvents(): void {

// Handle token refresh
this.firebase.onTokenRefresh().subscribe(
token => {
console.log(`The new token is ${token}`);
this.saveToken(token);
},
error => {
console.error('Error refreshing token', error);
});

// Handle incoming notifications
this.firebase.onNotificationOpen().subscribe(
(notification: NotificationModel) => {

!notification.tap
? console.log('The user was using the app when the notification arrived...')
: console.log('The app was closed when the notification arrived...');

let notificationAlert = this.alertCtrl.create({
title: notification.title,
message: notification.body,
buttons: ['Ok']
});
notificationAlert.present();
},
error => {
console.error('Error getting the notification', error);
});
}
}

另请注意,如果应用程序在前台运行或应用程序在通知到达时关闭,则发送的通知内容将不同。为了处理这个问题,在发送通知时,在高级选项部分添加titlebody

Example

关于angular - 如何读取 firebase 推送通知内容并触发 ionic2 中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43928185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com