如何使用PHP发送一个大图像信号推送通知

时间:2018-03-14 06:37:37

标签: php onesignal

代码只发送一个标题和消息,但图标没有收到。我是一个信号api的新手。我的代码是

<?php 
   $fields = array(
            'app_id' => 'my app id',
            'include_player_ids' => ['player_id'],
            'contents' => array("en" =>"test message"),
            'headings' => array("en"=>"test heading"),
            'largeIcon' => 'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github-512.png',
            );

 $fields = json_encode($fields);
 //print("\nJSON sent:\n");
 //print($fields);

$ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                       'Authorization: Basic M2ZNDYtMjA4ZGM2ZmE5ZGFj'));
                                       curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                                       curl_setopt($ch, CURLOPT_HEADER, FALSE);
                                       curl_setopt($ch, CURLOPT_POST, TRUE);
                                       curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
                                       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

                                       $response = curl_exec($ch);
                                       curl_close($ch);
                                       print_r($response);
?>

提前致谢

3 个答案:

答案 0 :(得分:1)

您只需添加: “chrome_web_image”=&gt; “image url”;

$fields = array(
            'app_id' => 'my app id',
            'include_player_ids' => ['player_id'],
            'contents' => array("en" =>"test message"),
            'headings' => array("en"=>"test heading"),
            'chrome_web_image' => 'https://cdn4.iconfinder.com/data/icons/iconsimple-logotypes/512/github-512.png',
            );

要进一步阅读,请访问:doc appearance of onesignal

结果: image result

答案 1 :(得分:0)

图标的关键字应为large_icon而不是largeIcon

检查the API for more details

答案 2 :(得分:-1)

您可以使用ios_attachments参数,该参数必须位于数组中。

这是我的方法:

$iCon = array(  
    "ios_attachments" => 'https://Your.Image.png'
);
 
$fields = array(     
    // OneSignal - Personal ID
    'app_id' => "Your_OneSignal_ID",
    'included_segments' => array('All'),'data' => array("foo" => "bar"),
        
    // Submitting Title && Message && iCon
    'headings' => $headings,
    'contents' => $content,
    'ios_attachments' => $iCon,
);