using System; using UnityEngine; public class OmotePlugin : MonoBehaviour { public delegate void OnNotifyEnabledReceivedEventHandler(bool enabled); public delegate void OnUnregisterReceivedEventHandler(bool isSuccess); [Serializable] private class OmotenashiFirebaseOptions { public string ApiKey; public string ProjectId; public string ApplicationId; public string SenderId; } public delegate void OmoteEventHandler(object sender, TEventArgs e) where TEventArgs : EventArgs; public class RequestResultEventArgs : EventArgs { internal class RawData { public int type; public int result; public string body; public string endpoint; public int statusCode; public string reason; } public int Type { get; private set; } public int Result { get; private set; } public string Body { get; private set; } public string EndPoint { get; private set; } public int StatusCode { get; private set; } public string Reason { get; private set; } internal RequestResultEventArgs(RawData data) { Type = data.type; Result = data.result; Body = data.body; EndPoint = data.endpoint; StatusCode = data.statusCode; Reason = data.reason; } } public class LocalNotification { private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); public string Message { get; private set; } public DateTime When { get; private set; } public string LabelId { get; private set; } public LocalNotificationPriority Priority { get; set; } public int NotificationId { get; set; } public string Title { get; set; } public string ExtraText { get; set; } public string ImagePath { get; set; } public LocalNotification(string message, DateTime when, string labelId) { if (message == null) { throw new ArgumentNullException("message"); } if (labelId == null) { throw new ArgumentNullException("labelId"); } Message = message; When = when; LabelId = labelId; Priority = LocalNotificationPriority.Normal; ExtraText = string.Empty; } public void Schedule() { _ = (When.ToUniversalTime() - UnixEpoch).TotalSeconds; } public void Cancel() { Cancel(LabelId); } public static void Cancel(string labelId) { if (labelId == null) { OmoteLog.Error("labelId must not be null."); } } public static void CancelAll() { } } public class RegistrationTokenEventArgs : EventArgs { public string RegistrationToken { get; set; } } public class PushNotificationEventArgs : EventArgs { internal class RawData { public string id; public string message; public string extra; } public string Id { get; private set; } public string Message { get; private set; } public string Extra { get; private set; } internal PushNotificationEventArgs(RawData data) { Id = data.id; Message = data.message; Extra = data.extra; } public override string ToString() { return $"Id={Id}, Message={Message}, Extra={Extra}"; } } public class LocalNotificationEventArgs : EventArgs { internal class RawData { public string scheduleId; public string label; public string scheduled; public string message; public string extra; } public string ScheduleId { get; private set; } public string LabelId { get; private set; } public DateTime ScheduledAt { get; private set; } public string MessageText { get; private set; } public string ExtraText { get; private set; } internal LocalNotificationEventArgs(RawData data) { ScheduleId = data.scheduleId; LabelId = data.label; ScheduledAt = DateTime.Parse(data.scheduled); MessageText = data.message; ExtraText = data.extra; } public override string ToString() { return $"MessageText={MessageText}, ScheduledAt={ScheduledAt}, LabelId={LabelId}, ScheduleId={ScheduleId}, ExtraText={ExtraText}"; } } public class NotificationChangedEventArgs : EventArgs { internal class RawData { public bool result; } public bool Result { get; private set; } internal NotificationChangedEventArgs(RawData data) { Result = data.result; } public override string ToString() { return $"Result={Result}"; } } public delegate void OnRegistrationTokenReceivedEventHandler(object sender, RegistrationTokenEventArgs e); public delegate void OnFailToRegisterForRemoteNotificationsEventHandler(object sender, string e); [SerializeField] private OmotenashiFirebaseOptions omotenashiFirebaseOptions; [SerializeField] private bool IsAutomatic = true; public string country; public event OnNotifyEnabledReceivedEventHandler OnNotificationReceived; public event OnUnregisterReceivedEventHandler OnUnregisterReceived; public event OmoteEventHandler OnRequestResult; public event OnRegistrationTokenReceivedEventHandler OnRegistrationTokenReceived; public event OmoteEventHandler OnReceivedPushNotification; public event OmoteEventHandler OnReceivedLocalNotification; public event OmoteEventHandler OnLaunchFromPushNotification; public event OmoteEventHandler OnLaunchFromLocalNotification; public event OmoteEventHandler OnNotificationEnableChanged; public event OmoteEventHandler OnNotificationCountryChanged; public event OnFailToRegisterForRemoteNotificationsEventHandler OnFailToRegisterForRemoteNotifications; private void Awake() { } private void AwakePush(OmotenashiFirebaseOptions options, AndroidJavaObject baseObject) { } private void Start() { StartPush(); } private void StartPush() { if (string.IsNullOrEmpty(country)) { OmoteLog.Error("Country code is not set."); } } public void SetSandbox(bool isSandbox) { OmoteLog.Info("setDebugMode(isDebuggable: {0}) called.", isSandbox); } public void SetDebugLogEnabled(bool isEnabled) { OmoteLog.SetEnable(isEnabled); OmoteLog.Info("setDebugLogEnabled(isEnabled: {0}) called.", isEnabled); } public void SendConversion(string appViewerId) { if (string.IsNullOrEmpty(appViewerId)) { OmoteLog.Error("appViewerId is not set."); return; } OmoteLog.Info("SendConversion(appViewerId: {0}) called.", appViewerId); } public void SendSession(string userId, string deviceId) { OmoteLog.Info("SendSession({0}, {1}) called.", userId, deviceId); } public void SetRequestEnabled(bool isEnabled) { } public bool IsRequestEnabled() { return true; } public void CallbackUnregister(string isSuccess) { if (this.OnUnregisterReceived != null) { if (isSuccess.Equals("Success")) { OmoteLog.Info("Unregister: Success"); this.OnUnregisterReceived(isSuccess: true); } else if (isSuccess.Equals("Fail")) { OmoteLog.Info("Unregister: Fail"); this.OnUnregisterReceived(isSuccess: false); } else { OmoteLog.Info("Unregister: unknown {0}", isSuccess); this.OnUnregisterReceived(isSuccess: false); } } else { OmoteLog.Info("Unregister: delegate is null"); } } public void GetNotificationEnabled() { if (this.OnNotificationReceived != null) { this.OnNotificationReceived(enabled: true); } } private void InvokeFromJson(string json, Func argsCreator, OmoteEventHandler action) where TArg : EventArgs { OmoteLog.Info("{0}", json); TRaw arg = JsonUtility.FromJson(json); TArg val = argsCreator(arg); OmoteLog.Info("{0}", val); action?.Invoke(this, val); } public void CallStartPush() { StartPush(); } public void Unregister(bool isLocalOnly) { CallbackUnregister("Success"); } private void CallbackOnRequestResult(string json) { OmoteLog.Info("CallbackOnRequestResult."); InvokeFromJson(json, (RequestResultEventArgs.RawData raw) => new RequestResultEventArgs(raw), this.OnRequestResult); } private void OnApplicationPause(bool pause) { } public bool CanScheduleExactAlarms() { return true; } public void RescheduleLocalNotification() { } public void OpenExactAlarmSettings() { } [Obsolete("Use LocalNotificationBuilder.")] public void ScheduleLocalNotification(string messageText, DateTime dateTime, string labelId, LocalNotificationPriority priority, int notificationId) { ScheduleLocalNotification(messageText, dateTime, labelId, priority, notificationId, string.Empty); } [Obsolete("Use LocalNotificationBuilder.")] public void ScheduleLocalNotification(string messageText, DateTime date, string labelId, LocalNotificationPriority priority, int notificationId, string extraText) { LocalNotification localNotification = new LocalNotification(messageText, date, labelId); localNotification.Priority = priority; localNotification.NotificationId = notificationId; localNotification.ExtraText = extraText; localNotification.Schedule(); } [Obsolete("Use OmotePlugin.Localnotification.Cancel(string)")] public void CancelLocalNotification(string labelId) { LocalNotification.Cancel(labelId); } [Obsolete("Use OmotePlugin.Localnotificatin.CancelAll()")] public void CancelAllLocalNotification() { LocalNotification.CancelAll(); } private void OnApnsTokenReceived(string token) { } public void SetNotificationsEnabled(bool enabled) { } public bool IsNotificationsEnabled() { return false; } public void UpdateCountry(string country) { if (string.IsNullOrEmpty(country)) { OmoteLog.Error("country must not be null nor empty."); } } public void RegisterForRemoteNotification() { } public bool isNotificationAuthorized() { return true; } public void RequestNotificationPermission() { } private void CallbackOnTokenReceived(string token) { if (!string.IsNullOrEmpty(token)) { RegistrationTokenEventArgs e = new RegistrationTokenEventArgs { RegistrationToken = token }; if (this.OnRegistrationTokenReceived != null) { this.OnRegistrationTokenReceived(this, e); } } } private void CallbackOnReceivedPushNotificationInForeground(string json) { OmoteLog.Info("CallbackOnReceivedPushNotificationInForeground."); InvokeFromJson(json, (PushNotificationEventArgs.RawData raw) => new PushNotificationEventArgs(raw), this.OnReceivedPushNotification); } private void CallbackOnReceivedLocalNotificationInForeground(string json) { OmoteLog.Info("CallbackOnReceivedLocalNotificationInForeground."); InvokeFromJson(json, (LocalNotificationEventArgs.RawData raw) => new LocalNotificationEventArgs(raw), this.OnReceivedLocalNotification); } private void CallbackOnLaunchFromPushNotification(string json) { OmoteLog.Info("CallbackOnLaunchFromPushNotification."); InvokeFromJson(json, (PushNotificationEventArgs.RawData raw) => new PushNotificationEventArgs(raw), this.OnLaunchFromPushNotification); } private void CallbackOnLaunchFromLocalNotification(string json) { OmoteLog.Info("CallbackOnLaunchFromLocalNotification."); InvokeFromJson(json, (LocalNotificationEventArgs.RawData raw) => new LocalNotificationEventArgs(raw), this.OnLaunchFromLocalNotification); } private void CallbackOnNotificationEnableChanged(string json) { OmoteLog.Info("CallbackOnNotificationEnableChanged."); InvokeFromJson(json, (NotificationChangedEventArgs.RawData raw) => new NotificationChangedEventArgs(raw), this.OnNotificationEnableChanged); } private void CallbackOnNotificationCountryChanged(string json) { OmoteLog.Info("CallbackOnNotificationCountryChanged"); InvokeFromJson(json, (NotificationChangedEventArgs.RawData raw) => new NotificationChangedEventArgs(raw), this.OnNotificationCountryChanged); } private void CallbackOnFailToRegisterForRemoteNotifications(string errorString) { OmoteLog.Info("CallbackOnFailToRegisterForRemoteNotifications."); if (errorString != null && this.OnFailToRegisterForRemoteNotifications != null) { this.OnFailToRegisterForRemoteNotifications(this, errorString); } } }