最近我从 GCM 迁移到 FCM,过去几天我一直在努力让它工作。
Android 应用程序正在接收来自 google firebase 控制台的通知,但它们没有来自 php 服务器。
这是我的 PHP 服务器端代码:
<?php
define("GOOGLE_API_KEY", Setting::get('browser_key') ? Setting::get('browser_key') : "");
class GCM {
function __construct() {
}
public function send_notification($registatoin_ids, $message) {
Log::info("GOOGLE_API_KEY".GOOGLE_API_KEY);
include_once 'const.php';
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'data' => $message,
);
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
Log::info('***** PUSH MESSAGE ******'.print_r(json_encode($fields),true));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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);
Log::info(print_r($result,true));
if ($result === FALSE) {
//die('Curl failed: ' . curl_error($ch));
Log::error('Curl failed: ' . curl_error($ch));
}
else{
//echo $result;
Log::error($result);
}
// Close connection
/*curl_close($ch);
echo $result/*."\n\n".json_encode($fields); */
}
}
?>
这是我的 const.php
<?php
define('TEAM','team');
define('MESSAGE' , 'message');
?>
这是我的 Firebase 消息传递代码:
public class MessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Message";
public MessagingService() {
super();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
sendNotification(remoteMessage.getNotification().getBody());
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification());
}
}
宝慕林4294392
拉风的咖菲猫
哆啦的时光机