我構建了一個Ionic 2,我必須整合推送通知。 首先,我嘗試通過firebase界面發送我的通知: https://console.firebase.google.com/ 一切正常。Ionic v2:FCM在後臺打開應用程序
但現在,我想發送通知與PHP服務器。所以我發現這個代碼:
<?php
// API access key from Google API's Console
define('API_ACCESS_KEY', 'my-key');
$registrationIds = array($_GET['id']);
// prep the bundle
$msg = array
(
'message'=> 'here is a message. message',
'title'=> 'This is a title. title',
'subtitle'=> 'This is a subtitle. subtitle',
'tickerText'=> 'Ticker text here...Ticker text here...Ticker text here',
'vibrate'=>1,
'sound'=>1,
'largeIcon'=>'large_icon',
'smallIcon'=>'small_icon',
'content-available'=>1
);
$fields = array
('registration_ids'=>$registrationIds,
'data'=>$msg
);
$headers = array
('Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
它的工作原理。但是當我的應用程序處於後臺(onpause)時,我收到通知,但觸摸它不會在前臺打開我的應用程序。 當我的應用程序關閉時效果很好 恢復:
- 如果我的應用程序關閉,我收到通知。當我觸摸通知時,我的應用已打開。
- 如果我的應用程序處於打開狀態並且處於前景中,我收到通知。
- 如果我的應用暫停,但在後臺打開,我收到通知,但觸摸它並不打開應用。但是我要。
我不明白,因爲它適用於Firebase控制檯... 我需要做什麼來解決這個問題?在app.component.ts
我的回調代碼:
this.push.rx.notification()
.subscribe((msg) => {
console.log('I received awesome push: ' + msg);
});
UPDATE:
當我觸摸我的通知,如果我的應用程序 「的onPause」,她進入forground和背部直接進入後臺。我不知道爲什麼,我不知道我必須做的......
您可以添加回調代碼,當您在應用程序中收到通知時調用該代碼? – Bills
當然,我做到了。 –