简体   繁体   中英

Sending Push notification through pushwoosh from php

I am trying to send push notification through push woosh such like this :

is anybody help me how to send push notification on device from this code

function pwCall( 'createMessage' , array(

        'application' => PW_APPLICATION,          

        'auth' => PW_AUTH,

        "devices" => PW_DEVICETOKEN,

        'notifications' =>  array(

                    'send_date' =>'now', //gmdate('d-m-Y H:i', strtotime('2014-04-07 20:35')),

                    'content' => 'my custom notification testing ',

    'link' => 'http://pushwoosh.com/',

                    'content' => array("en" => "English","ru" =>"Русский","de"=>"Deutsch")

                    ),

                  'page_id' => 16863,

                  'link' => 'http://google.com',

                 'data' => array( 'custom' => 'json data' ),   
            )
    );

I am getting error such as

Array ( [status_code] => 210 [status_message] => Cannot parse date [response] => )

  1. notifications should be array of objects in JSON notation. In PHP it will be array of arrays. This is because you can create multiple notifications in one request. final JSON for notifications field:

    "notifications":[{ ... notification properties... }, { ... second notification properties ... }, ...]

  2. There are only 3 root parameters in request: application (OR applications_group ), auth , and notifications . Other parameters are parameters of notification, not request.

Finally your PHP call should be like following:

pwCall("createMessage", array(
  "auth" => PW_AUTH,
  "application" => PW_APPLICATION,
  "notifications" => array(
    array(
      "send_date" => "now",
      "content" => array("en" => "English", "ru" =>"Русский", "de"=>"Deutsch"),
      "link" => "http://pushwoosh.com/",
      "page_id" => 16863,
      "devices" => array( PW_DEVICETOKEN ),
      "data" => array( "custom" => "json data" )
    )
  )
));

All notification's fields except of send_date and content are optional and can be omited

By the looks of it, your date is not formatted correctly. You're passing an ordinary string consisting of the word "now". What you'll want to do is something along the lines of the following:

function pwCall("createMessage", array(
    "application"   => PW_APPLICATION,          
    "auth"          => PW_AUTH,
    "devices"       => PW_DEVICETOKEN,
    "notifications" => array(
        "send_date" => gmdate("Y-m-d H:i"),
        "content"   => "My custom notification",
        "link"      => "http://pushwoosh.com/",
        "content"   => array("en" => "English", "ru" =>"Русский", "de"=>"Deutsch")
    ),
    "page_i" => 16863,
    "link"   => "http://google.com",
        "data" => array("custom" => "json data"),   
    )
);

we've developped an API to easily call the Pushwoosh Web Services.

This API should be a good quality one and is fully tested (very high code coverage).

https://github.com/gomoob/php-pushwoosh

$push_auth = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$push_app_id = 'XXXXX-XXXXX';
$push_debug = false;
$title = ''; // pushwoosh title
$banner = ''; // pushwoosh banner
$send_date = 'now'; // pushwoosh date
$android_header = '';    // pushwoosh android header 
$android_custom_icon = '' pushwoosh notification icon;
sendpush('createMessage', array(
                    'application' => $push_app_id,
                    'auth' => $push_auth,
                    'notifications' => array(
                        array(
                            'send_date' => $send_date,
                            'content' => $title,
                            'android_header'=>$android_header,
                            'android_custom_icon' =>$android_custom_icon,
                            'android_badges' => 2,
                            'android_vibration' => 1,                
                            'android_priority' => 1,
                            'data' => array('custom' => 'json data'),
                        ),
                    )
        ));

function sendpush($method, $data) {
            $url = 'https://cp.pushwoosh.com/json/1.3/' . $method;
            $request = json_encode(['request' => $data]);

            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $request);

            $response = curl_exec($ch);            
            $info = curl_getinfo($ch);
            curl_close($ch);

            if (defined('PW_DEBUG') && self::$push_de) {
                print "[PW] request: $request\n";
                print "[PW] response: $response\n";
                print "[PW] info: " . print_r($info, true);
            }

            return $info;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM