質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

1回答

10063閲覧

ApnsPHPを使ってPush通知をしたい(iOS)

tek_tekeo

総合スコア16

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2016/04/19 02:59

編集2016/04/19 22:19

現在Push通知を受け取ることができるアプリを開発しております。

ApnsPHPというライブラリを用いてpush通知させたいのですが、
以下のように出力されます。

$ php pushTest.php Tue, 19 Apr 2016 11:28:33 +0900 ApnsPHP[11004]: INFO: Trying ssl://gateway.sandbox.push.apple.com:2195... Tue, 19 Apr 2016 11:28:34 +0900 ApnsPHP[11004]: ERROR: Unable to connect to 'ssl://gateway.sandbox.push.apple.com:2195': (0)

StackOverflowに以下のように変更するとあり変更してみましたが結果は同じでした。

php

1$streamContext = stream_context_create(array('ssl' => array( 2//以下1行をコメントアウトする 3//'verify_peer' => isset($this->_sRootCertificationAuthorityFile), 4'cafile' => $this->_sRootCertificationAuthorityFile, 5'local_cert' => $this->_sProviderCertificateFile 6)));

ちなみにrubyではpush通知ができした。以下がコードになります。

ruby

1require 'rubygems' 2require 'apns' 3 4APNS.host = 'gateway.sandbox.push.apple.com' 5# APNS.host = 'gateway.push.apple.com' # プロダクションの場合 6APNS.pem = '*********.pem' 7APNS.port = 2195 8 9device_token = '******************************'# 取得したdeviceTokenをコピー 10APNS.send_notification(device_token, :alert => 'メッセージ', :badge => 0, :sound => 'default')

Push Notification用のSSL証明書を作り直せば良いとのブログも拝見したのですが、rubyではうまくいってるのでそうではないと思いますが...
解決方法がわからず困っています。
よろしくお願い致します。

追加情報
pushTest.phpと同じディレクトリに入れています。
setRootCertificationAuthorityは以下の通りです。

php

1$push->setRootCertificationAuthority('entrust_root_certification_authority.pem');

仮にファイルパスが間違っていた場合,

$php pushTest.php Fatal error: Uncaught exception 'ApnsPHP_Exception' with message 'Unable to read Certificate Authority file '../entrust_root_certification_authority.pem'' in ...

となるので、ファイルパスは正しいかと思います。

2016/04/20 追記
「pushTest.php」の内容は以下になります。
「server_certificates_sandbox.pem」と「entrust_root_certification_authority.pem」はpushTest.phpと同じ階層にあります。

php

1<?php 2/** 3 * @file 4 * sample_push.php 5 * 6 * Push demo 7 * 8 * LICENSE 9 * 10 * This source file is subject to the new BSD license that is bundled 11 * with this package in the file LICENSE.txt. 12 * It is also available through the world-wide-web at this URL: 13 * http://code.google.com/p/apns-php/wiki/License 14 * If you did not receive a copy of the license and are unable to 15 * obtain it through the world-wide-web, please send an email 16 * to aldo.armiento@gmail.com so we can send you a copy immediately. 17 * 18 * @author (C) 2010 Aldo Armiento (aldo.armiento@gmail.com) 19 * @version $Id: sample_push.php 65 2010-12-13 18:38:39Z aldo.armiento $ 20 */ 21 22// Adjust to your timezone 23date_default_timezone_set('Asia/Tokyo'); 24 25// Report all PHP errors 26error_reporting(-1); 27 28// Using Autoload all classes are loaded on-demand 29require_once 'ApnsPHP/Autoload.php'; 30 31// Instanciate a new ApnsPHP_Push object 32$push = new ApnsPHP_Push( 33 ApnsPHP_Abstract::ENVIRONMENT_SANDBOX, 34 'server_certificates_sandbox.pem' 35); 36 37// Set the Root Certificate Autority to verify the Apple remote peer 38$push->setRootCertificationAuthority('entrust_root_certification_authority.pem'); 39 40// Connect to the Apple Push Notification Service 41$push->connect(); 42 43// Instantiate a new Message with a single recipient 44$message = new ApnsPHP_Message('72cb1ae4707aef1df5e72d6c714ce245925317f85aca21fd7fc070acb193b1c2'); 45 46// Set a custom identifier. To get back this identifier use the getCustomIdentifier() method 47// over a ApnsPHP_Message object retrieved with the getErrors() message. 48$message->setCustomIdentifier("Message-Badge-3"); 49 50// Set badge icon to "3" 51$message->setBadge(3); 52 53// Set a simple welcome text 54$message->setText('Hello APNs-enabled device!'); 55 56// Play the default sound 57$message->setSound(); 58 59// Set a custom property 60$message->setCustomProperty('acme2', array('bang', 'whiz')); 61 62// Set another custom property 63$message->setCustomProperty('acme3', array('bing', 'bong')); 64 65// Set the expiry value to 30 seconds 66$message->setExpiry(30); 67 68// Add the message to the message queue 69$push->add($message); 70 71// Send all messages in the message queue 72$push->send(); 73 74// Disconnect from the Apple Push Notification Service 75$push->disconnect(); 76 77// Examine the error message container 78$aErrorQueue = $push->getErrors(); 79if (!empty($aErrorQueue)) { 80 var_dump($aErrorQueue); 81}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

CHERRY

2016/04/19 03:28

PHP のコードで、記載がない部分 setRootCertificationAuthority している部分などは、正しいファイルパスになっているのでしょうか?
tek_tekeo

2016/04/19 03:34

pushTest.phpと同じディレクトリに入れています。 setRootCertificationAuthorityは以下の通りです。 $push->setRootCertificationAuthority('entrust_root_certification_authority.pem'); 仮にファイルパスが間違っていた場合, $php pushTest.php Fatal error: Uncaught exception 'ApnsPHP_Exception' with message 'Unable to read Certificate Authority file '../entrust_root_certification_authority.pem'' in ... となるので、ファイルパスは正しいと思います。
guest

回答1

0

ベストアンサー

「pushTest.php」というファイルをコマンド実行させることでテストしているようですが、
http://d.hatena.ne.jp/MonteCut/20120827/1346079444
でも
http://www.lancork.net/2013/08/how-to-ios-push-second/
でも、
「sample_push.php」を編集して、それをブラウザでアクセスしてテストするよう説明されています。

「pushTest.php」というファイルが「sample_push.php」を上記サイトの説明通り編集したものであれば、それをブラウザでアクセスしてテストしてみてください。
それで解決しないなら、「pushTest.php」の内容を開示しないと誰もわからないと思います。


(4/20 11:10追記)
あと気になるのは、どのルート証明書を使っているかです。

http://d.hatena.ne.jp/MonteCut/20120827/1346079444
の方は「Entrust Root Certification Authority」を使うように書かれていますが、こちらは少し情報が古いと思います。この記事のコメントに「entrustのサイトからentrust_2048_ca.cerをダウンロードすれば、パッチ無しで動作する」というコメントが入っており、
http://www.lancork.net/2013/08/how-to-ios-push-second/
の方は「Entrust.net Certification Authority (2048)」を使うように書かれています。
こちらの証明書を使用して、'verify_peer'の設定処理はコメントアウトしないのがベストプラクティスではないかと思います。

また、Entrustのルート証明書ダウンロードサイトはリニューアルされており、各記事からの直リンクはリンク切れになっていますが、現在は
https://www.entrust.com/get-support/ssl-certificate-support/root-certificate-downloads/
からダウンロードできるようになっており、そこからダウンロードできる「Entrust.net Certificate Authority (2048)」の有効期限は2029/7/24となっています。
私のMacには有効期限の異なる「Entrust.net Certificate Authority (2048)」が2つダウンロードされているので、おそらくこのルート証明書も最近更新されたのではないかと思います。
もし有効期限の古い「Entrust.net Certificate Authority (2048)」をダウンロードしているのであれば、最新のものに更新してみることをお勧めします。

投稿2016/04/19 15:18

編集2016/04/20 02:30
TakeOne

総合スコア6299

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tek_tekeo

2016/04/19 22:15

ご回答ありがとうございます。 「pushTest.php」は「sample_push.php」を編集したものであり、 ローカルホストに配置してブラウザでアクセスしてみましたが、同じエラーが出力されます。 「pushTest.php」の内容を追記しておきますのでコメントをいただければ幸いです。 よろしくお願い致します。
TakeOne

2016/04/20 02:33

「pushTest.php」は「sample_push.php」を必要部分だけ編集したものであることを確認しました。それで、あと気になる点を回答に追記してみました。
tek_tekeo

2016/04/23 03:22

ご回答ありがとうございます。 Entrustのルート証明書をダウンロードし直してみたらうまくいきました。 以前もご指摘の証明書をダウンロードしたつもりだったので、かなりはまってしまいました。 本当にありがとうございます。非常に助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問