├── .gitignore ├── Assets ├── Plugins │ ├── Android │ │ ├── NotificationCompat.cs │ │ └── NotificationManager.cs │ └── Notification.cs └── Test.cs ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | [Ll]ibrary/ 2 | [Tt]emp/ 3 | [Oo]bj/ 4 | 5 | # Autogenerated VS/MD solution and project files 6 | *.csproj 7 | *.unityproj 8 | *.sln 9 | *.jar 10 | ProjectSettings 11 | *.apk 12 | -------------------------------------------------------------------------------- /Assets/Plugins/Android/NotificationCompat.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | public class NotificationCompat 5 | { 6 | static AndroidJavaObject currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"); 7 | 8 | public AndroidJavaObject androidJavaObject; 9 | 10 | public class Builder 11 | { 12 | public AndroidJavaObject androidJavaObject; 13 | public Builder() 14 | { 15 | androidJavaObject = new AndroidJavaObject("android.support.v4.app.NotificationCompat$Builder", currentActivity); 16 | } 17 | 18 | public AndroidJavaObject Build() 19 | { 20 | return androidJavaObject.Call("build"); 21 | } 22 | 23 | public Builder SetContentIntent(AndroidJavaObject intent = null) 24 | { 25 | if (intent == null) 26 | { 27 | intent = new AndroidJavaObject("android.content.Intent", "android.intent.action.MAIN"); 28 | AndroidJavaObject component = currentActivity.Call("getComponentName"); 29 | intent.Call("setComponent", component); 30 | } 31 | 32 | 33 | AndroidJavaClass pendingIntentClass = new AndroidJavaClass("android.app.PendingIntent"); 34 | AndroidJavaObject pendingIntent = pendingIntentClass.CallStatic("getActivity", currentActivity, 0, intent, 134217728); 35 | androidJavaObject = androidJavaObject.Call("setContentIntent", pendingIntent); 36 | return this; 37 | } 38 | 39 | public Builder SetContentInfo(string info) 40 | { 41 | androidJavaObject = androidJavaObject.Call("setContentInfo", info); 42 | return this; 43 | } 44 | 45 | public Builder SetContentTitle(string title) 46 | { 47 | androidJavaObject = androidJavaObject.Call("setContentTitle", title); 48 | return this; 49 | } 50 | public Builder SetContentText(string text) 51 | { 52 | androidJavaObject = androidJavaObject.Call("setContentText", text); 53 | return this; 54 | } 55 | 56 | public Builder SetNumber(int number) 57 | { 58 | androidJavaObject = androidJavaObject.Call("setNumber", number); 59 | return this; 60 | } 61 | 62 | public Builder SetProgress(int max, int progress, bool indeterminate) 63 | { 64 | androidJavaObject = androidJavaObject.Call("setProgress", max, progress, indeterminate); 65 | return this; 66 | } 67 | 68 | public Builder SetOnlyAlertOnce(bool onlyAlertOnce) 69 | { 70 | androidJavaObject = androidJavaObject.Call("setOnlyAlertOnce", onlyAlertOnce); 71 | return this; 72 | } 73 | 74 | public Builder SetSound(string uriString) 75 | { 76 | AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); 77 | AndroidJavaObject uri = uriClass.CallStatic("parse", uriString); 78 | androidJavaObject = androidJavaObject.Call("setSound", uri); 79 | return this; 80 | } 81 | 82 | public Builder SetDefaults(Notification.Default defaults) 83 | { 84 | androidJavaObject = androidJavaObject.Call("setDefaults", (int)defaults); 85 | return this; 86 | } 87 | 88 | public Builder SetSubText(string text) 89 | { 90 | androidJavaObject = androidJavaObject.Call("setSubText", text); 91 | return this; 92 | } 93 | 94 | public Builder SetTicker(string tickerText) 95 | { 96 | androidJavaObject = androidJavaObject.Call("setTicker", tickerText); 97 | return this; 98 | } 99 | public Builder SetSmallIcon(Notification.Icon icon = Notification.Icon.App) 100 | { 101 | int num = 0; 102 | 103 | switch (icon) 104 | { 105 | case Notification.Icon.App: 106 | var applicationInfo = currentActivity.Call("getApplicationInfo"); 107 | num = applicationInfo.Get("icon"); 108 | break; 109 | default: 110 | throw new ArgumentOutOfRangeException("icon"); 111 | } 112 | androidJavaObject = androidJavaObject.Call("setSmallIcon", num); 113 | return this; 114 | } 115 | 116 | } 117 | } -------------------------------------------------------------------------------- /Assets/Plugins/Android/NotificationManager.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class NotificationManager 4 | { 5 | private static NotificationManager notificationManager; 6 | private AndroidJavaObject androidJavaObject; 7 | 8 | static AndroidJavaObject currentActivity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic("currentActivity"); 9 | public static NotificationManager GetNotificationManager() 10 | { 11 | if (notificationManager == null) 12 | { 13 | notificationManager = new NotificationManager(); 14 | notificationManager.androidJavaObject = currentActivity.Call("getSystemService", "notification"); 15 | } 16 | return notificationManager; 17 | } 18 | 19 | public void Notify(int id, AndroidJavaObject androidJavaObject) 20 | { 21 | // TODO AndroidJavaRunnableを使うべき? 22 | // currentActivity.Call("runOnUiThread",new AndroidJavaRunnable(() =>{ 23 | // notificationManager.androidJavaObject.Call("notify", id, androidJavaObject); 24 | // })); 25 | notificationManager.androidJavaObject.Call("notify", id, androidJavaObject); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Assets/Plugins/Notification.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | using UnityEngine; 3 | using System.Collections; 4 | /// 5 | /// ここからiOS / Android 両方のNotificationを呼び出せるようにする予定 6 | /// 7 | public class Notification 8 | { 9 | public enum Default 10 | { 11 | All = -1, 12 | Lights = 4, 13 | Sound = 1, 14 | Vibrate = 2, 15 | } 16 | 17 | public enum Icon 18 | { 19 | App 20 | } 21 | // 22 | // public static int localNotificationCount { 23 | // get 24 | // { 25 | // 26 | //#if UNITY_ANDROID 27 | // return 0; 28 | //#endif 29 | // return NotificationServices.localNotificationCount; 30 | // } 31 | // } 32 | // 33 | // public static LocalNotification[] localNotifications 34 | // { 35 | // get 36 | // { 37 | //#if UNITY_ANDROID 38 | // return null; 39 | //#endif 40 | // return NotificationServices.localNotifications; 41 | // } 42 | // } 43 | // 44 | // public static LocalNotification[] scheduledLocalNotifications { 45 | // get 46 | // { 47 | //#if UNITY_ANDROID 48 | // return null; 49 | //#endif 50 | // return NotificationServices.scheduledLocalNotifications; 51 | // } 52 | // } 53 | // 54 | // public static int remoteNotificationCount { 55 | // get 56 | // { 57 | //#if UNITY_ANDROID 58 | // 59 | //#endif 60 | // return NotificationServices.remoteNotificationCount; 61 | // } 62 | // } 63 | // 64 | // public static RemoteNotification[] remoteNotifications 65 | // { 66 | // get 67 | // { 68 | //#if UNITY_ANDROID 69 | // return null; 70 | //#endif 71 | // return NotificationServices.remoteNotifications; 72 | // } 73 | // } 74 | // 75 | // public static RemoteNotificationType enabledRemoteNotificationTypes { 76 | // get 77 | // { 78 | //#if UNITY_ANDROID 79 | // return RemoteNotificationType.Alert; 80 | //#endif 81 | // return NotificationServices.enabledRemoteNotificationTypes; 82 | // } 83 | // } 84 | // 85 | // public static byte[] deviceToken { 86 | // get 87 | // { 88 | //#if UNITY_ANDROID 89 | // return null; 90 | //#endif 91 | // return NotificationServices.deviceToken; 92 | // } 93 | // } 94 | // 95 | // public static string registrationError { 96 | // get 97 | // { 98 | //#if UNITY_ANDROID 99 | // return null; 100 | //#endif 101 | // return NotificationServices.registrationError; 102 | // } 103 | // } 104 | // 105 | // public static LocalNotification GetLocalNotification(int index) 106 | // { 107 | //#if UNITY_ANDROID 108 | // return null; 109 | //#endif 110 | // return NotificationServices.GetLocalNotification(index); 111 | // } 112 | // 113 | // public static void ScheduleLocalNotification(LocalNotification notification) 114 | // { 115 | //#if UNITY_ANDROID 116 | // 117 | //#elif 118 | // NotificationServices.ScheduleLocalNotification(notification); 119 | //#endif 120 | // 121 | // } 122 | // 123 | // public static void PresentLocalNotificationNow(LocalNotification notification) 124 | // { 125 | //#if UNITY_ANDROID 126 | // 127 | //#elif 128 | // NotificationServices.PresentLocalNotificationNow(notification); 129 | //#endif 130 | // 131 | // } 132 | // 133 | // public static void CancelLocalNotification(LocalNotification notification) 134 | // { 135 | //#if UNITY_ANDROID 136 | // 137 | //#elif 138 | // NotificationServices.CancelLocalNotification(notification); 139 | //#endif 140 | // 141 | // } 142 | // 143 | // public static void CancelAllLocalNotifications() 144 | // { 145 | // #if UNITY_ANDROID 146 | // 147 | //#elif 148 | // NotificationServices.CancelAllLocalNotifications(); 149 | //#endif 150 | // 151 | // } 152 | // 153 | // public static RemoteNotification GetRemoteNotification(int index) 154 | // { 155 | //#if UNITY_ANDROID 156 | // return null; 157 | // 158 | //#endif 159 | // return NotificationServices.GetRemoteNotification(index); 160 | // } 161 | // 162 | // public static void ClearLocalNotifications() 163 | // { 164 | // #if UNITY_ANDROID 165 | // 166 | //#elif 167 | // NotificationServices.ClearLocalNotifications(); 168 | //#endif 169 | // 170 | // } 171 | // 172 | // public static void ClearRemoteNotifications() 173 | // { 174 | // #if UNITY_ANDROID 175 | // 176 | //#elif 177 | // NotificationServices.ClearRemoteNotifications(); 178 | //#endif 179 | // 180 | // } 181 | // 182 | // public static void RegisterForRemoteNotificationTypes(RemoteNotificationType notificationTypes) 183 | // { 184 | // #if UNITY_ANDROID 185 | // 186 | //#elif 187 | // NotificationServices.RegisterForRemoteNotificationTypes(notificationTypes); 188 | //#endif 189 | // 190 | // } 191 | // 192 | // public static void UnregisterForRemoteNotifications() 193 | // { 194 | // #if UNITY_ANDROID 195 | // 196 | //#elif 197 | // NotificationServices.UnregisterForRemoteNotifications(); 198 | //#endif 199 | // 200 | // } 201 | } 202 | -------------------------------------------------------------------------------- /Assets/Test.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | 4 | public class Test : MonoBehaviour 5 | { 6 | private WWW www; 7 | 8 | void Start() 9 | { 10 | StartCoroutine(Load()); 11 | } 12 | 13 | IEnumerator Load() 14 | { 15 | NotificationManager manager = NotificationManager.GetNotificationManager(); 16 | NotificationCompat.Builder builder = new NotificationCompat.Builder(); 17 | 18 | builder.SetContentTitle("必要なデータのダウンロード"); 19 | builder.SetContentText("ダウンロード中です"); 20 | builder.SetTicker("ダウンロード中です"); 21 | builder.SetSmallIcon(); 22 | builder.SetProgress(100, 0, true); 23 | 24 | manager.Notify(0, builder.Build()); 25 | 26 | www = new WWW("https://dl.dropboxusercontent.com/u/153254465/Unity%E7%B3%BB/test.unity3d"); 27 | 28 | while (!www.isDone && www.error == null) 29 | { 30 | builder.SetProgress(100, (int)(www.progress * 100), true); 31 | builder.SetNumber((int)(www.progress * 100)); 32 | manager.Notify(0, builder.Build()); 33 | yield return new WaitForEndOfFrame(); 34 | } 35 | builder.SetProgress(0, 0, false); 36 | Notification.Default defauls = Notification.Default.Sound | Notification.Default.Vibrate; 37 | builder.SetDefaults(defauls); 38 | builder.SetTicker("ダウンロードが完了しました"); 39 | builder.SetContentText("ダウンロードが完了しました"); 40 | builder.SetContentIntent(); 41 | manager.Notify(0, builder.Build()); 42 | } 43 | 44 | void OnGUI() 45 | { 46 | if (www != null) 47 | GUILayout.Label(www.progress.ToString()); 48 | if (GUILayout.Button("Retry", GUILayout.Width(256), GUILayout.Height(256))) 49 | { 50 | StartCoroutine(Load()); 51 | } 52 | GUILayout.Label(Time.time.ToString()); 53 | } 54 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The zlib License 2 | ------------------------------------- 3 | Copyright (C) 2013 Keigo Ando 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgment in the product documentation would be 16 | appreciated but is not required. 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source distribution. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | unity-notification 2 | ================== 3 | 4 | iOSとAndroidの通知に関するプラグイン 5 | 6 | **まだAndroidのProgress通知だけです** 7 | 8 | android-support-v4.jarが必要です。 9 | 10 | ``` 11 | /extras/android/support/v4 12 | ``` 13 | ![](http://gyazo.com/688483785aeca798ef52d97b5d99142c.png) 14 | 15 | `Android` 通知 (プログレスバー表示) 16 | 17 | [Android Developers](http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Progress)に書かれているサンプルと同じように実装できます 18 | 19 | ```cs 20 | using UnityEngine; 21 | using System.Collections; 22 | 23 | public class Test : MonoBehaviour 24 | { 25 | private WWW www; 26 | 27 | void Start() 28 | { 29 | StartCoroutine(Load()); 30 | } 31 | 32 | IEnumerator Load() 33 | { 34 | NotificationManager manager = NotificationManager.GetNotificationManager(); 35 | NotificationCompat.Builder builder = new NotificationCompat.Builder(); 36 | 37 | builder.SetContentTitle("必要なデータのダウンロード"); 38 | builder.SetContentText("ダウンロード中です"); 39 | builder.SetTicker("ダウンロード中です"); 40 | builder.SetSmallIcon(); 41 | builder.SetProgress(100, 0, true); 42 | 43 | manager.Notify(0, builder.Build()); 44 | 45 | www = new WWW("https://dl.dropboxusercontent.com/u/153254465/Unity%E7%B3%BB/test.unity3d"); 46 | 47 | while (!www.isDone && www.error == null) 48 | { 49 | builder.SetProgress(100, (int)(www.progress * 100), true); 50 | builder.SetNumber((int)(www.progress * 100)); 51 | manager.Notify(0, builder.Build()); 52 | yield return new WaitForEndOfFrame(); 53 | } 54 | builder.SetProgress(0, 0, false); 55 | Notification.Default defauls = Notification.Default.Sound | Notification.Default.Vibrate; 56 | builder.SetDefaults(defauls); 57 | builder.SetTicker("ダウンロードが完了しました"); 58 | builder.SetContentText("ダウンロードが完了しました"); 59 | builder.SetContentIntent(); 60 | manager.Notify(0, builder.Build()); 61 | } 62 | 63 | void OnGUI() 64 | { 65 | if (www != null) 66 | GUILayout.Label(www.progress.ToString()); 67 | if (GUILayout.Button("Retry", GUILayout.Width(256), GUILayout.Height(256))) 68 | { 69 | StartCoroutine(Load()); 70 | } 71 | GUILayout.Label(Time.time.ToString()); 72 | } 73 | } 74 | ``` 75 | 76 | ![](http://gyazo.com/c5c8f87e2205dfd5ac52d79b06962297.png) 77 | 78 | ![](http://gyazo.com/51918fc1d8e07e6c70a3f337579817e6.png) 79 | --------------------------------------------------------------------------------