【发布时间】:2016-11-19 06:40:45
【问题描述】:
好的,所以我到处寻找,但找不到答案。 我已经在我的 Android 应用程序中实现了推送通知,并且在应用程序处于活动状态(前台或后台)时一切正常,但是如果我关闭应用程序,我将停止接收通知。 这是我发送通知的 php 代码。
public static function sendNotification($token){
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'notification' => array("title" => "Test","body" => "Test Message","icon" => "default","sound" => "default"),
"data" => "test message",
'to' => $token
);
$headers = array(
'Authorization:key = AIzaSyAKHM3MoMACjmeVK46TDg8-rTj1KoVjzWs',
'Content-Type: application/json'
);
$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_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
$result = curl_exec($ch);
if($result === FALSE) {
throw new errorSendingNotification();
}
curl_close($ch);
// Result returns this
// {"multicast_id":8978533958735781479,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1468627796530714%7c0e4bee7c0e4bee"}]}
return $result;
}
【问题讨论】:
-
您的应用中有通知服务吗?
-
我也面临 FCM 的问题,请问在收到您在评论行中提到的回复后,请问如何处理该回复
-
我没有为我的应用创建任何后端服务器,只是在我的 MainActivity 中调用 api
标签: java php android curl push-notification