Ala*_*ega 4 php java android curl push-notification
好的,所以我到处都看了,找不到答案。我已经在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;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我有同样的问题。这对我有帮助。
请从有效负载中删除“通知”密钥,仅提供“数据”密钥。
应用程序仅在应用程序处于前台时处理通知消息,而即使应用程序在后台或关闭时也处理数据消息。
如果应用程序在前台,则onMessageReceived会同时处理数据和通知消息。如果应用程序关闭或在后台运行,则仅将数据消息传递到onMessageReceived。通知消息不会传递到onMessageReceived。因此,您无法自定义消息。
最好从有效负载中删除通知密钥。
| 归档时间: |
|
| 查看次数: |
5320 次 |
| 最近记录: |