原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0427/472.html
	主要是参照雨松MOMO的教程:Unity3D研讨院之IOS本地音讯通知LocalNotification的运用
	= = 之前在网上看的他人的,但是遗忘保存下网址了。 
	假如有人晓得请通知我,我加上。 
	直接上代码。稍有更改。 
	*只能用在ios里面。
	using System.Collections;
	using System.Collections.Generic;
	using UnityEngine;
	using System.Runtime.InteropServices;
	using System;
	using UnityEngine.iOS; //引入命名空间
	public class UnityToIOSManager : MonoBehaviour
	{
	    static UnityToIOSManager mInstance;
	    static long lastTime;
	    int PushIndex;//推送
	    string PushText; //推送信息
	    private void Awake()
	    {
	        Screen.sleepTimeout = SleepTimeout.NeverSleep;//游戏运转时制止休眠,游戏在前台时能够坚持屏幕长亮
	        if (mInstance == null)
	            mInstance = this;
	        //先清空本地音讯,然后注册
	        UnityEngine.iOS.NotificationServices.RegisterForNotifications(NotificationType.Badge | NotificationType.Alert | NotificationType.Sound);
	        CleanNotification();
	        DontDestroyOnLoad(this);
	    }
	//当有多条推送时,每次进入游戏时改换推送音讯
	    private void Push()
	    {
	        #region 推送常州微信公众平台条目记载
	        PushIndex = PlayerPrefs.GetInt("Push");
	        PushIndex++;
	        if (PushIndex > 4)
	            PushIndex = 1;
	        PlayerPrefs.SetInt("Push", PushIndex);
	        PlayerPrefs.Save();
	        PushText = LanguageMananger.GetPushText(PushIndex);
	        #endregion
	    }
	    //后台音讯通知
	    //本地推送
	    public static void NotificationMessage(string message, int hour, bool isRepeatDay)
	    {
	        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, 0, 0);
	        NotificationMessage(message, newDate, isRepeatDay);
	    }
	    //本地推送 你能够传入一个固定的推送时间
	    public static void NotificationMessage(string message, System.DateTime newDate, bool isRepeatDay)
	    {
	        if (isRepeatDay && newDate <= System.DateTime.Now)
	        {
	            newDate = newDate.AddDays(1);
	        }
	        //推送时间需求大于当前时间
	        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;
	            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);
	        }
	    }
	    void OnApplicationPause(bool paused)
	    {
	        //程序进入后台时
	        if (paused)
	        {
	            Push();
	            //每天中午12点推送
	            NotificationMessage(PushText, 19, true);
	            //10s后发送
	            NotificationMessage("10秒后发送",System.DateTime.Now.AddSeconds(10),false);
	            //时间戳
	            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
	            lastTime = Convert.ToInt64(ts.TotalSeconds);
	        }
	        else
	        {
	            //程序从后台进入前台时
	            CleanNotification();
	            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
	            long nowTime = Convert.ToInt64(ts.TotalSeconds);
	            if(nowTime - lastTime > 180)
	            {
	                //这个是判别程序退出到后台超出多久之后
	                //然后停止其他操作,播放广告,或是提示什么的
	            }
	        }
	    }
	    //清空常州网站开发建设一切本地音讯
	    void CleanNotification()
	    {
	        UnityEngine.iOS.LocalNotification l = new UnityEngine.iOS.LocalNotification();
	        l.applicationIconBadgeNumber = -1;
	        UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow(l);
	        Invoke("WaitOneFrameClear",0);
	    }
	    //延迟一帧执行,不然没法清算
	    void WaitOneFrameClear()
	    {
	        UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications();
	        UnityEngine.iOS.NotificationServices.ClearLocalNotifications();
	    }
	}
上篇:上一篇:unity 定时刷新小功用
下篇:下一篇:Unity+IOS GPS后台更新GPS数据




