Skip to content

Instantly share code, notes, and snippets.

@fanhexin
Last active June 10, 2020 01:26
Show Gist options
  • Save fanhexin/30a9cdcd1362bc704eddc4893e0baa6f to your computer and use it in GitHub Desktop.
Save fanhexin/30a9cdcd1362bc704eddc4893e0baa6f to your computer and use it in GitHub Desktop.
A simple wrapper of Unity Mobile Notifications.
using System;
#if UNITY_ANDROID
using Unity.Notifications.Android;
#elif UNITY_IOS
using UnityEngine.iOS;
using CalendarUnit = UnityEngine.iOS.CalendarUnit;
using LocalNotification = UnityEngine.iOS.LocalNotification;
using NotificationServices = UnityEngine.iOS.NotificationServices;
#endif
using UnityEngine;
namespace Game.Util
{
interface ILocalNotification
{
void SendNotification(string title, string content, DateTime triggerTime, TimeSpan? interval = null);
void ClearAllDisplayedNotifications();
void CancelAllNotifications();
}
#if UNITY_ANDROID
public class AndroidLocalNotification : ILocalNotification
{
readonly string _smallIcon;
readonly string _largeIcon;
readonly string _channelId;
public AndroidLocalNotification(
string channelId,
string channelName,
string smallIcon,
string largeIcon)
{
_channelId = channelId;
_smallIcon = smallIcon;
_largeIcon = largeIcon;
var channel = new AndroidNotificationChannel
{
Id = channelId,
Name = channelName,
Description = "Default channel",
Importance = Importance.High,
LockScreenVisibility = LockScreenVisibility.Public,
EnableVibration = true,
CanShowBadge = true
};
AndroidNotificationCenter.RegisterNotificationChannel(channel);
}
public void SendNotification(string title, string content, DateTime triggerTime, TimeSpan? interval = null)
{
var notification = new AndroidNotification
{
Title = title,
Text = content,
FireTime = triggerTime,
SmallIcon = _smallIcon,
LargeIcon = _largeIcon
};
if (interval != null)
{
notification.RepeatInterval = interval.Value;
}
AndroidNotificationCenter.SendNotification(notification, _channelId);
}
public void ClearAllDisplayedNotifications()
{
AndroidNotificationCenter.CancelAllDisplayedNotifications();
}
public void CancelAllNotifications()
{
AndroidNotificationCenter.CancelAllNotifications();
}
}
#endif
#if UNITY_IOS
public class IosLocalNotification : ILocalNotification
{
public IosLocalNotification()
{
NotificationServices.RegisterForNotifications(NotificationType.Alert |
NotificationType.Badge |
NotificationType.Sound);
}
/// <summary>
/// 受api限制,如果是repeat模式,会忽略interval值,直接每天repeat
/// </summary>
/// <param name="title"></param>
/// <param name="content"></param>
/// <param name="triggerTime"></param>
/// <param name="interval"></param>
public void SendNotification(string title, string content, DateTime triggerTime, TimeSpan? interval = null)
{
var notification = new LocalNotification
{
fireDate = triggerTime,
alertBody = content,
applicationIconBadgeNumber = 1,
soundName = LocalNotification.defaultSoundName
};
if (interval != null)
{
if (interval.Value.Days >= 1)
{
notification.repeatInterval = CalendarUnit.Day;
}
else if (interval.Value.Hours >= 1)
{
notification.repeatInterval = CalendarUnit.Hour;
}
else if (interval.Value.Minutes >= 1)
{
notification.repeatInterval = CalendarUnit.Minute;
}
}
NotificationServices.ScheduleLocalNotification(notification);
}
public void ClearAllDisplayedNotifications()
{
NotificationServices.ClearLocalNotifications();
}
public void CancelAllNotifications()
{
NotificationServices.CancelAllLocalNotifications();
}
}
#endif
#if UNITY_EDITOR
public class EditorLocalNotification : ILocalNotification
{
public void SendNotification(string title, string content, DateTime triggerTime, TimeSpan? interval = null)
{
Debug.Log($"{nameof(SendNotification)}--{title}--{content}--{triggerTime}--{interval}");
}
public void ClearAllDisplayedNotifications()
{
Debug.Log(nameof(ClearAllDisplayedNotifications));
}
public void CancelAllNotifications()
{
Debug.Log(nameof(CancelAllNotifications));
}
}
#endif
public class MobileLocalNotification : ILocalNotification
{
readonly ILocalNotification _localNotification;
public MobileLocalNotification()
{
#if UNITY_EDITOR
_localNotification = new EditorLocalNotification();
#elif UNITY_ANDROID
string channelId = "channel_id";
_localNotification = new AndroidLocalNotification(channelId, Application.productName, "small", "large");
#elif UNITY_IOS
_localNotification = new IosLocalNotification();
#endif
}
public void SendNotification(string title, string content, DateTime triggerTime, TimeSpan? interval = null)
{
_localNotification.SendNotification(title, content, triggerTime, interval);
}
public void ClearAllDisplayedNotifications()
{
_localNotification.ClearAllDisplayedNotifications();
}
public void CancelAllNotifications()
{
_localNotification.CancelAllNotifications();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment