最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

I want to send push notification just after publish a new post

programmeradmin0浏览0评论

I would like to send push messages from my website to iOs/android users everytime I publish a new post. Which would be the same as execute php code I think. I have tried to paste a php script which send a push message to my iOs device everytime I launch it, but without success. I've pasted the php code right after this code

<div id="content">

    <?php

    if( have_posts() ):

        get_template_part( 'templates/content/content', str_replace('bw_pt_', '', get_post_type() ) );

In the single.php file in word press->Appearance->editor. As well as I have uploaded the ck.pem file in the same folder. This is my php code and when I launch it from the terminal, it works well. What I need is to add some custom code, which would be a retrieve database to see all device and to send a push notification for each device. I'll do the code later, for the moment I need to know were I have to put this code, in order to be executed everytime the admin press the publish new entry button.

This is my php code:

    <?php

// Put your device token here (without spaces):
$deviceToken = '993c92d21b053c5115d1xxXXXXxxXXXXXxxxXXXXXxxebe9c21f0f59a0723de8bd38';

// Put your private key's passphrase here:
$passphrase = '123456';

// Put your alert message here:
$message = 'Hello world!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);
?>

I would like to send push messages from my website to iOs/android users everytime I publish a new post. Which would be the same as execute php code I think. I have tried to paste a php script which send a push message to my iOs device everytime I launch it, but without success. I've pasted the php code right after this code

<div id="content">

    <?php

    if( have_posts() ):

        get_template_part( 'templates/content/content', str_replace('bw_pt_', '', get_post_type() ) );

In the single.php file in word press->Appearance->editor. As well as I have uploaded the ck.pem file in the same folder. This is my php code and when I launch it from the terminal, it works well. What I need is to add some custom code, which would be a retrieve database to see all device and to send a push notification for each device. I'll do the code later, for the moment I need to know were I have to put this code, in order to be executed everytime the admin press the publish new entry button.

This is my php code:

    <?php

// Put your device token here (without spaces):
$deviceToken = '993c92d21b053c5115d1xxXXXXxxXXXXXxxxXXXXXxxebe9c21f0f59a0723de8bd38';

// Put your private key's passphrase here:
$passphrase = '123456';

// Put your alert message here:
$message = 'Hello world!';

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);
?>
Share Improve this question edited Mar 17, 2015 at 12:05 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Mar 17, 2015 at 11:56 AlfroAlfro 1271 gold badge2 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Wrap it in a function and hook it to transition_post_status:

function wpse_18140_transition_post_status( $new, $old, $post ) {
     if ( $new === 'publish' && $new !== $old ) {
           // Your code here
     }
}

add_action( 'transition_post_status', 'wpse_18140_transition_post_status', 10, 3 );

This will run everytime a post is published, but not when you simply update the post content.

发布评论

评论列表(0)

  1. 暂无评论