c1_unity/Assets/Scripts/Common/SDK/NotificationSDKManager.cs
2025-11-03 10:59:33 +08:00

169 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_ANDROID
using Unity.Notifications.Android;
#elif UNITY_IOS
using Unity.Notifications.iOS;
#endif
namespace BF
{
public class NotificationInfo
{
public string title;
public string text;
public int day;
public int hour;
public int minute;
public int second;
public bool repeat;
}
public class NotificationSDKManager : MonoBehaviour
{
private int NotificationId = 1;
private string NotificationTitle = "Daily surprise";
private string NotificationContent = "Let your fingers dance with the music";
private bool isOpenAppFromNotification = false;
void Start()
{
Init();
}
public void Init()
{
// 检查一下app是从推送点开还是正常打开的
#if UNITY_ANDROID
var notificationIntentData = AndroidNotificationCenter.GetLastNotificationIntent();
if (notificationIntentData != null)
{
isOpenAppFromNotification = true;
}
else
{
isOpenAppFromNotification = false;
}
#elif UNITY_IOS
var notification = iOSNotificationCenter.GetLastRespondedNotification();
if (notification != null)
{
isOpenAppFromNotification = true;
}
else
{
isOpenAppFromNotification = false;
}
#else
isOpenAppFromNotification = false;
#endif
}
public void InitPuchContent(string title, string content)
{
NotificationTitle = title;
NotificationContent = content;
}
//重置推送
public void ResetNotificationChannel()
{
#if UNITY_ANDROID
NotificationId = 1;
AndroidNotificationCenter.CancelAllNotifications();//清除上次注册的通知
var channel = new AndroidNotificationChannel()
{
Id = "channel_id",
Name = "Default Channel",
Importance = Importance.High,
Description = "Generic notifications",
CanShowBadge = false,
EnableLights = true,
LockScreenVisibility = LockScreenVisibility.Public
};
AndroidNotificationCenter.RegisterNotificationChannel(channel);
#elif UNITY_IOS
NotificationId = 1;
iOSNotificationCenter.ApplicationBadge = 0;
iOSNotificationCenter.RemoveAllDeliveredNotifications();
iOSNotificationCenter.RemoveAllScheduledNotifications();
#endif
}
/// <summary>
/// 注册通知
/// </summary>
public void RegisterAndSendNotification(string title, string text, int day, int hour, int minute, int second, bool repeat)
{
var notificationInfo = new NotificationInfo()
{
title = title,
text = text,
day = day,
hour = hour,
minute = minute,
second = second,
repeat = repeat
};
SendNotification(notificationInfo);
}
private void SendNotification(NotificationInfo notificationInfo)
{
#if UNITY_ANDROID
var time = GetNotificationTime(notificationInfo);
var notification = new AndroidNotification()
{
Title = notificationInfo.title,
Text = notificationInfo.text,
FireTime = time,
};
AndroidNotificationCenter.SendNotification(notification, "channel_id");
#elif UNITY_IOS
var time = GetNotificationTime(notificationInfo);
var daySpan = new TimeSpan(notificationInfo.day, notificationInfo.hour, notificationInfo.minute, notificationInfo.second);
var timeTrigger = new iOSNotificationTimeIntervalTrigger()
{
TimeInterval = daySpan,
Repeats = notificationInfo.repeat
};
var notification = new iOSNotification()
{
Identifier = "_notification_"+ NotificationId,
Title = notificationInfo.title,
Body = notificationInfo.text,
Badge = 0,
ShowInForeground = true,
ForegroundPresentationOption = (PresentationOption.Alert | PresentationOption.Sound),
CategoryIdentifier = "category_a",
ThreadIdentifier = "thread1",
Trigger = timeTrigger,
};
NotificationId++;
iOSNotificationCenter.ScheduleNotification(notification);
#endif
}
/// <summary>
/// 得到注册通知的时间
/// </summary>
/// <returns></returns>
public DateTime GetNotificationTime(NotificationInfo notificationInfo)
{
var daySpan = new TimeSpan(notificationInfo.day, notificationInfo.hour, notificationInfo.minute, notificationInfo.second);
var dateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
dateTime += daySpan;
// Debug.Log("GetNotificationTime 发出通知时间 : " + dateTime);
return dateTime;
}
public bool GetIsOpenAppFromNotification()
{
return isOpenAppFromNotification;
}
}
}