我有一個應用程序,每當數據庫項目被更改時向用戶發送推送通知。但是,問題是每當我發送通知時,它都會在通知抽屜中創建一個新項目。但是我想要做的是對這些通知進行堆疊或分組,因此只有一個項目表示「9個項目已更改」。Ionic 3 phonegap推送插件如何堆疊firebase通知
我如何用Ionic 3,phonegap push插件和firebase做到這一點?
這裏是我當前的代碼:
const options: PushOptions = {
android: {
senderID: SENDER_ID
},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
},
pushObject: PushObject = this.push.init(options);
pushObject.on('registration').subscribe((registration: any) => {
this.afDatabase.list('/users')
.update(`/${user.uid}/devices/${registration.registrationId}/`, {isKept: true});
});
pushObject.on('error').subscribe(error => alert('Error with Push plugin' + JSON.stringify(error)));
而且我在火力功能是什麼:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const _ = require('lodash');
admin.initializeApp(functions.config().firebase);
exports.onItemsListItemAdd = functions.database.ref('/items-list/{item_id}').onCreate(event => {
let payload = {
notification: {
title: 'Items list',
body: `Added: ${event.data.val().itemName} [${event.data.val().itemNumber}]`,
icon: 'default'
}
};
return sendToDevices(payload);
});
exports.onItemsListItemUpdate = functions.database.ref('/items-list/{item_id}').onUpdate(event => {
let payload = {
notification: {
title: 'Items list',
body: `Updated: ${event.data.val().itemName} [${event.data.val().itemNumber}]`,
icon: 'default'
}
};
return sendToDevices(payload);
});
exports.onItemsListItemDelete = functions.database.ref('/items-list/{item_id}').onDelete(event => {
let payload = {
notification: {
title: 'Items list',
body: `Deleted: ${event.data.previous.val().itemName} [${event.data.previous.val().itemNumber}]`,
icon: 'default'
}
};
return sendToDevices(payload);
});
function sendToDevices(payload) {
const deviceTokens = admin.database().ref('/users').once('value');
return deviceTokens.then(allTokens => {
if (allTokens.val()) {
// Listing all tokens.
const tokens = _(allTokens.val())
.mapValues(user => user.devices)
.values()
.map(device => Object.keys(device))
.flatten()
.value();
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
tokensToRemove.push(allTokens.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
}
});
}