Nobollel開発者ブログ

Nobollelのエンジニアが、UnityやCocos2d-xの旬な情報・技術を紹介します。

Missing Push Notification Entitlement

みなさんおはこんばんにちは、エンジニアの石橋です。

今回はPush通知に付いて話そうかと思います。
Xcode8からはPush Notificationに関する署名をきちんと設定しなければいけなくなりました。
設定しないと申請時にMissing Push Notification Entitlementという内容のメールで却下されます。
これに対応するのは簡単でCapabilities -> Push NotificationsをONとすれば自動的にAppName.entitlementsファイルが作成されます。
ただ、これをビルド毎にやるのは少々面倒なので自動化します。

1) 以下の内容のファイルをAssets以下に配置します ファイル名はproduction.entitlementsとします。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>production</string>
</dict>
</plist>

2) Unityのビルドを自動化します。
以下の内容のファイルをAssets/Editor以下に配置します。 こちらはファイル名はなんでもよいです。

using UnityEditor;
using UnityEngine;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;

public class Hoge {
    static void OnPostProcessBuild(BuildTarget buildTarget, string buildPath ) {
        string projPath = buildPath + "/Unity-iPhone.xcodeproj/project.pbxproj";

        PBXProject proj = new PBXProject();
        proj.ReadFromString( File.ReadAllText( projPath ) );

        string target = proj.TargetGuidByName( "Unity-iPhone" );

        var debug = proj.BuildConfigByName( target, "Debug" );
        var release = proj.BuildConfigByName( target, "Release" );

        var fileName = "production.entitlements";
        var src = Path.Combine( Application.dataPath, fileName );
        var dest = Path.Combine( buildPath, fileName );
        File.Copy( src, dest );
        proj.AddFileToBuild( target, proj.AddFile( dest, fileName, PBXSourceTree.Source ) );
        proj.SetBuildPropertyForConfig( debug, "CODE_SIGN_ENTITLEMENTS", fileName );
        proj.SetBuildPropertyForConfig( release, "CODE_SIGN_ENTITLEMENTS", fileName );

        proj.WriteToFile( projPath );
    }
}

3) 最後にipa出力後にきちんと設定されているかを確認します。

  1. 出力したipaをzipにリネームして解凍
  2. Terminalで1で解凍したPayloadフォルダへ移動
  3. codesign -d --entitlements :- Payload/パッケージ.appとコマンドを打ちproductionで設定されていればOKです。

参考1 参考2