Unity SDKでpush通知受信時の処理をカスタマイズする(iOS)
unity側でpush通知を受け取る
下記のサンプルの様に、MonoBehaviourを継承したComponentにて、
UnityEngine.iOS.NotificationServicesを使用することにより、
unity側で受信時の処理を実装することが可能です。
UnityEngine.iOS.NotificationServicesについて詳しくは、下記を御覧ください。
https://docs.unity3d.com/ja/current/ScriptReference/iOS.NotificationServices.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrowthbeatSampleComponent : MonoBehaviour {
bool isRemoteNotificationProcessing = false;
void Update ()
{
#if UNITY_IPHONE
// 起動中の受信処理
if (!isRemoteNotificationProcessing && UnityEngine.iOS.NotificationServices.remoteNotificationCount > 0) {
// 複数回処理されないために処理中のフラグを立てる
isRemoteNotificationProcessing = true;
UnityEngine.iOS.RemoteNotification[] notifications = UnityEngine.iOS.NotificationServices.remoteNotifications;
foreach (UnityEngine.iOS.RemoteNotification notification in notifications) {
// 通知受信時の処理
Debug.Log("alert body: " + notification.alertBody);
Debug.Log("user info: ");
foreach(string key in notification.userInfo.Keys) {
Debug.Log("key: " + key + ", value: " + notification.userInfo[key]);
}
}
// 処理済みのRemoteNotificationsは削除
UnityEngine.iOS.NotificationServices.ClearRemoteNotifications();
// 処理が終わったらフラグを開放
isRemoteNotificationProcessing = false;
}
#endif
}
}