Skip to content

Commit e01f99e

Browse files
Merge pull request #53 from IkeTheDestroyer/badgeRunOnMainThread
Changed badge access to run on the main thread, fixes issues where in…
2 parents 92eebf3 + 566636b commit e01f99e

File tree

1 file changed

+10
-81
lines changed

1 file changed

+10
-81
lines changed

Parse/ParseInstallation.Unity.cs

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -43,95 +43,24 @@ public void SetDeviceTokenFromData(byte[] deviceToken) {
4343
public int Badge {
4444
get {
4545
if (PlatformHooks.IsIOS) {
46-
// Basically what we want to achieve here is:
47-
// <code>
48-
// if (UnityEngine.iOS.NotificationServices.localNotificationCount > 0) {
49-
// badge = UnityEngine.iOS.NotificationServices.localNotifications[0].applicationIconBadgeNumber;
50-
// }
51-
// </code>
52-
// But because we want to support backward compatibility, we're forced to use Reflection.
53-
// We need to return to the non-reflection way once Unity 5 is stable on 95% developers use Unity 5.
54-
55-
// Warning (iOS Only): Unity 5 namespace for iOS notification is under `iOS.NotificationServices`,
56-
// while Unity 4.x put them under `NotificationServices`. They're basically the same class.
57-
Type notificationServicesType = PlatformHooks.GetTypeFromUnityEngine("iOS.NotificationServices");
58-
if (notificationServicesType == null) {
59-
notificationServicesType = PlatformHooks.GetTypeFromUnityEngine("NotificationServices");
60-
}
61-
62-
// Warning (iOS Only): We want to access `NotificationServices.localNotificationCount`.
63-
if (notificationServicesType != null) {
64-
PropertyInfo localNotificationCountProperty = notificationServicesType.GetProperty("localNotificationCount");
65-
int localNotificationCount = (int)localNotificationCountProperty.GetValue(null, null);
66-
67-
if (localNotificationCount > 0) {
68-
// Warning (iOS Only): Unity 5 namespace for iOS notification is under `iOS.LocalNotification`,
69-
// while Unity 4.x put them under `LocalNotification`. They're basically the same class.
70-
Type localNotificationType = PlatformHooks.GetTypeFromUnityEngine("iOS.LocalNotification");
71-
if (localNotificationType == null) {
72-
localNotificationType = PlatformHooks.GetTypeFromUnityEngine("LocalNotification");
73-
}
74-
75-
// Warning (iOS Only): We want to access `NotificationServices.localNotifications`.
76-
PropertyInfo localNotificationsProperty = notificationServicesType.GetProperty("localNotifications");
77-
IEnumerable localNotifications = (IEnumerable)localNotificationsProperty.GetValue(null, null);
78-
79-
// Warning (iOS Only): We want to access `localNotification.applicationIconBadgeNumber`.
80-
PropertyInfo applicationIconBadgeNumberProperty = localNotificationType.GetProperty("applicationIconBadgeNumber");
81-
IEnumerator enumerator = localNotifications.GetEnumerator();
82-
// Move to first element
83-
enumerator.MoveNext();
84-
int badge = (int)applicationIconBadgeNumberProperty.GetValue(enumerator.Current, null);
85-
SetProperty<int>(badge, "Badge");
46+
PlatformHooks.RunOnMainThread(() => {
47+
if (UnityEngine.iOS.NotificationServices.localNotificationCount > 0) {
48+
SetProperty<int>(UnityEngine.iOS.NotificationServices.localNotifications[0].applicationIconBadgeNumber, "Badge");
8649
}
87-
}
50+
});
8851
}
89-
9052
return GetProperty<int>("Badge");
9153
}
9254
set {
9355
int badge = value;
9456
SetProperty<int>(badge, "Badge");
95-
9657
if (PlatformHooks.IsIOS) {
97-
// Basically, what we want to achieve here is:
98-
// <code>
99-
// UnityEngine.iOS.LocalNotification notification = new UnityEngine.iOS.LocalNotification()
100-
// notification.applicationIconBadgeNumber = badge;
101-
// notification.hasAction = false;
102-
// UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow(notification);
103-
// </code>
104-
// But because we want to support backward compatibility, we're forced to use Reflection.
105-
// We need to return to the non-reflection way once Unity 5 is stable on 95% developers use Unity 5.
106-
107-
// Warning (iOS Only): Unity 5 namespace for iOS notification is under `iOS.LocalNotification`,
108-
// while Unity 4.x put them under `LocalNotification`. They're basically the same class.
109-
Type localNotificationType = PlatformHooks.GetTypeFromUnityEngine("iOS.LocalNotification");
110-
if (localNotificationType == null) {
111-
localNotificationType = PlatformHooks.GetTypeFromUnityEngine("LocalNotification");
112-
}
113-
object localNotification = Activator.CreateInstance(localNotificationType);
114-
115-
// Warning (iOS Only): We want to access `localNotification.applicationIconBadgeNumber`.
116-
PropertyInfo applicationIconBadgeNumberProperty = localNotificationType.GetProperty("applicationIconBadgeNumber");
117-
applicationIconBadgeNumberProperty.SetValue(localNotification, badge, null);
118-
119-
// Warning (iOS Only): We want to access `localNotification.hasAction` of the app.
120-
PropertyInfo hasActionProperty = localNotificationType.GetProperty("hasAction");
121-
hasActionProperty.SetValue(localNotification, false, null);
122-
123-
// Warning (iOS Only): Unity 5 namespace for iOS notification is under `iOS.NotificationServices`,
124-
// while Unity 4.x put them under `NotificationServices`. They're basically the same class.
125-
Type notificationServicesType = PlatformHooks.GetTypeFromUnityEngine("iOS.NotificationServices");
126-
if (notificationServicesType == null) {
127-
notificationServicesType = PlatformHooks.GetTypeFromUnityEngine("NotificationServices");
128-
}
129-
130-
// Warning (iOS Only): We want to call `NotificationServices.PresentLocalNotificationNow(notification)`.
131-
MethodInfo presentLocalNotificationNowMethod = notificationServicesType.GetMethod("PresentLocalNotificationNow");
132-
if (presentLocalNotificationNowMethod != null) {
133-
presentLocalNotificationNowMethod.Invoke(null, new object[] { localNotification });
134-
}
58+
PlatformHooks.RunOnMainThread(() => {
59+
UnityEngine.iOS.LocalNotification notification = new UnityEngine.iOS.LocalNotification ();
60+
notification.applicationIconBadgeNumber = badge;
61+
notification.hasAction = false;
62+
UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow(notification);
63+
});
13564
}
13665
}
13766
}

0 commit comments

Comments
 (0)