Esempio n. 1
0
        private static bool MigrateFromLegacySettings(NotificationSettingsManager settingsManager)
        {
            const string k_LegacyAssetPath = "Assets/Editor/com.unity.mobile.notifications/NotificationSettings.asset";

            if (!File.Exists(k_LegacyAssetPath))
            {
                return(false);
            }

            var settingsManagerLegacy = AssetDatabase.LoadAssetAtPath <NotificationSettingsManager>(k_LegacyAssetPath);

            if (settingsManagerLegacy == null)
            {
                return(false);
            }

            settingsManager.ToolbarIndex = settingsManagerLegacy.ToolbarIndex;
            settingsManager.m_iOSNotificationSettingsValues     = settingsManagerLegacy.m_iOSNotificationSettingsValues;
            settingsManager.m_AndroidNotificationSettingsValues = settingsManagerLegacy.m_AndroidNotificationSettingsValues;
            settingsManager.DrawableResources = settingsManagerLegacy.DrawableResources;

            AssetDatabase.DeleteAsset(k_LegacyAssetPath);
            DeleteEmptyDirectoryAsset("Assets/Editor/com.unity.mobile.notifications");

            return(true);
        }
Esempio n. 2
0
        private void InjectAndroidManifest(string projectPath)
        {
            var manifestPath = string.Format("{0}/src/main/AndroidManifest.xml", projectPath);

            if (!File.Exists(manifestPath))
            {
                throw new FileNotFoundException(string.Format("'{0}' doesn't exist.", manifestPath));
            }

            XmlDocument manifestDoc = new XmlDocument();

            manifestDoc.Load(manifestPath);

            InjectReceivers(manifestPath, manifestDoc);

            var settings = NotificationSettingsManager.Initialize().AndroidNotificationSettingsFlat;

            var useCustomActivity = (bool)settings.Find(i => i.key == "UnityNotificationAndroidUseCustomActivity").val;

            if (useCustomActivity)
            {
                var customActivity = (string)settings.Find(i => i.key == "UnityNotificationAndroidCustomActivityString").val;
                AppendAndroidMetadataField(manifestPath, manifestDoc, "custom_notification_android_activity", customActivity);
            }

            var enableRescheduleOnRestart = (bool)settings.Find(i => i.key == "UnityNotificationAndroidRescheduleOnDeviceRestart").val;

            if (enableRescheduleOnRestart)
            {
                AppendAndroidMetadataField(manifestPath, manifestDoc, "reschedule_notifications_on_restart", "true");
                AppendAndroidPermissionField(manifestPath, manifestDoc, "android.permission.RECEIVE_BOOT_COMPLETED");
            }

            manifestDoc.Save(manifestPath);
        }
            /// <summary>
            /// Remove all images from notification settings.
            /// </summary>
            public static void ClearDrawableResources()
            {
                var manager = NotificationSettingsManager.Initialize();

                manager.ClearDrawableResources();
#if UNITY_2020_2_OR_NEWER
                SettingsService.RepaintAllSettingsWindow();
#endif
            }
            /// <summary>
            /// Remove icon with given identifier from notification settings.
            /// </summary>
            /// <param name="id">ID of the image to remove</param>
            public static void RemoveDrawableResource(string id)
            {
                var manager = NotificationSettingsManager.Initialize();

                manager.RemoveDrawableResourceById(id);
#if UNITY_2020_2_OR_NEWER
                SettingsService.RepaintAllSettingsWindow();
#endif
            }
            /// <summary>
            /// Add image to notification settings.
            /// </summary>
            /// <param name="id">Image identifier</param>
            /// <param name="image">Image texture, must be obtained from asset database</param>
            /// <param name="type">Image type</param>
            public static void AddDrawableResource(string id, Texture2D image, NotificationIconType type)
            {
                var manager = NotificationSettingsManager.Initialize();

                manager.AddDrawableResource(id, image, type);
#if UNITY_2020_2_OR_NEWER
                SettingsService.RepaintAllSettingsWindow();
#endif
            }
        DrawableResourceData GetElementData(int index)
        {
            var res = NotificationSettingsManager.Initialize().TrackedResourceAssets;

            if (index < res.Count)
            {
                return(res[index]);
            }
            return(null);
        }
Esempio n. 7
0
        private static void SetSettingValue <T>(BuildTargetGroup target, string key, T value)
        {
            var manager = NotificationSettingsManager.Initialize();

            NotificationSetting setting = GetSetting(target, key);

            if (setting != null)
            {
                setting.Value = value;
                manager.SaveSetting(setting, target);
            }
        }
        static SettingsProvider CreateMobileNotificationsSettingsProvider()
        {
            var settingsAsset = NotificationSettingsManager.Initialize();

            if (settingsAsset != null)
            {
                var provider = AssetSettingsProvider.CreateProviderFromObject("Project/Mobile Notifications", settingsAsset);
                provider.label = "Mobile Notifications";
                return(provider);
            }

            return(null);
        }
Esempio n. 9
0
        private static NotificationSetting GetSetting(BuildTargetGroup target, string key)
        {
            var manager = NotificationSettingsManager.Initialize();

            NotificationSetting setting = null;

            if (target == BuildTargetGroup.Android)
            {
                setting = manager.AndroidNotificationSettingsFlat.Find(i => i.Key == key);
            }
            else if (target == BuildTargetGroup.iOS)
            {
                setting = manager.iOSNotificationSettingsFlat.Find(i => i.Key == key);
            }

            return(setting);
        }
Esempio n. 10
0
        private void Initialize()
        {
            label             = "Mobile Notifications";
            m_SettingsManager = NotificationSettingsManager.Initialize();

            // These two are for ReorderableList.
            m_SettingsManagerObject = new SerializedObject(m_SettingsManager);
            m_DrawableResources     = m_SettingsManagerObject.FindProperty("DrawableResources");

            // ReorderableList is only used to draw the drawable resources for Android settings.
            InitReorderableList();

            Undo.undoRedoPerformed += () =>
            {
                m_SettingsManagerObject.UpdateIfRequiredOrScript();
                Repaint();
            };
        }
Esempio n. 11
0
        private void CopyNotificationResources(string projectPath)
        {
            // The projectPath points to the the parent folder instead of the actual project path.
            if (!Directory.Exists(Path.Combine(projectPath, "src")))
            {
                projectPath = Path.Combine(projectPath, PlayerSettings.productName);
            }

            // Get the icons set in the UnityNotificationEditorManager and write them to the res folder, then we can use the icons as res.
            var icons = NotificationSettingsManager.Initialize().GenerateDrawableResourcesForExport();

            foreach (var icon in icons)
            {
                var fileInfo = new FileInfo(string.Format("{0}/src/main/res/{1}", projectPath, icon.Key));
                if (fileInfo.Directory != null)
                {
                    fileInfo.Directory.Create();
                    File.WriteAllBytes(fileInfo.FullName, icon.Value);
                }
            }
        }
Esempio n. 12
0
        public static NotificationSettingsManager Initialize()
        {
            if (s_SettingsManager != null)
            {
                return(s_SettingsManager);
            }

            var  settingsManager = CreateInstance <NotificationSettingsManager>();
            bool dirty           = false;

            if (File.Exists(k_SettingsPath))
            {
                var settingsJson = File.ReadAllText(k_SettingsPath);
                if (!string.IsNullOrEmpty(settingsJson))
                {
                    EditorJsonUtility.FromJsonOverwrite(settingsJson, settingsManager);
                }
            }
            else
            {
                // Only migrate if there is no new settings asset under k_SettingsPath.
                dirty = MigrateFromLegacySettings(settingsManager);
            }

            if (settingsManager.m_iOSNotificationSettingsValues == null)
            {
                settingsManager.m_iOSNotificationSettingsValues = new NotificationSettingsCollection();
                dirty = true;
            }

            if (settingsManager.m_AndroidNotificationSettingsValues == null)
            {
                settingsManager.m_AndroidNotificationSettingsValues = new NotificationSettingsCollection();
                dirty = true;
            }

            // Create the settings for iOS.
            settingsManager.iOSNotificationSettings = new List <NotificationSetting>()
            {
                new NotificationSetting(
                    "UnityNotificationRequestAuthorizationOnAppLaunch",
                    "Request Authorization on App Launch",
                    "It's recommended to make the authorization request during the app's launch cycle. If this is enabled the authorization pop-up will show up immediately during launching. Otherwise you need to manually create an AuthorizationRequest before sending or receiving notifications.",
                    settingsManager.GetOrAddNotificationSettingValue("UnityNotificationRequestAuthorizationOnAppLaunch", true, false),
                    dependencies: new List <NotificationSetting>()
                {
                    new NotificationSetting(
                        "UnityNotificationDefaultAuthorizationOptions",
                        "Default Notification Authorization Options",
                        "Configure the notification interaction types which will be included in the authorization request if \"Request Authorization on App Launch\" is enabled.",
                        settingsManager.GetOrAddNotificationSettingValue("UnityNotificationDefaultAuthorizationOptions",
                                                                         AuthorizationOption.Alert | AuthorizationOption.Badge | AuthorizationOption.Sound, false)),
                }),
                new NotificationSetting(
                    "UnityAddRemoteNotificationCapability",
                    "Enable Push Notifications",
                    "Enable this to add the push notification capability to the Xcode project, also to retrieve the device token from an AuthorizationRequest.",
                    settingsManager.GetOrAddNotificationSettingValue("UnityAddRemoteNotificationCapability", false, false),
                    false,
                    new List <NotificationSetting>()
                {
                    new NotificationSetting(
                        "UnityNotificationRequestAuthorizationForRemoteNotificationsOnAppLaunch",
                        "Register for Push Notifications on App Launch",
                        "Enable this to automatically register your app with APNs after launching to receive remote notifications. You need to manually create an AuthorizationRequest to get the device token.",
                        settingsManager.GetOrAddNotificationSettingValue("UnityNotificationRequestAuthorizationForRemoteNotificationsOnAppLaunch", false, false)),
                    new NotificationSetting(
                        "UnityRemoteNotificationForegroundPresentationOptions",
                        "Remote Notification Foreground Presentation Options",
                        "Configure the default presentation options for received remote notifications. In order to use the specified presentation options, your app must have received the authorization (the user might change it at any time).",
                        settingsManager.GetOrAddNotificationSettingValue("UnityRemoteNotificationForegroundPresentationOptions", (PresentationOption)iOSPresentationOption.All, false)),
                    new NotificationSetting("UnityUseAPSReleaseEnvironment",
                                            "Enable Release Environment for APS",
                                            "Enable this when signing the app with a production certificate.",
                                            settingsManager.GetOrAddNotificationSettingValue("UnityUseAPSReleaseEnvironment", false, false),
                                            false),
                }),
                new NotificationSetting("UnityUseLocationNotificationTrigger",
                                        "Include CoreLocation Framework",
                                        "Include the CoreLocation framework to use the iOSNotificationLocationTrigger in your project.",
                                        settingsManager.GetOrAddNotificationSettingValue("UnityUseLocationNotificationTrigger", false, false),
                                        false)
            };

            // Create the settings for Android.
            settingsManager.AndroidNotificationSettings = new List <NotificationSetting>()
            {
                new NotificationSetting(
                    "UnityNotificationAndroidRescheduleOnDeviceRestart",
                    "Reschedule on Device Restart",
                    "Enable this to automatically reschedule all non-expired notifications after device restart. By default AndroidSettings removes all scheduled notifications after restarting.",
                    settingsManager.GetOrAddNotificationSettingValue("UnityNotificationAndroidRescheduleOnDeviceRestart", false, true)),
                new NotificationSetting(
                    "UnityNotificationAndroidUseCustomActivity",
                    "Use Custom Activity",
                    "Enable this to override the activity which will be opened when the user taps the notification.",
                    settingsManager.GetOrAddNotificationSettingValue("UnityNotificationAndroidUseCustomActivity", false, true),
                    dependencies: new List <NotificationSetting>()
                {
                    new NotificationSetting(
                        "UnityNotificationAndroidCustomActivityString",
                        "Custom Activity Name",
                        "The full class name of the activity which will be assigned to the notification.",
                        settingsManager.GetOrAddNotificationSettingValue("UnityNotificationAndroidCustomActivityString", "com.unity3d.player.UnityPlayerActivity", true))
                })
            };

            settingsManager.SaveSettings(dirty);

            s_SettingsManager = settingsManager;
            return(s_SettingsManager);
        }
        void OnEnable()
        {
            manager = NotificationSettingsManager.Initialize();
            manager.CustomEditor = this;

            if (target == null)
            {
                return;
            }

            m_Target = new SerializedObject(target);

            m_ResourceAssets = serializedObject.FindProperty("TrackedResourceAssets");

            m_ReorderableList = new ReorderableList(serializedObject, m_ResourceAssets, false, true, true, true);
            m_ReorderableList.elementHeight = kSlotSize + kIconSpacing;

            m_ReorderableList.showDefaultBackground = false;

            m_ReorderableList.drawHeaderCallback = (rect) =>
            {
                if (Event.current.type == EventType.Repaint)
                {
                    var headerBackground = new GUIStyle("RL Header");

                    var paddedRect = GetContentRect(rect,
                                                    1f,
                                                    (ReorderableList.Defaults.padding + 2f) * -1);

                    headerBackground.Draw(
                        paddedRect,
                        false, false, false, false);

                    var labelRect = GetContentRect(paddedRect, 0f, 3f);

                    GUI.Label(labelRect, "Notification Icons", EditorStyles.label);
                }
            };
            m_ReorderableList.onAddCallback = (list) =>
            {
                AddIconDataElement(list);
            };

            m_ReorderableList.onRemoveCallback = (list) =>
            {
                RemoveIconDataElement(list);
            };

            m_ReorderableList.onCanAddCallback = (list) =>
                                                 CanAddCallbackDelegate(list);

            m_ReorderableList.drawElementBackgroundCallback = (rect, index, active, focused) =>
            {
                if (!(Event.current.type == EventType.Repaint))
                {
                    return;
                }

                var evenRow = new GUIStyle("CN EntryBackEven");
                var oddRow  = new GUIStyle("CN EntryBackOdd");

                var bg = index % 2 == 0 ? evenRow : oddRow;
                bg.Draw(rect, false, false, false, false);
                ReorderableList.defaultBehaviours.DrawElementBackground(rect, index, active, focused, true);
            };


            m_ReorderableList.drawElementCallback = (rect, index, selected, focused) =>
                                                    DrawIconDataElement(rect, index, selected, focused);

            m_ReorderableList.elementHeightCallback = index =>
            {
                var data = GetElementData(index);
                if (data == null)
                {
                    return(kSlotSize);
                }
                return(m_ReorderableList.elementHeight +
                       (data.Asset != null && !data.IsValid ? kSlotSize : 0));
            };

            Undo.undoRedoPerformed += UpdateEditorStateOnUndo;
        }