问题描述
我有一个iOS应用程序,在应用程序商店中生活。我可以将推送通知发送到安装了该应用程序的iOS设备,但只有当我从Firebase控制台发送推送通知时。当我尝试通过一个cURL请求,来自服务器的响应表明我成功了,但是这个消息在设备上没有收到。我已经尝试了多播和单个收件人的有效载荷。
我必须错过更基本的东西,但我看不到它。 b
以下是我的PHP代码:
<?php
// API访问Google API控制台的密码
define('API_ACCESS_KEY','AI ***** 4LPGkx8xtDG2tBl ***** 7KWJfmp1szcA');
$ registrationIds = array($ _GET ['id']);
//准备捆绑包
$ msg =数组
(
'message'=>'这里是一条消息message',
'title'=> ;'这是一个标题'title'
);
$ fields =数组
N4H-cAD6ahY7mQNYZtEJLAIE,
'data'=> $ msg
);
$ header = 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;
以下是运行此代码时得到的响应:
有两个问题: b
$ b
- 我需要在有效负载中包含
通知
部分,而不是data
- 不知何故,有效载荷未被PHP正确格式化。
shell_exec()来通过SSH进行cURL请求。这是不理想的,但它完成了工作。
示例代码:
shell_exec('curl -X POST -headerAuthorization:key =< key here>--headerContent-Type:application / jsonhttps://fcm.googleapis.com/fcm/send -d{\to \:\'。$ to.'\,\priority \:\high \,\notification \:{ \body \:\'。stripslashes($ message)。'\}}');
I have an iOS app which is live on the app store. I am able to send push notifications to iOS devices which have the app installed, but only when I send them from the Firebase console.
When I try to send push notifications via a cURL request, the response from the server indicates that I was successful but the message isn't received on the device. I have tried this with both multicast and single recipient payloads.
I must be missing something more fundamental, but I can't see it.
Here is my PHP code:
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AI*****4LPGkx8xtDG2tBl*****7KWJfmp1szcA' );
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'message' => 'here is a message. message',
'title' => 'This is a title. title'
);
$fields = array
(
'to' => "cUxd-iTVVWo:APA*****kQTuqJ5RREKhhlJjm27NCuVfUn5APg3lBFqh-YWjgx*****iOpAQeLB14CzM2YTwIJo_75jzCmbFLKj0_zpKSHvAEbmJz*****BBezGJIng-N4H-cAD6ahY7mQNYZtEJLAIE",
'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;
Here is the response I get when running this code:
There were two issues:
- I needed to include a
notification
section in the payload instead ofdata
- Somehow the payload wasn't being formatted properly by PHP.
In the end I used the PHP function shell_exec()
to do a cURL request over SSH instead. This isn't ideal but it got the job done.
Example code:
shell_exec('curl -X POST --header "Authorization: key=<key here>" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"'.$to.'\",\"priority\":\"high\",\"notification\":{\"body\": \"'.stripslashes($message).'\"}}"');
这篇关于通过cURL / PHP发送的设备上未收到Firebase通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!