UnityでAndroidのPushNotification(GCM)を実装する方法 | ひささん日記のとき同様、Apple Push Notifications (PNS for iOS) – Unity | Games2win Developersこちらのサイトのとおりやってみました。
iOS版の場合、NotificationServicesを使って自前で書く方法がちょいちょいググると出てくるんですが、この方法はremoteNotificationCountが1以上の場合かどうかで判断していて、ポーリングしないとダメみたいであんまりやりたくありませんでした。
たとえOnFixedUpdate内でやったとしても常にPushNotificationがあるかどうかのために回すのはあんまりイケていないです。
このあたりの議論は、Unity 3.5 and Push Notificationsここに載っています。
その点、prime[31] – Unity plugin documentationはイベントハンドラを登録しておくことができるので、ポーリングする必要がありません。
今回はこのAssetの使い方がメインになります。
事前にPushNotification用の.p12(証明書)を作成する
このあたりはググるとすぐに出てくるので割愛。
.pemファイルを作る方法が多いのですが、キーチェーンアクセスから.p12ファイルを書き出してそれを使っても動くので、今回は.p12ファイルを使っています。
(どうゆう環境でPushNotificationするかによるのかもしれません)
PushNotification用のunitypackageをimportする
Asset Storeから prime31 Etcetera Plugin などで検索し、購入後にimportしておきます。
demoのシーンを使ってPushNotificationを試してみる
demoにシーンがあるので、そちらを表示しておく。
EtceteraEventListener.cs
StringBuilderを使うので、以下を追加。
using System.Text;
アプリ起動時にPushNotification用のdeviceTokenを取得するAPIを実行します。(詳細は別クラスで)
ここのremoteRegistrationSucceededの中でdeviceTokenが取れるので、PushNotificationを配信するサーバーに登録しましょう。
private string regURL = null; void remoteRegistrationSucceeded( string deviceToken ) { Debug.Log( "remoteRegistrationSucceeded with deviceToken: " + deviceToken ); //string apns_env = "production"; string apns_env = "develop"; StringBuilder urlString = new StringBuilder (); urlString.AppendFormat ("&appName={0}", WWW.EscapeURL ("app_name")); // provided by G2W urlString.AppendFormat ("&app_id={0}", WWW.EscapeURL ("PNS_ID")); // provided by G2W urlString.AppendFormat ("&appversion={0}", WWW.EscapeURL ("APP_Version")); // provided by G2W urlString.AppendFormat ("&deviceuid={0}", WWW.EscapeURL ("00")); urlString.AppendFormat ("&devicetoken={0}", WWW.EscapeURL (deviceToken)); urlString.AppendFormat ("&devicename={0}", WWW.EscapeURL (SystemInfo.deviceName)); urlString.AppendFormat ("&devicemodel={0}", WWW.EscapeURL (SystemInfo.deviceModel)); urlString.AppendFormat ("&deviceversion={0}", WWW.EscapeURL (SystemInfo.graphicsDeviceVersion)); urlString.AppendFormat ("&apns_env={0}", WWW.EscapeURL (apns_env)); Debug.Log ("http://" + ("hoge.com/" + urlString)); regURL = urlString.ToString (); StartCoroutine ("RegDeviceForPush"); } IEnumerator RegDeviceForPush () { WWW w = new WWW ("http://" + ("hoge.com/" + regURL), UTF8Encoding.UTF8.GetBytes (regURL)); yield return w; Debug.Log ("EEEEE: " + w.error); }
アプリがフォアグラウンドでPushNotificationを受けた場合。
void remoteNotificationReceived( IDictionary notification ) { Debug.Log( "remoteNotificationReceived" ); Prime31.Utils.logObject( notification ); }
アプリがバックグラウンドでPushNotificationを受け、さらにその通知自体をタップした場合。
void remoteNotificationReceivedAtLaunch( IDictionary notification ) { Debug.Log( "remoteNotificationReceivedAtLaunch" ); Prime31.Utils.logObject( notification ); }
EtceteraGUIManagerTwo.cs
アプリ起動時にEtceteraBinding.registerForRemoteNotifcationsを呼んで、deviceTokenを登録します。
if( GUILayout.Button( "Register for Push" ) ) { EtceteraBinding.registerForRemoteNotifcations( P31RemoteNotificationType.Alert | P31RemoteNotificationType.Badge | P31RemoteNotificationType.Sound ); }
タイミングの話し
PushNotificationするときに必ずこの話しが出てきちゃう(ぼくだけかな?)んですが、ネイティブの場合は、
アプリ側でPushが来るパターンは以下の3つ
- フォアグラウンドでプッシュ通知を受け取ったとき
- アプリのプロセスがバックグラウンドで生きているときにプッシュ通知を受け取り、ユーザーがアクションボタンをタップしたとき
- アプリのプロセスがバックグラウンドで生きていないときにプッシュ通知を受け取り、ユーザーがアクションボタンをタップしたとき
ただ、2の項目は、アプリ内にバッジのようなものを表示する場合は、 ここではやらずapplicationDidBecomeActiveでやるようにしている。
これの理由は、バックグラウンドにいてアクションボタンではなく、ホーム画面からアプリアイコンをタップされた場合の処理が必要なので、
あえて、applicationDidBecomeActiveでやっているのです。
ネイティブの実装例は、PushNotification-iOSandAndroid/iOS/AppDelegate.m at master · hisasann/PushNotification-iOSandAndroidこちらをご覧ください。
問題はこの2のapplicationDidBecomeActiveでやるような処理をUnityでどうやるかですね。
ここでNotificationServicesを使ってもできそうなんですが、
せっかくなので、Etceteraを使ってみましょう。
if( EtceteraBinding.getBadgeCount() > 0) { // 何か処理をゴニョゴニョ // バッジの数値をクリア EtceteraBinding.setBadgeCount( -1 ); }
アプリケーションがOnResumeしたタイミングでこんな感じでPushNotificationが来ていたかどうかを確認できます。
参考リンク
prime[31] – Unity plugin documentation
Unity and IOS push notifications – Google Docs
ソーテック社
売り上げランキング: 2,016