0

我構建了一個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和背部直接進入後臺。我不知道爲什麼,我不知道我必須做的......

+0

您可以添加回調代碼,當您在應用程序中收到通知時調用該代碼? – Bills

+0

當然,我做到了。 –

回答

2

在PHP代碼,你發送一個data - 只有消息負載,使用火力地堡控制檯時,同時,在您發送的消息被認爲是notification消息有效負載(請參閱Message Types)。

嘗試編輯您的PHP代碼,以發送notification消息有效負載。像這樣:

// prep the bundle 
$msg = array 
(
    'title'=> 'This is a title', 
    'body'=> 'This is a body' 
); 
$fields = array 
('registration_ids'=>$registrationIds, 
    'notification'=>$msg 
); 

然而,做注意到,notification消息有效載荷只有特定的參數可用。請參閱HTTP Protocol Reference作爲參數的指南。

+0

太棒了!謝謝 ! :d –

相關問題