Unity3D IOS后台推送

主要是参照雨松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();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值