Ionic+Firebase-推送通知



我有一个ionic+firebase聊天应用程序,我想在收到新消息时向用户发送通知。

在Ionic的官方文档中,推荐的插件是:https://github.com/katzer/cordova-plugin-local-notifications

但正如你在存储库中看到的,自去年2月以来,它一直没有更新,根据里面的评论,它似乎不适用于最新的Android操作系统版本。

有人知道其他选择吗?

谢谢!

上个月我在应用程序中实现推送通知时遇到了同样的问题。这是最好的教程:https://medium.com/@发送/推送通知-带洋葱和蘑菇-蘑菇-防火酶-ab0c0cad3cc0

我将本教程自定义为一个文件:messageaging.service.ts

import {Injectable} from '@angular/core';
import {ApiService} from './api.service';
import {AppApiResponse} from '../interfaces/interfaces'
import {Firebase} from "@ionic-native/firebase";
import {Platform} from "ionic-angular";
import {AngularFirestore} from "@angular/fire/firestore";
@Injectable()
export class MessagingService {
private userUid: string;
constructor(private firebase: Firebase,
public afs: AngularFirestore,
public platform: Platform) {
}
initializeFirebase(userUid) {
this.userUid = userUid;
if (!this.platform.is("core")) {
this.firebase.subscribe("all");
this.platform.is('android') ? this.initializeFirebaseAndroid() : this.initializeFirebaseIOS();
}
}
initializeFirebaseAndroid() {
this.firebase.getToken().then(token => {
this.saveTokenToFirestore(token);
console.log('token android= ' + JSON.stringify(token));
});
this.firebase.onTokenRefresh().subscribe(token => {
this.saveTokenToFirestore(token);
console.log('token refresh android= ' + JSON.stringify(token));
});
this.subscribeToPushNotifications();
}
initializeFirebaseIOS() {
this.firebase.grantPermission()
.then(() => {
this.firebase.getToken().then(token => {
this.saveTokenToFirestore(token);
console.log('token ios= ' + JSON.stringify(token));
});
this.firebase.onTokenRefresh().subscribe(token => {
this.saveTokenToFirestore(token);
console.log('token refresh ios= ' + JSON.stringify(token));
});
this.subscribeToPushNotifications();
})
.catch((error) => {
this.firebase.logError('push erro ios= ' + error);
});
}
subscribeToPushNotifications() {
this.firebase.onNotificationOpen().subscribe((response) => {
console.log('response push= ' + JSON.stringify(response));
if (response.tap) {
//Received while app in background (this should be the callback when a system notification is tapped)
//This is empty for our app since we just needed the notification to open the app
} else {
//received while app in foreground (show a toast)
}
});
}
private saveTokenToFirestore(token) {
if (!token) return;
const devicesRef = this.afs.collection('devices');
const docData = {
token,
userId: this.userUid,
};
return devicesRef.doc(token).set(docData)
}
}

要在代码中使用,只需包含到页面构造函数:

public msgService: MessagingService

使用:

try {
this.msgService.initializeFirebase(user.uid);
} catch (error) {
console.log('fire push erro= ' + error);
}

最新更新