Ionic 3 phonegap 推送插件如何堆叠 firebase 通知
Ionic 3 phonegap push plugin how to stack firebase notifications
我有一个应用程序,只要数据库项发生更改,它就会向用户发送推送通知。但是,问题是每当我发送通知时,它都会在通知抽屉中创建一个新项目。但我想做的是将这些通知堆叠或分组,这样所有通知都只有一个项目,上面写着类似“9 项已更改”之类的内容。
如何使用 Ionic 3、phonegap 推送插件和 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)));
以及我在 firebase 函数中所拥有的:
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);
});
}
});
}
在查看了官方文档并进行了一些实验后,我终于找到了答案。基本上我需要做的是在我的推送通知中用 "data" 字段替换 "notification" 字段并赋予它不同的属性。请注意,当且仅当您完全删除 "notification" 字段时,给定的代码才有效。
这是当前的有效载荷:
let payload = {
data: {
title: 'Items list',
message: `Added: ${event.data.val().itemName} [${event.data.val().itemNumber}]`,
style: 'inbox',
summaryText: 'There are %n% notifications'
}
};
其中 "style" 属性 使 Ionic 堆叠通知。
如果您不使用 "style" 属性,每个新通知都将替换之前的通知。
如果您使用 "notification" 属性 而不是 "data",它将在每次推送时在通知栏中创建新通知。
另请注意,您可以将 collapseKey 选项添加到您的请求中,以便在消息尚未到达时仅向您的用户显示最新消息。你可以这样做:
const options = {
collapseKey: 'sl_update'
};
admin.messaging().sendToDevice(tokens, payload, options);
我希望这会对某人有所帮助。
有关详细信息,请参阅 https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#stacking
我有一个应用程序,只要数据库项发生更改,它就会向用户发送推送通知。但是,问题是每当我发送通知时,它都会在通知抽屉中创建一个新项目。但我想做的是将这些通知堆叠或分组,这样所有通知都只有一个项目,上面写着类似“9 项已更改”之类的内容。
如何使用 Ionic 3、phonegap 推送插件和 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)));
以及我在 firebase 函数中所拥有的:
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);
});
}
});
}
在查看了官方文档并进行了一些实验后,我终于找到了答案。基本上我需要做的是在我的推送通知中用 "data" 字段替换 "notification" 字段并赋予它不同的属性。请注意,当且仅当您完全删除 "notification" 字段时,给定的代码才有效。
这是当前的有效载荷:
let payload = {
data: {
title: 'Items list',
message: `Added: ${event.data.val().itemName} [${event.data.val().itemNumber}]`,
style: 'inbox',
summaryText: 'There are %n% notifications'
}
};
其中 "style" 属性 使 Ionic 堆叠通知。
如果您不使用 "style" 属性,每个新通知都将替换之前的通知。
如果您使用 "notification" 属性 而不是 "data",它将在每次推送时在通知栏中创建新通知。
另请注意,您可以将 collapseKey 选项添加到您的请求中,以便在消息尚未到达时仅向您的用户显示最新消息。你可以这样做:
const options = {
collapseKey: 'sl_update'
};
admin.messaging().sendToDevice(tokens, payload, options);
我希望这会对某人有所帮助。
有关详细信息,请参阅 https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#stacking