Unity iOS本地推送解决方案✨
Posted 时光不染
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity iOS本地推送解决方案✨相关的知识,希望对你有一定的参考价值。
上个项目使用的简易版ios推送解决方案,记录一下,以备后用
该方案需要注意iOS推送权限 Push Notifications
using UnityEngine;
using System.Collections;
#if UNITY_IPHONE
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
#endif
public class Nofication : MonoBehaviour
//本地推送
public static void NotificationMessage(string message,int hour ,int min,int sec,bool isRepeatDay,string title)
int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day= System.DateTime.Now.Day;
System.DateTime newDate = new System.DateTime(year,month,day,hour,min,sec);
NotificationMessage(message,newDate,isRepeatDay,title);
//本地推送 你可以传入一个固定的推送时间
public static void NotificationMessage(string message,System.DateTime newDate,bool isRepeatDay,string title)
#if UNITY_IPHONE
//推送时间需要大于当前时间
if(newDate > System.DateTime.Now)
UnityEngine.iOS.LocalNotification localNotification = new UnityEngine.iOS.LocalNotification();
localNotification.fireDate =newDate;
localNotification.alertBody = message;
localNotification.applicationIconBadgeNumber = 1;
localNotification.hasAction = true;
localNotification.alertAction = title;
localNotification.alertTitle = title;//标题 注释掉可以去掉标题
if(isRepeatDay)
//是否每天定期循环
localNotification.repeatCalendar = UnityEngine.iOS.CalendarIdentifier.ChineseCalendar;
localNotification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;
localNotification.soundName = UnityEngine.iOS.LocalNotification.defaultSoundName;
UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(localNotification);
#endif
void Awake()
DontDestroyOnLoad(gameObject);
//下面这段会调起iOS是否允许推送的授权提示框,可以放到自己想要授权的时机调用这段代码
//是否弹出提示框由iOS系统判定,第一次拒绝后,长时间内不会主动再次弹出。
//开发者可以在iOS获取授权状态,自己做界面跳转授权
#if UNITY_IPHONE
UnityEngine.iOS.NotificationServices.RegisterForNotifications(
NotificationType.Alert |
NotificationType.Badge |
NotificationType.Sound);
#endif
//第一次进入游戏的时候清空,有可能用户自己把游戏从后台杀死,这里强制清空
CleanNotification();
void OnApplicationPause(bool paused)
//程序进入后台时
if(paused)
//10秒后发送
// NotificationMessage("这是notificationtest的推送正文信息",System.DateTime.Now.AddSeconds(10),false);
//每天中午12点推送
NotificationMessage("推送内容1",11,55,0,true,"推送标题");
NotificationMessage("推送内容2",18,55,0,true,"推送标题");
else
//程序从后台进入前台时
CleanNotification();
//清空所有本地消息
public static void CleanNotification()
#if UNITY_IPHONE
UnityEngine.iOS.LocalNotification l = new UnityEngine.iOS.LocalNotification ();
l.applicationIconBadgeNumber = -1;
UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow (l);
UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications ();
UnityEngine.iOS.NotificationServices.ClearLocalNotifications ();
#endif
以上是关于Unity iOS本地推送解决方案✨的主要内容,如果未能解决你的问题,请参考以下文章
Firebase Analytics iOS/Unity 推送通知?
如何在 Unity(C#,目前针对 iOS)中设置每周重复推送通知?