Beispiel #1
0
        /// <summary>
        /// Initialize the notifications manager.
        /// </summary>
        /// <param name="channels">An optional collection of channels to register, for Android</param>
        /// <exception cref="InvalidOperationException"><see cref="Initialize"/> has already been called.</exception>
        public void Initialize(params GameNotificationChannel[] channels)
        {
            if (Initialized)
            {
                throw new InvalidOperationException("NotificationsManager already initialized.");
            }

            Initialized = true;

#if UNITY_ANDROID
            Platform = new AndroidNotificationsPlatform();

            // Register the notification channels
            var doneDefault = false;
            foreach (GameNotificationChannel notificationChannel in channels)
            {
                if (!doneDefault)
                {
                    doneDefault = true;
                    ((AndroidNotificationsPlatform)Platform).DefaultChannelId = notificationChannel.Id;
                }

                // Wrap channel in Android object
                var androidChannel = new AndroidNotificationChannel(notificationChannel.Id, notificationChannel.Name,
                                                                    notificationChannel.Description,
                                                                    (Importance)notificationChannel.Style)
                {
                    CanBypassDnd         = notificationChannel.HighPriority,
                    CanShowBadge         = notificationChannel.ShowsBadge,
                    EnableLights         = notificationChannel.ShowLights,
                    EnableVibration      = notificationChannel.Vibrates,
                    LockScreenVisibility = (LockScreenVisibility)notificationChannel.Privacy,
                    VibrationPattern     = notificationChannel.VibrationPattern
                };

                AndroidNotificationCenter.RegisterNotificationChannel(androidChannel);
            }
#elif UNITY_IOS
            Platform = new IosNotificationsPlatform();
#endif

            if (Platform == null)
            {
                return;
            }

            PendingNotifications           = new List <PendingNotification>();
            Platform.NotificationReceived += OnNotificationReceived;

            // Check serializer
            if (Serializer == null)
            {
                Serializer = new DefaultSerializer(Path.Combine(Application.persistentDataPath, DefaultFilename));
            }

            OnForegrounding();
        }
Beispiel #2
0
        /// <summary>
        /// Initialize the notifications manager.
        /// </summary>
        /// <param name="settingPath">An optional collection of channels to register, for Android</param>
        /// <exception cref="InvalidOperationException"><see cref="Initialize"/> has already been called.</exception>
        public IEnumerator Initialize(string settingPath)
        {
            if (Initialized)
            {
                yield break;
            }

            var handle = Addressables.LoadAssetAsync <NotificationSetting>($"{settingPath}/{nameof(NotificationSetting)}.asset");

            yield return(handle);

            var setting = handle.Result;

            Initialized = true;


#if UNITY_ANDROID
            Platform = new AndroidNotificationsPlatform();

            // Register the notification channels
            var doneDefault = false;
            foreach (GameNotificationChannel notificationChannel in setting.channels)
            {
                if (!doneDefault)
                {
                    doneDefault = true;
                    ((AndroidNotificationsPlatform)Platform).DefaultChannelId = notificationChannel.Id;
                }

                long[] vibrationPattern = null;
                if (notificationChannel.VibrationPattern != null)
                {
                    vibrationPattern = notificationChannel.VibrationPattern.Select(v => (long)v).ToArray();
                }

                // Wrap channel in Android object
                var androidChannel = new AndroidNotificationChannel(notificationChannel.Id, notificationChannel.Name,
                                                                    notificationChannel.Description,
                                                                    (Importance)notificationChannel.Style)
                {
                    CanBypassDnd         = notificationChannel.HighPriority,
                    CanShowBadge         = notificationChannel.ShowsBadge,
                    EnableLights         = notificationChannel.ShowLights,
                    EnableVibration      = notificationChannel.Vibrates,
                    LockScreenVisibility = (LockScreenVisibility)notificationChannel.Privacy,
                    VibrationPattern     = vibrationPattern
                };

                AndroidNotificationCenter.RegisterNotificationChannel(androidChannel);
            }
#elif UNITY_IOS
            Platform = new iOSNotificationsPlatform();
#endif

            Platform = Platform ?? new DefaultPlatform();

            PendingNotifications           = new List <PendingNotification>();
            Platform.NotificationReceived += OnNotificationReceived;

            // Check serializer
            if (Serializer == null)
            {
                Serializer = new DefaultSerializer(Path.Combine(Application.persistentDataPath, DefaultFilename));
            }

            OnForegrounding();
            Observable.EveryApplicationFocus().FirstOrDefault().Subscribe(OnApplicationFocus);
        }