【问题标题】:Ionic 3 phonegap push plugin how to stack firebase notificationsIonic 3 phonegap 推送插件如何堆叠 Firebase 通知
【发布时间】:2018-03-05 12:12:39
【问题描述】:

我有一个应用程序,当数据库项目更改时,它会向用户发送推送通知。但是,问题是每当我发送通知时,它都会在通知抽屉中创建一个新项目。但我想要做的是将这些通知堆叠或分组,以便所有通知只有一项,上面写着“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);
            });
        }
    });
}

【问题讨论】:

    标签: cordova firebase ionic-framework push-notification phonegap-plugins


    【解决方案1】:

    在查看了官方文档并进行了一些实验后,我终于找到了答案。基本上我需要做的是在我的推送通知中用“数据”字段替换“通知”字段并赋予它不同的属性。请注意,当且仅当您完全删除“通知”字段时,给定的代码才有效。

    这是当前的有效载荷:

    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 堆叠通知。

    如果您不使用“样式”属性,则每个新通知都会替换之前的通知。

    如果您使用“通知”属性而不是“数据”,它将在每次推送的通知阴影中创建新通知。

    还请注意,您可以在请求中添加 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

    【讨论】:

      猜你喜欢
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 2018-12-25
      • 1970-01-01
      • 2023-03-16
      • 2017-10-10
      相关资源
      最近更新 更多