首页 文章

关闭应用程序时,我不会收到FCM推送通知 . 离子3和角度

提问于
浏览
1

美好的一天,这是我在网站上的第一个出版物,如果我不遵守所有出版规则,我道歉,因为我不详细了解它们 .

步骤来评论我的问题..我有一个使用离子3和角度的应用程序,它与用户之间的通知工作,以显示一个动作,调查firebase论坛和其他几个地方,并尝试了几个代码,但我无法解决它 . 应用程序将数据和通知发送到特定令牌,令牌注册的设备接收数据但不接收通知 . 不是在应用程序打开时,或者在后台或关闭时 . 这是您使用的代码:

1) To receive the token

this.fcm.getToken (). then (token => {

         alert ('token' + JSON.stringify (token))

         // Here I save the token I receive from the device
      })

这工作..我可以得到设备令牌 .

2) To receive the notification:

this.fcm.onNotification (). subscribe (data => {
        if (data.wasTapped) {
          alert ("Received in background")
        } else {
          alert ("Received in foreground")
        };
      })

这也有效, when I sent a notification from the firebase console I get it perfectly ..问题是我从服务器发送的时候

3) Code for notification:

SendNotific ()
{
let keytoken = 'fqN5yelN7gk: APA91bGT0 ......'
let keyserver = 'AAAA4oCrTNk: APA91bHLBWcyQimF ....'
let headers = {'Authorization': 'key =' + keyserver,
'Content-Type': 'application / json'};
let url = 'https://fcm.googleapis.com/fcm/send';
let body = {
"notification":{
"title": "notification title",
"body": "notification body"
},
"data":{
"key1": "value1",
"key2": "value2"
},
"to": keytoken,
"priority": "high",
"content_available": true
}

this.http.post (url, body, headers) .then (data =>
{
alert (JSON.stringify (data))

}). then (error => {
alert ('error' + JSON.stringify (error))
})
}

If the application is in the foreground of the device ,警报变得可见,也就是说,我收到了"Data"中发送的数据 . (对于这种情况,它将是"key1"和"key2")但我没有收到任何通知 . If the application is in the background or closed ,没有显示通知,当我再次打开应用程序时,警报消息显示为"Data"到达 .

正如我之前所说, if you sent a notification from the Firebase console it works perfectly ..

很明显我错过了一些我找不到的数据..我看到几个出版物谈论同样的事情,但我不能给出解决方案..

我是否会丢失通知中的特定数据,以便可见?

用户是否需要接受任何接收通知的权限?

非常感谢,如果我无法解释清楚,请对不起

问候,

朱利安 .

1 回答

  • 0

    因此,我在代码中注意到的第一件事是 headers 中缺少 Sender . Headers 应该是这样的:

    let headers = {
                   'Authorization': 'key=' + keyserver,
                   'Content-Type': 'application / json',
                   'Sender': 'id=' + senderId
                  };
    

    身体将 totokendevice token 即收件人 . 所以,身体将是这样的:

    let body = {
      "token": keytoken,
      "notification":{
        "title": "notification title",
        "body": "notification body"
      },
      "data":{
        "key1": "value1",
        "key2": "value2"
      },
      "priority": "high",
      "content_available": true
    }
    

    并且,我认为你想宣传 http 并在 http.post 中捕获错误 . 在这种情况下,代码应该类似于:

    this.http.post(url, body, headers).toPromise().then(data =>
    {
      alert (JSON.stringify(data));
    }).catch( error => {
      alert ('error' + JSON.stringify (error));
    })
    

相关问题