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

参考: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()
	{

	}
}




你可能感兴趣的:(IOS,Unity3D,Unity摸索记录)