2017-02-09 39 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有效负载仅具有可使用的预定义参数。

更多细节在这里: