I developed a realtime chat with ionic + firebase, and at the moment I'm trying to develop the notification system if a new msg will be received. I'm using Onesignal, but I have no idea how I could perform this integration.
ionViewDidLoad() {
//recebe parametros
this.recipient = this.navParams.get('recipientUser');
this.pageTitle = this.recipient.name;
//pegar remetente
this.userService.currentUser
.first()//somente primeiro valor
.subscribe((currentUser: User) => {
this.sender = currentUser;
this.chat1 = this.chatService.getDeepChat(this.sender.$key, this.recipient.$key);//pega soemnte um chat
this.chat2 = this.chatService.getDeepChat(this.recipient.$key, this.sender.$key);
if (this.recipient.photo) {
this.chat1
.first()
.subscribe((chat: Chat) => {
this.chatService.updatePhoto(this.chat1, chat.photo, this.recipient.photo);
});
}
let doSubscription = () => {
this.messages
.subscribe((messages: Message[]) => {
this.scrollToBottom();
});
};
this.messages = this.messageService
.getMessages(this.sender.$key, this.recipient.$key);
this.messages
.first()
.subscribe((messages: Message[]) => {
if (messages.length === 0) {//se for igual a 0 nao existe msmg na lista
this.messages = this.messageService
.getMessages(this.recipient.$key, this.sender.$key);//busca ao contrario
doSubscription();
} else {
doSubscription();
}
});
});
}
//RECEBE MENSAGEM DA VIEW
sendMessage(newMessage: string): void {
if (newMessage) {//se existir msm
let currentTimestamp: Object = firebase.database.ServerValue.TIMESTAMP;
this.messageService.create(
new Message(
this.sender.$key,
newMessage,
currentTimestamp
),
this.messages
).then(() => {
this.chat1
.update({
lastMessage: newMessage,
timestamp: currentTimestamp
});
this.chat2
.update({
lastMessage: newMessage,
timestamp: currentTimestamp
});
});
}
}
//
private scrollToBottom(duration?: number): void {
setTimeout(() => {
if (this.content._scroll) {
this.content.scrollToBottom(duration || 300);
}
}, 50);
}