How to Send push notifications using One Signal + PHP + Server API?

后端 未结 4 1943
青春惊慌失措
青春惊慌失措 2020-12-25 10:30

I am using one signal to send push notification for android app. My question is

How Can I setup send push notifications using server rest api?

相关标签:
4条回答
  • 2020-12-25 10:42

    $to - Device ID

    $title - Notification Title

    $message - Notification Message

    $img - Full image link

    Usage:

    sendnotification($to, $title, $message, $img);

    With Demo Values :

    sendnotification("Device_ID","Test Notification","Test Message","https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png");

    function sendnotification($to, $title, $message, $img)
    {
        $msg = $message;
        $content = array(
            "en" => $msg
        );
        $headings = array(
            "en" => $title
        );
        if ($img == '') {
            $fields = array(
                'app_id' => 'YOUR_APP_ID',
                "headings" => $headings,
                'include_player_ids' => array($to),
                'large_icon' => "https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",
                'content_available' => true,
                'contents' => $content
            );
        } else {
            $ios_img = array(
                "id1" => $img
            );
            $fields = array(
                'app_id' => 'YOUR_APP_ID',
                "headings" => $headings,
                'include_player_ids' => array($to),
                'contents' => $content,
                "big_picture" => $img,
                'large_icon' => "https://www.google.co.in/images/branding/googleg/1x/googleg_standard_color_128dp.png",
                'content_available' => true,
                "ios_attachments" => $ios_img
            );
    
        }
        $headers = array(
            'Authorization: key=**APP_KEY**',
            'Content-Type: application/json; charset=utf-8'
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications');
        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);
        return $result;
    }
    
    0 讨论(0)
  • 2020-12-25 10:58
    <?PHP
    function sendMessage(){
        $content = array(
            "en" => 'Testing Message'
            );
    
        $fields = array(
            'app_id' => "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
            'included_segments' => array('All'),
            'data' => array("foo" => "bar"),
            'large_icon' =>"ic_launcher_round.png",
            'contents' => $content
        );
    
        $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 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
        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);
    
        return $response;
    }
    
    $response = sendMessage();
    $return["allresponses"] = $response;
    $return = json_encode( $return);
    print("\n\nJSON received:\n");
    print($return);
    print("\n");
    ?>
    
    0 讨论(0)
  • 2020-12-25 11:00

    I see you have set isAndroid=true, but OneSignal is returning an error that shows that the application with ID eec33e8e-5774-4b74-9aae-37370778c4b2 does not have Android notifications enabled.

    Make sure your app ID is correct, and if it is, that Android notifications are enabled in your OneSignal settings.

    0 讨论(0)
  • 2020-12-25 11:01

    You can always refer to the official docs:

    https://documentation.onesignal.com/reference#section-example-code-create-notification

    • 'app_id' is currently known as (OneSignal App ID) in the OneSignal Settings->Keys and IDs

    • in 'Authorization: Basic xxx...' past the "REST API Key" just below the App ID

    0 讨论(0)
提交回复
热议问题