PHPでpush通知を打つまでの流れ
iOS/Androidのpush通知
言葉
主にPHPのライブラリを利用しています(apns-php)
https://code.google.com/p/apns-php/wiki/CertificateCreation sample_push.phpを利用
この記事を読めばイメージがつかめます
参考
- https://support.apple.com/ja-jp/HT203609
- http://takalife.com/blog/tech/247/
- https://github.com/immobiliare/ApnsPHP/blob/master/sample_push.php
- https://github.com/davibennun/laravel-push-notification
必要な物
- iOS Dev Center へのアカウント
- Dev Center で発行した証明書
- apns-phpで利用するもう一つの証明書 (entrustを利用しています)
- 確認する為の実機
- XCode 7.x (Swift2.x)
参考URLのざっくり手順
- iOS Dev center でApp Ids でアプリを登録
- Push Notifications を利用するにチェック
- 登録したAppを選択してDevelopment SSL Certificate > Create xxx
- mac で CSR作成してアップロード
- Development SSL Certificate 作成した証明書をダウンロード
- Wクリックすると、キーチェーンに取り込まれる
- キーチェーンアシスタントで左上:ログイン 左下:証明書 Apple Development iOS Push Service
を右クリック - .p12で書き出す 書き出す時にpassを指定、または指定なしも可
- .p12だけでも運用出来るような記事
- http://qiita.com/takasan/items/2e9b1f7c3f78ea093195
- .p12 > pemを作成する
- openssl pkcs12 -in dev_push.p12 -out sample.pem -nodes -clcerts -passin pass:<passを指定していたら>
- openssl pkcs12 -in dev_push.p12 -out sample.pem -nodes -clcerts -passin pass: <=passなしの場合
- ここでpassを入力してもcan't read file とかで怒られてはまった -passinオプションで逃げた
端末に通知が届かない場合に確認する項目(ハマった)
iOS Dev Centerで登録したAppIdsのIDと一致しているか?
- product > general > bundle identifier
アプリのPush通知利用がON になっているか?
- product > Capabilities > Push Notifications
Provisioning Profile が iOS Dev Centerで登録したアプリのプロファイルになっているか?
- product > Capabilities > Build Settings > Code Signing
Tips
- iOS9から、アプリインストール時に毎回DeviceTokenが新規発行される事になったようです
- 以前はApple様のタイミングでDeviceTokenが変わっていました
便利なライブラリ
- apns-phpに関する記事
push を受けるだけのサンプルアプリ
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. // device token の発行 let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) application.registerUserNotificationSettings(settings) application.registerForRemoteNotifications() return true } // device tokenの発行に成功したら呼ばれる func application( application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) { // <>と" "(空白)を取る let characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" ) let deviceTokenString: String = ( deviceToken.description as NSString ) .stringByTrimmingCharactersInSet( characterSet ) .stringByReplacingOccurrencesOfString( " ", withString: "" ) as String print( deviceTokenString ) } // 起動中 通知からの起動時に呼ばれる func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { //print("application:didReceiveRemoteNotification: " + userInfo.description); switch application.applicationState { case .Active: // 起動中 print("active") break case .Inactive: // バックグラウンド中で、通知タッチでフォアへ print("inactive") break default: break } }
push発行のサーバサイド(apns-php)
<?php /** * @file * sample_push.php * * Push demo * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://code.google.com/p/apns-php/wiki/License * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to aldo.armiento@gmail.com so we can send you a copy immediately. * * @author (C) 2010 Aldo Armiento (aldo.armiento@gmail.com) * @version $Id: sample_push.php 65 2010-12-13 18:38:39Z aldo.armiento $ */ // Adjust to your timezone date_default_timezone_set('Asia/Tokyo'); // Report all PHP errors error_reporting(-1); // Using Autoload all classes are loaded on-demand require_once 'ApnsPHP/Autoload.php'; // Instanciate a new ApnsPHP_Push object $push = new ApnsPHP_Push( ApnsPHP_Abstract::ENVIRONMENT_SANDBOX, 'p12から作成した.pem' // 1つめの証明書 ); // Set the Root Certificate Autority to verify the Apple remote peer $push->setRootCertificationAuthority('entrust.pem'); // entrustからダウンロードした2つめの証明書 //$push->setProviderCertificatePassphrase('pemにpass phraseがあれば利用'); //$push->setProviderCertificatePassphrase(''); //pemをつくるときに指定したパスワード passなし // Connect to the Apple Push Notification Service $push->connect(); // Instantiate a new Message with a single recipient $message = new ApnsPHP_Message('c8c689e14fcfe31bf1bca8754c51e6235b1eae3a0297e552d0872aee9fb9868d'); // Device Token <>スペースを除く // Set a custom identifier. To get back this identifier use the getCustomIdentifier() method // over a ApnsPHP_Message object retrieved with the getErrors() message. $message->setCustomIdentifier("Message-Badge-3"); // Set badge icon to "3" $message->setBadge(3); // Set a simple welcome text $message->setText('Hello APNs-enabled device!あいうえおやっほお=ー'); // Play the default sound $message->setSound(); // Set a custom property $message->setCustomProperty('acme2', array('bang', 'whiz')); // Set another custom property $message->setCustomProperty('acme3', array('bing', 'bong')); // Set the expiry value to 30 seconds $message->setExpiry(30); // Add the message to the message queue $push->add($message); // Send all messages in the message queue $push->send(); // Disconnect from the Apple Push Notification Service $push->disconnect(); // Examine the error message container $aErrorQueue = $push->getErrors(); if (!empty($aErrorQueue)) { var_dump($aErrorQueue); }