php - 通过 cURL/PHP 发送时,设备未收到 Firebase 通知

标签 php ios curl firebase firebase-cloud-messaging

我有一个 iOS 应用程序,它在应用程序商店上线。我能够向安装了应用程序的 iOS 设备发送推送通知,但只有当我从 Firebase 控制台发送它们时。

当我尝试通过 cURL 请求发送推送通知时,服务器的响应表明我成功了,但设备上没有收到消息。我已经对多播和单个接收者有效负载进行了尝试。

我一定是遗漏了一些更基本的东西,但我看不到。

这是我的 PHP 代码:

<?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;

这是我在运行这段代码时得到的响应:

{"multicast_id":5814921248239922706,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1476193002715692%a4ddee3cf9fd7ecd"}]}

最佳答案

有两个问题:

  1. 我需要在负载中包含一个 notification 部分而不是 data
  2. 不知何故,有效载荷没有被 PHP 正确格式化。

最后,我使用 PHP 函数 shell_exec() 通过 SSH 执行 cURL 请求。这并不理想,但它完成了工作。

示例代码:

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).'\"}}"');

关于php - 通过 cURL/PHP 发送时,设备未收到 Firebase 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39979235/

相关文章:

php - YouTube iframe API 触发所有事件两次

ios - 如何在 Objective-C 中确定相同的 NSString?

RCurl 和自签名证书问题

linux - 在 Linux 脚本中接收 "curl: (3) Illegal characters found in URL"

ios - 界面中的未知类 FBLoginView

ios - 使用最新的 iOS SDK 编译时,libcurl 在 iOS 9 上崩溃

PHP - 使用 session.use_trans_sid 时 session ID 发生变化

php - Symfony 1.4 Doctrine fixtures加载单个文件?

php - WebSocket 握手不起作用

iphone 3G 上的 IOS 5 sdk 更新问题