Skip to content

Instantly share code, notes, and snippets.

@brainv
Forked from sawapi/AppDelegate.swift
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save brainv/d381b25d6b80c5bdcf46 to your computer and use it in GitHub Desktop.

Select an option

Save brainv/d381b25d6b80c5bdcf46 to your computer and use it in GitHub Desktop.
<?php
$deviceToken = '************';
// 送信する文字列
$alert = 'Push test.';
// バッジ
$badge = 1;
$body = array();
$body['aps'] = array( 'alert' => $alert );
$body['aps']['badge'] = $badge;
// SSL証明書
$cert = '********.pem';
$url = 'ssl://gateway.sandbox.push.apple.com:2195'; // 開発用
//$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 );
//
// AppDelegate.swift
// pushtest
//
// Created by sawapi on 2014/06/08.
// Copyright (c) 2014年 sawapi. All rights reserved.
//
// iOS8用
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application( application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary? ) -> Bool {
// Push通知を許可する
var types: UIUserNotificationType = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound
var settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
application.registerUserNotificationSettings( settings )
application.registerForRemoteNotifications()
return true
}
// デバイストークンを取得する
func application( application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData! ) {
// <>と" "(空白)を取る
var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
var deviceTokenString: String = ( deviceToken.description as NSString )
.stringByTrimmingCharactersInSet( characterSet )
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
println( deviceTokenString )
}
// デバイストークンの取得に失敗した場合
func application( application: UIApplication!, didFailToRegisterForRemoteNotificationsWithError error: NSError! ) {
println( error.localizedDescription )
}
func applicationWillResignActive( application: UIApplication ) {
}
func applicationDidEnterBackground( application: UIApplication ) {
}
func applicationWillEnterForeground( application: UIApplication ) {
}
func applicationDidBecomeActive( application: UIApplication ) {
}
func applicationWillTerminate( application: UIApplication ) {
}
}
//
// AppDelegate.swift
// pushtest
//
// Created by sawapi on 2014/06/08.
// Copyright (c) 2014年 sawapi. All rights reserved.
//
// iOS7以下
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application( application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary? ) -> Bool {
application.registerForRemoteNotificationTypes( UIRemoteNotificationType.Badge |
UIRemoteNotificationType.Sound |
UIRemoteNotificationType.Alert )
return true
}
// デバイストークンを取得する
func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
// <>と" "(空白)を取る
var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
var deviceTokenString: String = ( deviceToken.description as NSString )
.stringByTrimmingCharactersInSet( characterSet )
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
println( deviceTokenString )
}
func applicationWillResignActive( application: UIApplication ) {
}
func applicationDidEnterBackground( application: UIApplication ) {
}
func applicationWillEnterForeground( application: UIApplication ) {
}
func applicationDidBecomeActive( application: UIApplication ) {
}
func applicationWillTerminate( application: UIApplication ) {
}
}
<?php
// Put your device token here (without spaces):
$deviceToken = '0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78';
// Put your private key's passphrase here:
$passphrase = 'pushchat';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$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.com: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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment