You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
8.7 KiB
253 lines
8.7 KiB
using UnityEngine;
|
|
using System;
|
|
using System.Xml.Serialization;
|
|
using Firebase.Messaging;
|
|
using Firebase;
|
|
using UnityEngine.Android;
|
|
#if UNITY_ANDROID
|
|
using Unity.Notifications.Android;
|
|
#elif UNITY_IOS
|
|
using Unity.Notifications.iOS;
|
|
#endif
|
|
|
|
public class NotifyMgr : MonoBehaviour
|
|
{
|
|
#if VER_ONESTORE
|
|
protected const string ChannelId = "jf_onestore";
|
|
#elif UNITY_ANDROID
|
|
protected const string ChannelId = "jf_google";
|
|
#else
|
|
protected const string ChannelId = "jf_apple";
|
|
#endif
|
|
protected const string ChannelName = "Jelly";
|
|
protected const string ChannelDesc = "Jelly News";
|
|
|
|
protected static bool bEndNoti = false;
|
|
protected static bool bInitChannel = false;
|
|
|
|
static bool notifiOn = true;
|
|
static bool midNightNotifiOn = true;
|
|
|
|
public static void SetNotifiON(bool bvalue)
|
|
{
|
|
notifiOn = bvalue;
|
|
}
|
|
|
|
public static void SetnightNotifiON(bool bvalue)
|
|
{
|
|
midNightNotifiOn = bvalue;
|
|
}
|
|
|
|
public static void SSendEndNoti()
|
|
{
|
|
if (bEndNoti)
|
|
return;
|
|
bEndNoti = true;
|
|
SSendLocalNotification(notifiOn, midNightNotifiOn, true, TimeUtils.NextDayUtc());
|
|
}
|
|
|
|
public static void SCancelAllNotification()
|
|
{
|
|
bEndNoti = false;
|
|
#if UNITY_EDITOR
|
|
#elif UNITY_ANDROID
|
|
AndroidNotificationCenter.CancelAllNotifications();
|
|
#elif UNITY_IOS
|
|
iOSNotificationCenter.RemoveAllDeliveredNotifications();
|
|
iOSNotificationCenter.RemoveAllScheduledNotifications();
|
|
#endif
|
|
}
|
|
|
|
private static DateTime nightStartTime = DateTime.Today.AddHours(15); // 밤 9시
|
|
private static DateTime korNightStartTime= nightStartTime.AddHours(9);
|
|
private static DateTime morningEndTime = DateTime.Today.AddHours(18); // 아침 8시
|
|
private static DateTime korMorningEndTime = morningEndTime.AddHours(9);
|
|
|
|
public static void SSendLocalNotification(bool isNotiflyOn, bool isNightNotiflyOn, bool bcancelall, DateTime datetimeutc, string strtitle = null, string strmsg = null)
|
|
{
|
|
if (!isNotiflyOn)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (datetimeutc >= korNightStartTime || datetimeutc <= korMorningEndTime)
|
|
{
|
|
if (!isNightNotiflyOn)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (bcancelall)
|
|
SCancelAllNotification();
|
|
|
|
if (string.IsNullOrEmpty(strtitle))
|
|
strtitle = LocalizationText.GetText("msg_localpushtitle001");
|
|
if (string.IsNullOrEmpty(strmsg))
|
|
strmsg = LocalizationText.GetText("msg_localpushbody001");
|
|
|
|
#if UNITY_EDITOR
|
|
#elif UNITY_ANDROID
|
|
if (!bInitChannel)
|
|
{
|
|
bInitChannel = true;
|
|
AndroidNotificationChannel channel = new AndroidNotificationChannel(ChannelId, ChannelName, ChannelDesc, Importance.Default);
|
|
AndroidNotificationCenter.RegisterNotificationChannel(channel);
|
|
}
|
|
|
|
AndroidNotification notification = new AndroidNotification();
|
|
notification.Title = strtitle;
|
|
notification.Text = strmsg;
|
|
notification.FireTime = datetimeutc.ToLocalTime();
|
|
notification.SmallIcon = "icon_0";
|
|
notification.LargeIcon = "icon_1";
|
|
|
|
AndroidNotificationCenter.SendNotification(notification, ChannelId);
|
|
#elif UNITY_IOS
|
|
|
|
iOSNotification notification = new iOSNotification();
|
|
notification.Title = strtitle;
|
|
notification.Body = strmsg;
|
|
notification.ShowInForeground = false;
|
|
notification.CategoryIdentifier = ChannelId;
|
|
|
|
DateTime datetimelocal = datetimeutc.ToLocalTime();
|
|
iOSNotificationCalendarTrigger trigger = new iOSNotificationCalendarTrigger()
|
|
{
|
|
Year = datetimelocal.Year,
|
|
Month = datetimelocal.Month,
|
|
Day = datetimelocal.Day,
|
|
Hour = datetimelocal.Hour,
|
|
Minute = datetimelocal.Minute,
|
|
Second = datetimelocal.Second,
|
|
Repeats = false
|
|
};
|
|
notification.Trigger = trigger;
|
|
|
|
iOSNotificationCenter.ScheduleNotification(notification);
|
|
|
|
#endif
|
|
}
|
|
|
|
//https://firebase.google.com/docs/cloud-messaging/unity/client?hl=ko 의 클라우드 메시징 초기화 부분
|
|
//https://siyan.tistory.com/9 를 참조. 하기전에 파이어베이스 메세지 패키지도 임포팅함. 구글서비스는 임포팅되어있어서 건너뜀
|
|
//https://velog.io/@maratangsoft/%EC%9C%A0%EB%8B%88%ED%8B%B0-%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EC%95%B1%EC%97%90%EC%84%9C-FCM-%EC%84%9C%EB%B2%84-%ED%91%B8%EC%8B%9C-%EB%B0%9B%EA%B8%B0
|
|
//https://boxwitch.tistory.com/entry/%EC%9C%A0%EB%8B%88%ED%8B%B0-%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EB%A1%9C%EC%BB%AC%ED%91%B8%EC%8B%9C-Mobile-Notifications
|
|
//맨 아래쪽은 아이콘, 3번째는 아이콘 기능을 추가하기 위해 찾은 곳.
|
|
string CHANNEL_ID = "myChannel";
|
|
int apiLevel;
|
|
|
|
void Start()
|
|
{
|
|
#if UNITY_ANDROID
|
|
InitializeAndroidLocalPush();//로컬 푸시 알림 초기화
|
|
InitializeFCM();//파이어베이스 푸시 알림 초기화
|
|
#elif UNITY_IOS
|
|
|
|
#endif
|
|
}
|
|
|
|
public void InitializeAndroidLocalPush()//로컬푸시 알림 초기화
|
|
{
|
|
#if UNITY_ANDROID
|
|
string androidInfo = SystemInfo.operatingSystem;
|
|
Debug.Log("androidInfo: " + androidInfo);
|
|
apiLevel = int.Parse(androidInfo.Substring(androidInfo.IndexOf("-") + 1, 2));
|
|
Debug.Log("apiLevel: " + apiLevel);
|
|
|
|
if (apiLevel >= 33 &&
|
|
!Permission.HasUserAuthorizedPermission("android.permission.POST_NOTIFICATIONS"))//API 에 따라 알림 초기화가 다르게 동작된다.
|
|
{
|
|
Permission.RequestUserPermission("android.permission.POST_NOTIFICATIONS");
|
|
}
|
|
|
|
if (apiLevel >= 26)
|
|
{
|
|
var channel = new AndroidNotificationChannel()
|
|
{
|
|
Id = CHANNEL_ID,
|
|
Name = "test",
|
|
Importance = Importance.High,
|
|
Description = "for test",
|
|
};
|
|
AndroidNotificationCenter.RegisterNotificationChannel(channel);
|
|
}
|
|
#else
|
|
//TODO: iOS
|
|
#endif
|
|
}
|
|
|
|
public void InitializeFCM()//파이어베이스 메세징 초기화
|
|
{
|
|
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
|
|
var dependencyStatus = task.Result;
|
|
if (dependencyStatus == DependencyStatus.Available)
|
|
{
|
|
Debug.Log("Google Play version OK");
|
|
|
|
FirebaseMessaging.TokenReceived += OnTokenReceived;
|
|
FirebaseMessaging.MessageReceived += OnMessageReceived;
|
|
//FirebaseMessaging.RequestPermissionAsync().ContinueWithOnMainThread(task => {
|
|
// Debug.Log("push permission: " + task.Status.ToString());
|
|
//});
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError(string.Format(
|
|
"Could not resolve all Firebase dependencies: {0}",
|
|
dependencyStatus
|
|
));
|
|
}
|
|
});
|
|
}
|
|
|
|
public void OnTokenReceived(object sender, TokenReceivedEventArgs token)//알람을 받았을때 디버그 확인용. 에디터에서는 알람을 받지 못 한다.
|
|
{
|
|
Debug.Log("OnTokenReceived: " + token.Token);
|
|
}
|
|
|
|
public void OnMessageReceived(object sender, MessageReceivedEventArgs e)//알림메세지를 처리하고 안드로이드 푸시를 생성한다. 아이콘의 교체도 여기서 이뤄진다.
|
|
{
|
|
#if UNITY_ANDROID
|
|
//아이콘은 C:\Users\stone110\Documents\zombiestone_test\Zombie\Library\Bee\Android\Prj\IL2CPP\Gradle\unityLibrary\FirebaseApp.androidlib\res 의 drawable 쪽에 있음.
|
|
string type = "";
|
|
string title = "";
|
|
string body = "";
|
|
|
|
// for notification message
|
|
if (e.Message.Notification != null)
|
|
{
|
|
type = "notification";
|
|
title = e.Message.Notification.Title;
|
|
body = e.Message.Notification.Body;
|
|
}
|
|
// for data message
|
|
else if (e.Message.Data.Count > 0)
|
|
{
|
|
type = "data";
|
|
title = e.Message.Data["title"];
|
|
body = e.Message.Data["body"];
|
|
}
|
|
Debug.Log("message type: " + type + ", title: " + title + ", body: " + body);
|
|
|
|
var notification = new AndroidNotification();
|
|
notification.SmallIcon = "icon_0";
|
|
notification.Title = title;
|
|
notification.Text = body;
|
|
notification.FireTime = System.DateTime.Now;
|
|
|
|
notification.SmallIcon = "icon_0";
|
|
|
|
if (apiLevel >= 26)
|
|
{
|
|
AndroidNotificationCenter.SendNotification(notification, CHANNEL_ID);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Android 8.0 이상의 디바이스에서만 푸시 알림이 정상적으로 표시됩니다.");
|
|
}
|
|
#else
|
|
//TODO: iOS
|
|
#endif
|
|
}
|
|
}
|