ios本地推送

看了雨松的博客,写了这样一个工具类,再lua里面调用

using UnityEngine;

using System;
using System.Collections;


#if UNITY_IPHONE
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
#endif


public static class NotifyMessage  {
    //初始化推送
    public static void InitNotification()
    {
#if UNITY_IPHONE
UnityEngine.iOS.NotificationServices.RegisterForNotifications (
NotificationType.Alert |
NotificationType.Badge |
NotificationType.Sound);
#endif
        //第一次进入游戏的时候清空,有可能用户自己把游戏冲后台杀死,这里强制清空
        //CleanNotification();
    }


    //本地推送固定的推送时间Fixed time
    public static void NotificationMessageFixedTime(string message, string title, 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,title, newDate, isRepeatDay);
    }


    //本地推送固定的推送时间传一个时间戳
    public static void NotificationMessageTS(string message, string title, long ts, bool isRepeatDay)
    {
        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
        System.DateTime newDate = startTime.AddSeconds(ts);
        NotificationMessage(message,title, newDate, isRepeatDay);
    }


    //本地推送 多少秒后
    public static void NotificationMessage(string message,string title, 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 = UnityEngine.iOS.NotificationServices.localNotificationCount+1; //
localNotification.hasAction = true;
localNotification.alertAction = 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
    }


    //清空所有本地消息
    public static void CleanNotification()
    {
#if UNITY_IPHONE
UnityEngine.iOS.LocalNotification l = new UnityEngine.iOS.LocalNotification (); 
l.applicationIconBadgeNumber = -1; 
UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow (l);
#endif
    }
    //清空所有本地消息延迟一帧执行 这里不好写在lua里面延时一帧
    public static void CleanNotificationDelay()
    {
#if UNITY_IPHONE
        //方法 等待一帧执行  否则会出现小红点删不掉
        UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications (); 
UnityEngine.iOS.NotificationServices.ClearLocalNotifications (); 
#endif


    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值