基于unity自带方法,实现IOS推送
最初打算使用timeZone设置时区,后来测试的时候,发现添加和不添加没什么区别,IGameNotification是一个推送消息的数据类,这里不做介绍
代码很简单,但是调试的时候,遇到很多奇怪的问题
因为系统不会自动判断是否是重复设置,每调用一次NotificationServices.ScheduleLocalNotification,就会创建一条新的推送信息,所以需要自己添加代码处理
#if UNITY_IOS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
using LocalNotification = UnityEngine.iOS.LocalNotification;
using NotificationSamples;
public class IoSLocalNotification : MonoBehaviour
{
static string NotificationIdKey = "NotificationId";
public void Init()
{
NotificationServices.RegisterForNotifications(
NotificationType.Alert |
NotificationType.Sound |
NotificationType.Badge
);
}
public void ScheduleDayNotification(IGameNotification notification, string timeZone) {
CreateScheduleNotification(notification, timeZone, UnityEngine.iOS.CalendarUnit.Day);
}
public void ScheduleWeekNotification(IGameNotification notification, string timeZone)
{
CreateScheduleNotification(notification, timeZone, UnityEngine.iOS.CalendarUnit.Week);
}
public void OnceNotification(IGameNotification notification, string timeZone)
{
CreateScheduleNotification(notification, timeZone, UnityEngine.iOS.CalendarUnit.Era);
}
void CreateScheduleNotification(IGameNotification notification, string timeZone, UnityEngine.iOS.CalendarUnit interval) {
LocalNotification localNotification;
if (notification.Id != null)
{
CancelLocalNotification(notification.Id.Value);
}
else
{
Debug.LogWarning("IOS ScheduleNotification id can't be null");
return;
}
localNotification = new LocalNotification();
IDictionary newDict = new Dictionary();
newDict.Add(NotificationIdKey , notification.Id.Value + "");
localNotification.userInfo = newDict as IDictionary;
localNotification.alertTitle = notification.Title;
localNotification.alertBody = notification.Body;
if(UnityEngine.iOS.CalendarUnit.Week == interval){
localNotification.fireDate = (notification as NotificationSamples.iOS.IosGameNotification).RepeatWeekDeliveryTime;
}else{
localNotification.fireDate = notification.RepeatDeliveryTime.Value;
}
Debug.LogWarning("create notification " + localNotification.fireDate.ToString("yy-MM-dd-HH-mm-ss"));
// localNotification.applicationIconBadgeNumber = 1;
localNotification.alertAction = notification.Subtitle;
localNotification.hasAction = true;
// localNotification.timeZone = "GMT" + System.DateTime.Now.ToString("%z");
localNotification.repeatInterval = interval;
localNotification.repeatCalendar = UnityEngine.iOS.CalendarIdentifier.ISO8601Calendar;
localNotification.soundName = LocalNotification.defaultSoundName;
NotificationServices.ScheduleLocalNotification(localNotification);
}
public void CancelLocalNotification(int id)
{
LocalNotification[] allNotifications = NotificationServices.scheduledLocalNotifications;
// Debug.Log("CancelLocalNotification.scheduledLocalNotifications------------id" + id + ", length " + allNotifications.Length);
foreach(LocalNotification l in allNotifications)
{
IDictionary userInfo = l.userInfo;
if (userInfo.Contains(NotificationIdKey))
{
if (userInfo[NotificationIdKey].Equals(id + ""))
{
Debug.Log("CancelLocalNotification alertTitle title is " + l.alertTitle + userInfo[NotificationIdKey]);
// l.applicationIconBadgeNumber = -1;
NotificationServices.CancelLocalNotification( l );
}
}
}
Debug.Log("CancelLocalNotification.remain notification count is " + NotificationServices.scheduledLocalNotifications.Length);
}
public void CleanNotification()
{
Debug.Log("------------CleanNotification ");
NotificationServices.CancelAllLocalNotifications();
NotificationServices.ClearLocalNotifications();
// LocalNotification[] allNotifications = NotificationServices.scheduledLocalNotifications;
}
}
#endif
Notification数据类,这个借用的是项目中其他三方插件中的推送数据类
namespace NotificationSamples
{
///
/// Represents a notification that will be delivered for this application.
///
public interface IGameNotification
{
///
/// Gets or sets a unique identifier for this notification.
///
///
///
/// If null, will be generated automatically once the notification is delivered, and then
/// can be retrieved afterwards.
///
/// On some platforms, this might be converted to a string identifier internally.
///
/// A unique integer identifier for this notification, or null (on some platforms) if not explicitly set.
int? Id { get; set; }
///
/// Gets or sets the notification's title.
///
/// The title message for the notification.
string Title { get; set; }
///
/// Gets or sets the body text of the notification.
///
/// The body message for the notification.
string Body { get; set; }
///
/// Gets or sets a subtitle for the notification.
///
/// The subtitle message for the notification.
string Subtitle { get; set; }
///
/// Gets or sets group to which this notification belongs.
///
/// A platform specific string identifier for the notification's group.
string Group { get; set; }
///
/// Gets or sets the badge number for this notification. No badge number will be shown if null.
///
/// The number displayed on the app badge.
int? BadgeNumber { get; set; }
///
/// Gets or sets time to deliver the notification.
///
/// The time of delivery in local time.
DateTime? DeliveryTime { get; set; }
DateTime? RepeatDeliveryTime { get; set; }
bool Repeat{get; set; }
///
/// Gets whether this notification has been scheduled.
///
/// True if the notification has been scheduled with the underlying operating system.
bool Scheduled { get; }
///
/// Notification small icon.
///
string SmallIcon { get; set; }
///
/// Notification large icon.
///
string LargeIcon { get; set; }
string Data {get; set;}
}
}
ios 数据类
#if UNITY_IOS
using System;
using Unity.Notifications.iOS;
using UnityEngine;
using UnityEngine.Assertions;
namespace NotificationSamples.iOS
{
///
/// iOS implementation of .
///
public class IosGameNotification : IGameNotification
{
private readonly iOSNotification internalNotification;
///
/// Gets the internal notification object used by the mobile notifications system.
///
public iOSNotification InternalNotification => internalNotification;
///
///
/// Internally stored as a string. Gets parsed to an integer when retrieving.
///
/// The identifier as an integer, or null if the identifier couldn't be parsed as a number.
public int? Id
{
get
{
if (!int.TryParse(internalNotification.Identifier, out int value))
{
Debug.LogWarning("Internal iOS notification's identifier isn't a number.");
return null;
}
return value;
}
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
internalNotification.Identifier = value.Value.ToString();
}
}
///
public string Title { get => internalNotification.Title; set => internalNotification.Title = value; }
///
public string Body { get => internalNotification.Body; set => internalNotification.Body = value; }
///
public string Subtitle { get => internalNotification.Subtitle; set => internalNotification.Subtitle = value; }
///
///
/// On iOS, this represents the notification's Category Identifier.
///
/// The value of .
public string Group { get => CategoryIdentifier; set => CategoryIdentifier = value; }
///
public int? BadgeNumber
{
get => internalNotification.Badge != -1 ? internalNotification.Badge : (int?)null;
set => internalNotification.Badge = value ?? -1;
}
///
public bool Scheduled { get; private set; }
///
///
/// On iOS, setting this causes the notification to be delivered on a calendar time.
/// If it has previously been manually set to a different type of trigger, or has not been set before,
/// this returns null.
/// The millisecond component of the provided DateTime is ignored.
///
/// A representing the delivery time of this message, or null if
/// not set or the trigger isn't a .
public DateTime? DeliveryTime
{
get
{
if (!(internalNotification.Trigger is iOSNotificationCalendarTrigger calendarTrigger))
{
return null;
}
DateTime now = DateTime.Now;
var result = new DateTime
(
calendarTrigger.Year ?? now.Year,
calendarTrigger.Month ?? now.Month,
calendarTrigger.Day ?? now.Day,
calendarTrigger.Hour ?? now.Hour,
calendarTrigger.Minute ?? now.Minute,
calendarTrigger.Second ?? now.Second,
DateTimeKind.Local
);
return result;
}
set
{
if (!value.HasValue)
{
return;
}
DateTime date = value.Value.ToLocalTime();
internalNotification.Trigger = new iOSNotificationCalendarTrigger
{
Year = date.Year,
Month = date.Month,
Day = date.Day,
Hour = date.Hour,
Minute = date.Minute,
Second = date.Second
};
}
}
public DateTime? RepeatDeliveryTime
{
get
{
if (!(internalNotification.Trigger is iOSNotificationCalendarTrigger calendarTrigger))
{
return null;
}
DateTime now = DateTime.Now;
var result = new DateTime
(
calendarTrigger.Year ?? now.Year,
calendarTrigger.Month ?? now.Month,
calendarTrigger.Day ?? now.Day,
calendarTrigger.Hour ?? now.Hour,
calendarTrigger.Minute ?? now.Minute,
calendarTrigger.Second ?? now.Second,
DateTimeKind.Local
);
return result;
}
set
{
if (!value.HasValue)
{
return;
}
DateTime date = value.Value.ToLocalTime();
Debug.Log("repeat on " + date.Hour + ":" + date.Minute);
internalNotification.Trigger = new iOSNotificationCalendarTrigger
{
// Year = date.Year,
// Month = date.Month,
// Day = date.Day,
Hour = date.Hour,
Minute = date.Minute,
Second = date.Second,
Repeats = true,
};
Repeat = true;
}
}
DateTime repeatWeekDeliveryTime ;
public DateTime RepeatWeekDeliveryTime
{
get
{
if(repeatWeekDeliveryTime == null)
return DateTime.Now;
else
return repeatWeekDeliveryTime;
}
set
{
repeatWeekDeliveryTime = value;
Repeat = true;
}
}
bool repeat;
public bool Repeat{
get
{
return repeat;
}
set
{
repeat = value;
}
}
///
/// The category identifier for this notification.
///
public string CategoryIdentifier
{
get => internalNotification.CategoryIdentifier;
set => internalNotification.CategoryIdentifier = value;
}
///
/// Does nothing on iOS.
///
public string SmallIcon { get => null; set { } }
///
/// Does nothing on iOS.
///
public string LargeIcon { get => null; set { } }
public string Data{
get
{
return internalNotification.Data;
}
set
{
internalNotification.Data = value;
}
}
///
/// Instantiate a new instance of .
///
public IosGameNotification()
{
internalNotification = new iOSNotification
{
ShowInForeground = false, // Deliver in foreground by default
// ForegroundPresentationOption = (PresentationOption.Alert |
// PresentationOption.Sound | PresentationOption.Badge),
};
}
///
/// Instantiate a new instance of from a delivered notification.
///
/// The delivered notification.
internal IosGameNotification(iOSNotification internalNotification)
{
this.internalNotification = internalNotification;
}
///
/// Mark this notifications scheduled flag.
///
internal void OnScheduled()
{
Assert.IsFalse(Scheduled);
Scheduled = true;
}
public override string ToString()
{
return "Id : " + Id + ", Title : " + Title + ", Body : " + Body + ", Data : " + Data;
}
}
}
#endif