如何在应用程序处于后台时点击通知后接收通知数据?

时间:2018-01-08 09:03:10

标签: android push-notification ionic3

考虑到我的应用正在运行后台或已关闭,当我收到通知并点按它时,我的应用会打开,但我没有从该通知中收到任何数据。

以下是我正在使用的代码:

var Jasmine = require('jasmine');
var jasmine = new Jasmine();

jasmine.loadConfig({
    spec_dir: 'spec',
    spec_files: [
        'someTest.js',
    ]
});

var executeJasmine = new Promise( (resolve,reject) => {
    jasmine.execute();
    jasmine.onComplete(function(status) {
        // status is true if all specs are passed
        // false otherwise
        resolve(status);
    });

});

executeJasmine().then((passed) => {
    // code that gets executed only AFTER jasmine finishes execution
    doSomething();

    if(passed) {
        console.log('All specs have passed');
    }
    else {
        console.log('At least one spec has failed');
    }
})

我期待从该部分获取日志,但我什么都没得到:

initPushNotification() {
        if (!this.platform.is('cordova')) {
            console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
            return;
        }
        const options: PushOptions = {
            android: {
                senderID: MY_SENDER_ID
            },
            ios: {
                alert: 'true',
                badge: false,
                sound: 'true'
            },
            windows: {}
        };
        const pushObject: PushObject = this.push.init(options);

        pushObject.on('registration').subscribe((data: any) => {             
            // send notification token to server
            // ...
        });

        pushObject.on('notification').subscribe((data: any) => {
            console.log(data);
            this.notificationService.display(data);
        });

        pushObject.on('error').subscribe(error => console.error('Error with Push plugin' + error));
    }

注意:我只遇到Android的问题,它在iOS上运行良好。

我正在使用

pushObject.on('notification').subscribe((data: any) => {
            console.log(data);
            this.notificationService.display(data);
        });

我花了好几个小时浏览Ionic docs和Stack,但我不再有想法了。任何帮助将不胜感激。

修改: 这是我的json消息数据:

“@angular/*”: “5.0.0”,
"@ionic-native/push": “4.5.2”,
“cordova-plugin-device”: “1.1.7”,
“cordova-plugin-fcm”: “2.1.2”,

2 个答案:

答案 0 :(得分:0)

这是预期的行为(正如您在文档中看到的那样)。如果您正在使用仅通知消息有效负载(您是),则只有在您的应用处于前台时才会调用onMessageReceived(),否则系统托盘将接收它。

(强调我的)中提到的数据:

在这些情况下,通知会传递到设备的系统托盘,并且数据有效负载会以启动器活动的附加内容的形式提供。

是数据消息有效负载(请参阅消息类型)。

您必须发送包含通知和数据的有效负载。像这样:

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
    },
  "data": {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
  }
}

有关详细信息,请参阅以下链接:

https://firebase.google.com/docs/cloud-messaging/concept-options

请检查您的消息json与上面的内容是否相同。如果不相同,请将其设为相同。经过同样的希望检查后,它对您有用。

如果它有效,那么请关闭这些问题。如果您有任何疑问,请在下面发表评论post.i将帮助您。

答案 1 :(得分:0)

我通过阅读post from Github找到了我的错误。

而不是发送

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
    },
  "data": {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
  }
}

我必须发送

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "data": {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
  }
}

我现在检索我的数据。