美文网首页
Unity本地通知(iOS和Android)

Unity本地通知(iOS和Android)

作者: 下雨之後 | 来源:发表于2020-03-31 11:34 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    #if UNITY_IPHONE
    using NotificationServices = UnityEngine.iOS.NotificationServices;
    using NotificationType = UnityEngine.iOS.NotificationType;
    #endif
    
    public class MyLocalNotification : MonoBehaviour
    {
        //本地推送
        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 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 = "这是notificationtest的标题";
                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
    #if UNITY_ANDROID
            if (newDate > System.DateTime.Now) 
            {
                LocalNotification.SendNotification(1,10,"这是notificationtest的标题","这是notificationtest的消息",new Color32(0xff, 0x44, 0x44, 255));
                if (System.DateTime.Now.Hour >= 12) {
                    //System.DateTime dataTimeNextNotify = new System.DateTime(
                    long delay = 24 * 60 * 60 - ((System.DateTime.Now.Hour - 12)* 60 * 60 + System.DateTime.Now.Minute * 60 + System.DateTime.Now.Second);
                    LocalNotification.SendRepeatingNotification(2,delay, 24 * 60 * 60,"这是notificationtest的标题","每天中午12点推送",new Color32(0xff, 0x44, 0x44, 255));
                }
                else 
                {
                    long delay = (12 - System.DateTime.Now.Hour)* 60 * 60 - System.DateTime.Now.Minute * 60 - System.DateTime.Now.Second;
                    LocalNotification.SendRepeatingNotification(2,delay,24 * 60 * 60 ,"这是notificationtest的标题","每天中午12点推送",new Color32(0xff, 0x44, 0x44, 255));  
                }
            }
    #endif
        }
        void Awake()
        {
    #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("每天中午12点推送", 12, true);
            }
            else
            {
                //程序从后台进入前台时
                CleanNotification();
            }
        }
    
        //清空所有本地消息
        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
    #if UNITY_ANDROID
            LocalNotification.CancelNotification(1);
            LocalNotification.CancelNotification(2);
    #endif
        }
    }
    

    参考:https://blog.csdn.net/azhou_hui/article/details/50790870
    iOS参考:http://www.xuanyusong.com/archives/2632
    安卓插件:https://github.com/Agasper/unity-android-notifications

    Android:Unable to merge android manifests
    集成安卓本地通知插件报错
    android-notifications
    解决:修改AndroidManifest.xml debuggable="false"

    相关文章

      网友评论

          本文标题:Unity本地通知(iOS和Android)

          本文链接:https://www.haomeiwen.com/subject/wdqwuhtx.html