Remote Notification push backend code (PHP and Java)

Source: Internet
Author: User

This file is a remote notification server code, do not use his

The following is the code for writing server-side PHP

###### @begin # # #

<?php

$deviceToken = ' 38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7ce90d56e9 fe145bcc 6c2c594b '; Mobile-to-server 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, $, 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

The following is a Java-written server-side code

###### @begin # # #

public static void Main (string[] args) throws Exception

{

Try

{

The Devicetoken obtained from the client, in order to test the simple, write a fixed test device identity.

String Devicetoken = "Df779eda 73258894 5882ec78 3ac7b254 6ebc66fe fa295924 440d34ad 6505f8c4"

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

Defining Message Patterns

PayLoad PayLoad = new PayLoad ();

Payload.addalert ("This is test!");

Payload.addbadge (1);//The number of message push marks, the number shown in the small red circle.

Payload.addsound ("Default");

Register Devicetoken

Pushnotificationmanager Pushmanager = Pushnotificationmanager.getinstance ();

Pushmanager.adddevice ("IPhone", Devicetoken);

Connection APNs

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

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

int port = 2195;

String Certificatepath = "C:/PUSHTEST.P12";//*.P12 file location generated earlier for the Java Background Connection APNs service

String Certificatepassword = "123456";//p12 file password.

Pushmanager.initializeconnection (host, Port, Certificatepath, Certificatepassword, sslconnectionhelper.keystore_ TYPE_PKCS12);

Send push

Device client = Pushmanager.getdevice ("IPhone");

SYSTEM.OUT.PRINTLN ("Push message:" + client.gettoken () + "\ n" +payload.tostring () + "");

Pushmanager.sendnotification (client, payLoad);

Stop Connection APNs

Pushmanager.stopconnection ();

Delete Devicetoken

Pushmanager.removedevice ("IPhone");

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

}

catch (Exception ex)

{

Ex.printstacktrace ();

}

}

@end

Remote Notification push backend code (PHP and Java)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.