2017-02-09 37 views
0

響應表示通知已成功,但未在我的Android手機上顯示。Firebase通知不會顯示使用PHP

響應:

{"multicast_id":6696507350475373329,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1486651908616097%298b4204f9fd7ecd"}]} 

PHP(SRC:https://gist.github.com/prime31/5675017

<?php 
// API access key from Google API's Console 
define('API_ACCESS_KEY', ThisIsMyKey); 
$registrationIds = array($_GET['id']); 
// prep the bundle 
$msg = array 
(
    'message' => 'TestNachricht', 
    'title'  => 'TestTitel', 
    'subtitle' => 'TestSubTitle', 
    'vibrate' => 1, 
    'sound'  => 1, 
    'priority' => 10, 
    'smallIcon' => 'small_icon' 
); 
$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; 

安卓

我沒加的onReceive的方法,因爲我能夠播放notifcations併發送通知給特定設備(RegistrationId)使用FireBase控制檯。所有這些都顯示在我的設備上。

private String getRegistrationId() { 
     return FirebaseInstanceId.getInstance().getToken(); 
} 

    <service android:name=".MyFirebaseInstanceIdService" android:exported="true"> 
     <intent-filter> 
      <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
     </intent-filter> 
    </service> 
    <service android:name=".MyFirebaseMessagingService"> 
     <intent-filter> 
      <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
     </intent-filter> 
    </service> 
+0

喜戴夫。你還可以添加你的Android端代碼來接收消息嗎? –

+0

我編輯了我的帖子。 – Dave

回答

1

當通過Firebase控制檯發送推送通知時,它被分類爲notification消息有效負載。默認情況下,Android會在您的應用程序處於後臺時處理此消息。

在您的PHP代碼中,您只發送一個data - 僅限消息有效內容,可以通過執行onMessageReceived()在Android中處理。

如果您仍然不想實施onMessageReceived(),則必須修改您的PHP端代碼才能使用notification-僅限消息有效負載。

請注意,notification有效負載僅具有可使用的預定義參數。

更多細節在這裏: