我正在使用ionic2与cordova原生firebase插件。
当我想通过推送通知通知我的用户聊天中有新消息时。 因此,每当用户发布消息并将其发送到我的主题时,我都会创建通知:/ topics / chat
我希望设备上有一个逻辑,用于检查通知是否与以前相同,并过滤掉新的。
但现在我收到用户添加消息的每一行的通知,这不是那么有趣; - )
有没有一种简单的方法来实现这一点,而不是收集所有usertokens并逐一通知他们,并检查他们是否已经收到聊天通知?
编辑: chat.service.ts
//Angular
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
//Services
import { LoginService } from '../services/login.service';
import { FCMPushService } from '../services/fcmpush.service';
//Models
import { ChatModel } from '../models/chat';
@Injectable()
export class ChatService {
constructor (
private loginService: LoginService,
private fcmpushService: FCMPushService
){ }
create(object: ChatModel): void {
this.loginService.getUser().subscribe( userdata => {
object.user = this.loginService.getAuth().uid;
object.created = new Date().getTime();
this.loginService.af.database.list(`/games/${userdata.gameid}/chat/`).push(object).then( data => {
this.fcmpushService.sendNotification('Chat', 'There is a new message in your aTippr chat!', '/topics/chat');
});
})
}
}
fcmpush.service.ts
//Angular
import { Injectable, Inject } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
//Ionic
import { Firebase } from '@ionic-native/firebase';
import { Platform } from 'ionic-angular';
//Rxjs
import { Observable } from 'rxjs';
@Injectable()
export class FCMPushService {
constructor (
public platform: Platform,
public http: Http,
private FireNative: Firebase,
){}
sendNotification(title: String, content: String, to: String): Observable<any> {
//Headers
let headers = new Headers({"Authorization": 'key=####'});
headers.append('Content-Type', 'application/json')
let options = new RequestOptions({ headers: headers });
//Body
let body = {
"notification":{
"title":title,
"body":content,
"sound":"default",
"icon":"ic_notification"
},
/*"data":{
"param1":"value1",
"param2":"value2"
},*/
"to":to,
"priority":"high",
"restricted_package_name":"###"
}
// Send
return this.http.post('https://fcm.googleapis.com/fcm/send', body, options);
}
}