Unity3D IOS后台推送
2018-01-15 14:31
190 查看
主要是参照雨松MOMO的教程:Unity3D研究院之IOS本地消息通知LocalNotification的使用(六十七)
但是有一些小问题,有人进行了修改。
= = 之前在网上看的别人的,但是忘记保留下网址了。
如果有人知道请告诉我,我加上。
直接上代码。稍有更改。
*只能用在ios里面。
但是有一些小问题,有人进行了修改。
= = 之前在网上看的别人的,但是忘记保留下网址了。
如果有人知道请告诉我,我加上。
直接上代码。稍有更改。
*只能用在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(); } }
相关文章推荐
- android系统和ios系统是如何实现推送的,ios为什么没有后台推送
- IOS 基于APNS消息推送原理与实现(JAVA后台)
- 转:IOS 基于APNS消息推送原理与实现(JAVA后台)
- iOS 和 Android 的后台推送工作原理各是如何?有什么区别?
- 浅谈iOS和Android后台实时消息推送的原理和区别
- iOS- iOS 和 Android 的后台推送原理各是什么?有什么区别?
- IOS 基于APNS消息推送原理与实现(JAVA后台)
- iOS 基于APNS消息推送原理与实现(包括JAVA后台代码)
- ios后台推送那点事
- iOS推送后台唤醒
- IOS使用个推前台可以收到推送消息后台收不到
- IOS 基于APNS消息推送原理与实现(JAVA后台)--转
- Unity3D游戏中Android和iOS本地推送通知
- iOS 和 Android 的后台推送原理
- iOS- iOS 和 Android 的后台推送原理各是什么?有什么区别?
- IOS 基于APNS消息推送原理与实现(JAVA后台)
- iOS推送&PHP后台
- IOS 基于APNS消息推送原理与实现(JAVA后台)
- iOS 推送后台语音播报
- Android与IOS的后台与推送对比