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

60 篇文章 3 订阅
40 篇文章 0 订阅

参考:http://blog.csdn.net/pz789as/article/details/54948938


本地推送我是这样写的:

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
	}



2017.4.1补充说明:

在后面使用中发现,这种方式,在高版本的IOS中依然无效,下标根本去不掉。后来 网友小灰找到了方法,需要在导出的xcode工程项目中,找到Classes下面的UnityAppController.mm类,在 - (void)applicationDidBecomeActive:(UIApplication*)application 函数中添加下面两句话:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];	
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];


为了不用每次导出都要修改,我根据雨松大神的IOS全自动打包,改了下,将下面的类放置到Assets/Editor下面即可。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Callbacks;
#endif
using System.IO;

public static class XCodePostProcess {
	#if UNITY_EDITOR
	[PostProcessBuildAttribute(100)]
	public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
		if (target != BuildTarget.iOS){
			Debug.LogWarning("Target is not iphone, XCodePostProcess will not run");
			return;
		}

		//得到xcode工程的路径
		string path = Path.GetFullPath(pathToBuiltProject);

		//编辑代码文件
                EditorCode(path);
	}
    private static void EditorCode(string filePath)
    {
		//读取UnityAppController.mm文件
        XClass UnityAppController = new XClass(filePath + "/Classes/UnityAppController.mm"); 
        //在指定代码后面增加一行代码
        UnityAppController.WriteBelow(
			"- (void)applicationDidBecomeActive:(UIApplication*)application\n{",
			"\t\n[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];" +
			"\t\n[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];"
		);
    }
	#endif
}

public partial class XClass: System.IDisposable{
	private string filePath;
	public XClass(string fPath){
		filePath = fPath;
		if (!File.Exists(filePath)){
			Debug.LogError(filePath + " 路径下文件不存在");
			return;
		}
	}
	public void WriteBelow(string below, string text){
		StreamReader streamReader = new StreamReader(filePath);
		string text_all = streamReader.ReadToEnd();
		streamReader.Close();

		int beginIndex = text_all.IndexOf(below);
		if (beginIndex == -1){
			Debug.LogError(filePath + " 中没有找到标识 " + below);
			return;
		}
		int endIndex = text_all.LastIndexOf("\n", beginIndex + below.Length);
		text_all = text_all.Substring(0, endIndex) + "\n"+text+"\n" + text_all.Substring(endIndex);
		StreamWriter streamWriter = new StreamWriter(filePath);
		streamWriter.Write(text_all);
		streamWriter.Close();
	}
	public void Replace(string below, string newText)
	{
		StreamReader streamReader = new StreamReader(filePath);
		string text_all = streamReader.ReadToEnd();
		streamReader.Close();

		int beginIndex = text_all.IndexOf(below);
		if(beginIndex == -1){
			Debug.LogError(filePath + " 中没有找到标识 " + below);
			return; 
		}

		text_all =  text_all.Replace(below,newText);
		StreamWriter streamWriter = new StreamWriter(filePath);
		streamWriter.Write(text_all);
		streamWriter.Close();
	}

	public void Dispose()
	{

	}
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏小败在路上

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值