Project_WL/Assets/Script/Info/NotiInfo.cs

130 lines
4.2 KiB
C#
Raw Permalink Normal View History

2024-12-15 01:39:39 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
#if UNITY_ANDROID
using Unity.Notifications.Android;
#elif UNITY_IOS
using Unity.Notifications.iOS;
#endif
public class NotiInfo : MonoBehaviourSingletonTemplate<NotiInfo>
{
int id = 1;
protected override void Awake()
{
base.Awake();
DontDestroyOnLoad(transform.parent);
Del_AllNoti();
}
IEnumerator Start()
{
int notivalue = -1;
#if UNITY_ANDROID
var request = new PermissionRequest();
while (request.Status == PermissionStatus.RequestPending)
yield return null;
notivalue = request.Status == PermissionStatus.Allowed ? 1 : 0;
#elif UNITY_IOS
var authorizationOption = AuthorizationOption.Alert | AuthorizationOption.Badge;
using (var req = new AuthorizationRequest(authorizationOption, true))
{
while (!req.IsFinished)
{
yield return null;
};
string res = "\n RequestAuthorization:";
res += "\n finished: " + req.IsFinished;
res += "\n granted : " + req.Granted;
res += "\n error: " + req.Error;
res += "\n deviceToken: " + req.DeviceToken;
Debug.Log(res);
notivalue = req.Granted ? 1 : 0;
}
#endif
while (true)
{
if (ServerInfo.Ins != null && ServerInfo.Ins.m_ServerData != null)
{
if (notivalue > -1)
ServerInfo.Ins.m_ServerData.Set_Common(eCommon.Noti, notivalue);
break;
}
yield return null;
}
}
/// <summary>
/// subtitle 은 ios 에서만 사용
/// ios는 아이콘 지정안하면 기본 아이콘 나옴 (안드로이드는 아이콘 지정해야 함)
/// </summary>
/// <returns></returns>
public int Make_LocalPush(string title, string text, string subtitle, int seconds)
{
// 현재 낮인 지 밤인 지
bool isday = DateTime.Now.Hour > 7 && DateTime.Now.Hour < 22;
var sdata = ServerInfo.Ins.m_ServerData;
if ((isday && sdata.Get_Common(eCommon.Noti) == 1) || (!isday && sdata.Get_Common(eCommon.NightNoti) == 1))
{
#if UNITY_ANDROID
var notification = new AndroidNotification();
notification.Title = title;
notification.Text = text;
notification.FireTime = DateTime.Now.AddSeconds(seconds);
AndroidNotificationCenter.SendNotification(notification, id.ToString());
#elif UNITY_IOS
var timeTrigger = new iOSNotificationTimeIntervalTrigger()
{
TimeInterval = TimeSpan.FromSeconds(seconds),
Repeats = false
};
var notification = new iOSNotification()
{
// You can specify a custom identifier which can be used to manage the notification later.
// If you don't provide one, a unique string will be generated automatically.
Identifier = id.ToString(),
Title = title,
Body = text,
Subtitle = subtitle,
ShowInForeground = true,
ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
CategoryIdentifier = "category_a",
ThreadIdentifier = "thread1",
Trigger = timeTrigger,
};
iOSNotificationCenter.ScheduleNotification(notification);
#endif
return id++;
}
return 0;
}
public void Del_AllNoti()
{
#if UNITY_ANDROID
AndroidNotificationCenter.CancelAllNotifications();
#elif UNITY_IOS
iOSNotificationCenter.RemoveAllDeliveredNotifications();
iOSNotificationCenter.RemoveAllScheduledNotifications();
#endif
}
public void Del_LocalPush(int _id)
{
string notiid = _id.ToString();
#if UNITY_ANDROID
AndroidNotificationCenter.DeleteNotificationChannel(notiid);
#elif UNITY_IOS
iOSNotificationCenter.RemoveDeliveredNotification(notiid);
iOSNotificationCenter.RemoveScheduledNotification(notiid);
#endif
}
}