Ionic 2 - 在接收推送通知时在应用内部运行代码

时间:2017-02-17 06:50:39

标签: android push-notification ionic2

我正在Android的Ionic 2应用中实现推送通知。为此,我使用以下功能:

registerPush() {
    this.push.register().then((t: PushToken) => {
        return this.push.saveToken(t, {ignore_user: false});
    }).then((t: PushToken) => {
        this.push.rx.notification().subscribe(msg => {
            // execute some code
        });
    });
}

有了这个,我就能收到服务器发送的推送通知。当应用程序位于前台时,execute some code部分运行没有问题。虽然应用程序在后台(我正在使用后台插件),但是收到推送通知,但是当我点击它时没有任何反应。

我也想在这种情况下执行通知代码,并在点击它时打开应用程序。有没有办法实现这个目标?

更新

我已阅读插件文档并相应更改了代码:

registerPush() {
    this.pushNotifications = Push.init({ android: { senderID: "xxxxxxxx" } });

    this.pushNotifications.on('registration', data => {
        // send token to server
    });

    this.pushNotifications.on('notification', data => {
        // handle notification
    });

 }

有了这个,并且在服务器发送的通知中将content-available设置为1,应用程序会执行代码,无论我是否在其中。 但是,当我点击通知时,我仍然无法将其置于前台。

0 个答案:

没有答案