Unity在iOS的本地推送功能

Babybus-u3d技术交流-Unity在iOS的本地推送功能

Unity内部封装了在iOS下的本地推送功能,可以很方便的实现在iOS设备上的简单本地推送。
命名空间为:UnityEngine.iOS

在Unity中的代码和直接用swift的代码基本类似。

具体代码实现:

    static void NotificationMessage(string message,System.DateTime newDate,bool isRepeatDay)  
  {        //推送时间需要大于当前时间
        if(newDate > System.DateTime.Now)     
   {            UnityEngine.iOS.LocalNotificationlocalNotification = new UnityEngine.iO      S.LocalNotification();            
      localNotification.fireDate =newDate;      
      localNotification.alertBody = message;       
      localNotification.applicationIconBadgeNumber = 1;        
      localNotification.hasAction = true;          
      if(isRepeatDay)        
         {                //是否每天定期循环
                localNotification.repeatCalendar = UnityEngine.iOS.CalendarIdentifier.C  hineseCalendar;//中国日历
                localNotification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;//每日推送
          }        
    localNotification.soundName = UnityEngine.iOS.LocalNotification.defaultSound Name;              
   UnityEngine.iOS.NotificationServices.RegisterForNotifications(UnityEngine.iOS.  NotificationType.Alert| UnityEngine.iOS.NotificationType.Badge | UnityEngine.iOS.NotificationType.Sound);
//以特定的类型推送
            UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(localNotification);   
     } 
   }    //游戏退出时调用
    void OnApplicationQuit()
  {        //每天中午12点推送
        NotificationMessage("每天中午12点推送",12,true);  
  }    //游戏进入后台时或者从后台进入前台时调用
    void OnApplicationPause(bool paused) 
   {        //程序进入后台时
        if(paused)     
   {            //每天中午12点推送
            NotificationMessage("每天12点推送",12,true);     
   }        else
        {            //程序从后台进入前台时
            CleanNotification();      
       }    
}    //清除推送消息,在Awake中调用
    void CleanNotification()   
 {        UnityEngine.iOS.LocalNotification ln = new UnityEngine.iOS.LocalNotification();      
      ln.applicationIconBadgeNumber = -1;        
      UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow (ln);   
      UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications ();     
      UnityEngine.iOS.NotificationServices.ClearLocalNotifications ();     }  
 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 跑步是我日常生活的支柱。只要跑步,我便感到快乐。积极地选择磨难,就是将人生的主动权握在自己手中。——村上春...
    Love惊鸿阅读 312评论 0 6
  • 一、网宿 网宿科技正式加入了国际电信联盟标准化部门(ITU-T),成为国内首个加入ITU-T的专业CDN厂商。 二...
    Captain7阅读 185评论 0 0
  • 开始利用工作闲余时间简书写文以来,村长也在同步做一件事:捡英语。自从离开校门踏入职场已经有数个年头,曾经恩师倾囊相...
    村长下乡阅读 334评论 2 1
  • 如果你愿意 请给我写封信 说说那一夜滚烫的你 如果你愿意 请丢掉花折伞 与我狂奔在雨里 如果你愿意 请在日出之前 ...
    非此阅读 371评论 23 22