首页 > 学院 > 开发设计 > 正文

Unity3D使用自带的LocalNotification推送后,Icon上的Badge Number数量消除不掉

2019-11-09 15:43:27
字体:
来源:转载
供稿:网友

本地推送我是这样写的:

LocalNotification localNotification = new LocalNotification();			localNotification.fireDate = newDate;			localNotification.alertBody = message;			localNotification.applicationIconBadgeNumber = 1;			localNotification.hasAction = true;			localNotification.alertAction = "SlotGame1";			if (isRepeatDay){				localNotification.repeatCalendar = UnityEngine.iOS.CalendarIdentifier.ISO8601Calendar;				localNotification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;			}			localNotification.soundName = LocalNotification.defaultSoundName;			NotificationServices.ScheduleLocalNotification(localNotification);然后是得到推送后点进去再退出来的时候,发现图标上的Badge数量还是依然存在:

开始是按照雨松大神的写的:

LocalNotification l = new LocalNotification();		l.applicationIconBadgeNumber = -1;		NotificationServices.PResentLocalNotificationNow(l);		NotificationServices.CancelAllLocalNotifications();		NotificationServices.ClearLocalNotifications();完了之后并没有效果,BadgeNumber还是存在,后面在网上找到另一人写的方法,只需要将上面的代码添加一句话

LocalNotification l = new LocalNotification();		l.applicationIconBadgeNumber = -1;		l.hasAction = false;		NotificationServices.PresentLocalNotificationNow(l);		NotificationServices.CancelAllLocalNotifications();		NotificationServices.ClearLocalNotifications();主要是添加了hasAction,但是我在实际测试的时候,偶尔还是会出现不消失的问题

多次试了之后发现就是有一定延迟,于是在原来的基础上加上一个协程,在PresentLocalNotificationNow之后等一段时间再clear和cancel

IEnumerator CleanNotification(){		#if UNITY_ipHONE		LocalNotification l = new LocalNotification();		l.applicationIconBadgeNumber = -1;		l.hasAction = false;		NotificationServices.PresentLocalNotificationNow(l);		yield return new WaitForSeconds(0.2f);		NotificationServices.CancelAllLocalNotifications();		NotificationServices.ClearLocalNotifications();		#endif	}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表