我目前正在开发一个具有聊天功能的ionic 3项目。我正在使用firebase firestore作为数据库。
我的目标是实现一个应用程序范围的通知功能,该功能将在firestore聊天集合中创建新消息时触发,但是我在尝试实现此功能时遇到了一些困难。
这是我的:
this.chat = this.firestoreProvider.getChatRooms(userEmail).valueChanges();
this.chat.subscribe(res=>{
// Triggered when there are changes in the firestore chat room collection
});
这是我的firestoreProvider
中的代码块:
getChatRooms(chatRoomOwnerEmail:string){
return this.firestore.collection("Chat Room", ref => ref.where("chatRoomOwnerEmail", "==", chatRoomOwnerEmail).orderBy("chatRoomLastMessage", "asc"));
}
我试图将此方法放入app.component.ts
,但是当我进行离子注入时,它使整个应用程序崩溃了。
关于如何使用Firestore实施可在应用程序中所有页面上触发的实时通知功能,是否有任何解决方案? (意味着在所有页面中,聊天室中对新消息的检查将继续触发)。
更新:
我设法发出了实时通知,但是现在当出现通知时,我需要在主页上做一些事情。关于如何做到这一点的任何建议?
嗯,由于您正在使用ionic
,因此可以使用承载AppComponent
的ion-nav
来监听更改。然后例如在收到响应时举杯祝酒。