Skip to content

Instantly share code, notes, and snippets.

@sawapi
Last active October 25, 2023 09:26
Show Gist options
  • Select an option

  • Save sawapi/a7cee65e4ad95578044d to your computer and use it in GitHub Desktop.

Select an option

Save sawapi/a7cee65e4ad95578044d to your computer and use it in GitHub Desktop.

Revisions

  1. sawapi revised this gist Jun 12, 2014. 2 changed files with 75 additions and 5 deletions.
    24 changes: 19 additions & 5 deletions AppDelegate.swift
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    // Created by sawapi on 2014/06/08.
    // Copyright (c) 2014年 sawapi. All rights reserved.
    //
    // iOS8用

    import UIKit

    @@ -16,15 +17,21 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

    func application( application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary? ) -> Bool {

    application.registerForRemoteNotificationTypes( UIRemoteNotificationType.Badge |
    UIRemoteNotificationType.Sound |
    UIRemoteNotificationType.Alert )
    // 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 ) {
    func application( application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData! ) {

    // <>と" "(空白)を取る
    var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
    @@ -36,6 +43,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
    println( deviceTokenString )

    }

    // デバイストークンの取得に失敗した場合
    func application( application: UIApplication!, didFailToRegisterForRemoteNotificationsWithError error: NSError! ) {

    println( error.localizedDescription )
    }

    func applicationWillResignActive( application: UIApplication ) {
    }
    @@ -51,4 +64,5 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

    func applicationWillTerminate( application: UIApplication ) {
    }
    }
    }

    56 changes: 56 additions & 0 deletions ios7_AppDelegate.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    //
    // 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 ) {
    }
    }
  2. sawapi revised this gist Jun 8, 2014. 1 changed file with 0 additions and 0 deletions.
    Binary file modified screenshot_push.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  3. sawapi revised this gist Jun 8, 2014. 1 changed file with 0 additions and 0 deletions.
    Binary file added screenshot_push.png
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  4. sawapi created this gist Jun 8, 2014.
    54 changes: 54 additions & 0 deletions AppDelegate.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    //
    // AppDelegate.swift
    // pushtest
    //
    // Created by sawapi on 2014/06/08.
    // Copyright (c) 2014年 sawapi. All rights reserved.
    //

    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 ) {
    }
    }
    37 changes: 37 additions & 0 deletions apns.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <?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 );