Skip to content

Instantly share code, notes, and snippets.

@nnsnodnb
Last active June 21, 2017 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnsnodnb/8f6c6d57a00baf702460a2670df07065 to your computer and use it in GitHub Desktop.
Save nnsnodnb/8f6c6d57a00baf702460a2670df07065 to your computer and use it in GitHub Desktop.
Apple Push Notification Service Sample codes
var http = require('http');
var server = http.createServer();
var apns = require('apn');
var options = {
cert: './cert.pem',
key: './key.pem',
gateway: 'gateway.sandbox.push.apple.com',
port: 2195,
};
var token = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87';
var device = apns.Device(token);
server.on('request', function(req, res) {
var apnsConnection = new apns.Connection(options);
var note = new apns.Notification();
note.badge = 2;
note.alert = 'Title';
note.device = device;
// note.contentAvailable = 1;
note.priority = 10;
note.sound = 'default';
apnsConnection.sendNotification(note);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Send notification');
res.end();
});
server.listen(3000, '127.0.0.1');
console.log('start server');
<?php
$deviceToken = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87';
$body = array();
$body['aps']['alert']['title'] = 'Title';
$body['aps']['alert']['subtitle'] = 'Sub Title';
$body['aps']['alert']['body'] = 'Body';
$body['aps']['sound'] = 'default';
$body['aps']['badge'] = 1;
$body['aps']['mutable-content'] = 1;
$cert = 'aps_development.pem';
//$cert = 'aps_production.pem';
$url = 'ssl://gateway.sandbox.push.apple.com:2195'; // Use sandbox
//$url = 'ssl://gateway.push.apple.com:2195';
$context = stream_context_create();
stream_context_set_option( $context, 'ssl', 'local_cert', $cert );
$fp = stream_socket_client( $url, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $context );
if( !$fp ) {
echo 'Failed to connect.' . PHP_EOL;
exit( 1 );
}
$payload = json_encode( $body );
$message = chr( 0 ) . pack( 'n', 32 ) . pack( 'H*', $deviceToken ) . pack( 'n', strlen($payload ) ) . $payload;
print 'send message:' . $payload . PHP_EOL;
fwrite( $fp, $message );
fclose( $fp );
echo 'end';
"""
$ pip install apns3
"""
# coding: utf-8
import time
from apns3 import APNs, Frame, Payload, PayloadAlert
apns = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem')
# Send a notification
token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87'
payload = Payload(alert="Hello World!", sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)
# Send multiple notifications in a single transmission
frame = Frame()
identifier = 1
expiry = int(time.time() + 3600)
priority = 10
frame.add_item('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87', payload, identifier, expiry, priority)
apns.gateway_server.send_notification_multiple(frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment