码迷,mamicode.com
首页 > 编程语言 > 详细

远程通知推送后台代码( PHP 和 java )

时间:2015-01-19 14:12:40      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

//  本文件为远程通知服务端代码,不做他用

 

// 以下是以 PHP 为写服务器端的代码

// ######    @begin   ###

<?php

 

$deviceToken = ‘38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7ce90d56e9 fe145bcc 6c2c594b‘; // 手机端传给服务器的deviceToken

// Passphrase for the private key (ck.pem file)

// $pass = ‘‘;

// Get the parameters from http get or from command line

$message = $_GET[‘message‘] or $message = $argv[1] or $message = ‘Message received from javacom‘;

$badge = (int)$_GET[‘badge‘] or $badge = (int)$argv[2];

$sound = $_GET[‘sound‘] or $sound = $argv[3];

// Construct the notification payload

$body = array();

$body[‘aps‘] = array(‘alert‘ => $message);

if ($badge)

$body[‘aps‘][‘badge‘] = $badge;

if ($sound)

$body[‘aps‘][‘sound‘] = $sound;

 

 

$ctx = stream_context_create();

stream_context_set_option($ctx, ‘ssl‘, ‘local_cert‘, ‘ck.pem‘);

// assume the private key passphase was removed.

// stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $pass);

$fp = stream_socket_client(‘ssl://gateway.sandbox.push.apple.com:2195‘, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

if (!$fp) {

    print "Failed to connect $err $errstrn";

    return;

}

else {

    print "Connection OKn";

}

$payload = json_encode($body);

$msg = chr(0) . pack("n",32) . pack(‘H*‘, str_replace(‘ ‘, ‘‘, $deviceToken)) . pack("n",strlen($payload)) . $payload;

print "sending message :" . $payload . "n";

fwrite($fp, $msg);

fclose($fp);

?>

 

//  @end

 

 

// 以下是以 java 为写服务器端的代码

// ######    @begin   ###

 

public static void main(String[] args) throws Exception

{

    try

    {

        //从客户端获取的deviceToken,在此为了测试简单,写固定的一个测试设备标识。

        String deviceToken = "df779eda 73258894 5882ec78 3ac7b254 6ebc66fe fa295924 440d34ad 6505f8c4"

        System.out.println("Push Start deviceToken:" + deviceToken);

        //定义消息模式

        PayLoad payLoad = new PayLoad();

        payLoad.addAlert("this is test!");

        payLoad.addBadge(1);//消息推送标记数,小红圈中显示的数字。

        payLoad.addSound("default");

        //注册deviceToken

        PushNotificationManager pushManager = PushNotificationManager.getInstance();

        pushManager.addDevice("iPhone", deviceToken);

        //连接APNS

        String host = "gateway.sandbox.push.apple.com";

        //String host = "gateway.push.apple.com";

        int port = 2195;

        String certificatePath = "c:/PushTest.p12";//前面生成的用于JAVA后台连接APNS服务的*.p12文件位置

        String certificatePassword = "123456";//p12文件密码。

        pushManager.initializeConnection(host, port, certificatePath, certificatePassword, SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);

        //发送推送

        Device client = pushManager.getDevice("iPhone");

        System.out.println("推送消息: " + client.getToken()+"\n"+payLoad.toString() +" ");

        pushManager.sendNotification(client, payLoad);

        //停止连接APNS

        pushManager.stopConnection();

        //删除deviceToken

        pushManager.removeDevice("iPhone");

        System.out.println("Push End");

    }

    catch (Exception ex)

    {

        ex.printStackTrace();

    }

}

 

 

// @end

远程通知推送后台代码( PHP 和 java )

标签:

原文地址:http://www.cnblogs.com/weixhe/p/4233612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!