Firebase Console に Unity アプリケーション プロジェクトを追加します。詳細は FCM ガイドをご参照ください。
次に、Firebase Unity SDK を https://firebase.google.com/download/unity からダウンロードし、Firebase Unity SDK をアプリに追加します。
Unity で、 メニューより Assets > Import Package > Custom Package を選択します。
ダウンロードした Firebase Unity SDK を展開し、含まれる FirebaseMessaging.unitypackage パッケージを選択します。
Import Unity Package ウィンドウが表示されます。 Import ボタンを押します。
Assets/TapjoySample/Scripts にある TapjoySample.cs スクリプトファイルで、以下の行をアンコメントします:
#define USE_FIREBASE_MESSAGING
Firebase clound messaging をプロジェクトのスクリプトで設定する前に、Firebase SDK を 下記のコードで初期化します。
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
var dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available) {
// FirebaseApp を作成し、参照を保持します。例えば、
// app = Firebase.FirebaseApp.DefaultInstance;
// ここで、app はアプリクラスの Firebase.FirebaseApp 型のプロパティとします。
// また、Firebase をアプリで使用可能と示すフラグをここでセットします。
} else {
UnityEngine.Debug.LogError(System.String.Format(
"Could not resolve all Firebase dependencies: {0}", dependencyStatus));
// Firebase Unity SDK を使用するのはこの場合には安全ではありません。
}
});
Firebase Cloud Messaging をアプリ スクリプトの中で設定します:
public void Start() {
Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;
Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
}
public void OnTokenReceived(object sender, Firebase.Messaging.TokenReceivedEventArgs token) {
UnityEngine.Debug.Log("Received Registration Token: " + token.Token);
}
public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
UnityEngine.Debug.Log("Received a new message from: " + e.Message.From);
}
注記: Firebase SDK は Unity プロジェクトにプロジェクトレベルで追加され、プラットフォームレベルでの追加にはなりません。このため、Tapjoy では Unity iOS に FCM を使用しないのですが Google_service.plist ファイルを Assets に追加する必要があります。 Google_service.plist を取得するには、Firebase コンソールで iOS アプリを追加し、config plist ファイルを生成してダウンロードし、Assets に追加します。
参照: https://firebase.google.com/docs/cloud-messaging/unity/client
プッシュを送るためにはAPI KeyとSender ID が必要になります。Tapjoyダッシュボードの 設定 - アプリ設定 の Push通知 から登録してください。
Sender ID と API Key は Google API Consoleにあります。
Unity の FCM 実装では、 アプリケーションがバックグランドや起動していない時に MessageReceived デリゲーションが呼ばれない場合があります。アプリケーションがバックグラウンドまたは起動していない際に通知を受け取るには、下記の追加の実装をAndroidプロジェクトとして書き出したAndroidネイティブにて行う必要があります。
com.google.firebase.messaging.cpp.ListenerService
のサブクラスを作成し、onMessageReceived をオーバーライドして Tapjoy.setReceiveRemoteNotification が呼ばれるようにします。 import com.google.firebase.messaging.RemoteMessage;
import com.google.firebase.messaging.cpp.ListenerService;
import com.tapjoy.Tapjoy;
public class FCMMessageService extends ListenerService {
@Override
public void onMessageReceived(RemoteMessage message) {
// super.onMessageReceived(message);
Tapjoy.setReceiveRemoteNotification(getApplicationContext(), message.getData());
}
}
<service android:name=".pushnotification.FCMMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Tapjoyダッシュボードからのプッシュ通知(FCMメッセージ)送信時のアイコンはデフォルトではアプリケーションのアイコンが表示されます。
AndroidManifest.xml
の <application>
タグにリソースを下記のように指定する事によりアイコンをカスタマイズできます:
<manifest ...>
...
<application ...>
...
// 通知バー用 小アイコン
<meta-data android:name="com.tapjoy.notification.icon" android:resource="@drawable/{id}"/>
// 通知パネル用 大アイコン (SDK 11.6.0 以降)
<meta-data android:name="com.tapjoy.notification.icon.large" android:resource="@drawable/{id}"/>
// Android 5.0 以前の端末用の通知バー用アイコン(SDK 11.6.0 以降)
<meta-data android:name="com.tapjoy.notification.icon.compat" android:resource="@drawable/{id}"/>
...
</application>
...
</manifest>
以下のAPIでPush通知の受信のオプトアウトの制御ができます。 このAPIは、Tapjoy.connect が成功した後に使用可能になります。
/**
* @brief Returns true if the push notification is disabled when target platform is Android
* Returns false otherwise
*/
public static bool IsPushNotificationDisabled()
/**
* @brief Sets whether the push notification is disabled. (Only for Android)
* @param disabled
* true to disable the push notification
*/
public static void SetPushNotificationDisabled(bool disabled)
Tapjoy Unity Pluginには iOS Push 通知のための C# インターフェースがありません。
このため、iOS でPush通知を実装する場合は下記のようにUnityから出力されたプロジェクトを Xcodeで変更する必要があります。:
Tapjoy の機能を下記のデリゲートメソッド内で呼び出します:
// called when the remote notification is registered
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[Tapjoy setDeviceToken:deviceToken];
}
// called when the user get push message while playing the app
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
[Tapjoy setReceiveRemoteNotification:userInfo];
}
Push Notification Serviceの登録を呼び出すことによって、登録の手順が開始されます。アプリインストール後初回の呼びだし時に、プッシュ通知許可可否の確認ダイアログが表示されます。
if (NSClassFromString(@"UNUserNotificationCenter")) {
// iOS 10+ Notifications
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionAlert | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[application registerForRemoteNotifications];
});
}
}];
} else if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
// iOS 8 - 9 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
詳細は iOS Local and Remote Notification Programming Guide をご参照ください。
まずアップルのデベロッパーサイトからプッシュサービスに利用する証明書を生成する必要があります。詳しい内容はLocal Notifications and Remote Notifications Guideをご参照ください。用意した証明書は管理画面のPush通知からアップロードしてください。
Tapjoyで同時に複数のPushを送信できますか?
はい。この記事をご参照ください。
Push証明書の期間がもうすぐ切れそうです。
期間切れまでに新しい証明書をアップロードしてください。
ユーザーをアプリの特定の箇所にリダイレクトできますか?
はい。カスタムフィールドを利用してユーザーをリダイレクトできます。この記事をご参照ください。