コード例 #1
0
        private static List <NotificationApp> Deserialize([ReadOnlyArray] byte[] data)
        {
            using (var m = new MemoryStream(data))
            {
                using (var reader = new BinaryReader(m))
                {
                    var notificationApps = new List <NotificationApp>();

                    while (true)
                    {
                        try
                        {
                            var notificationApp = new NotificationApp
                            {
                                Key     = reader.ReadString(),
                                Name    = reader.ReadString(),
                                Allowed = reader.ReadBoolean()
                            };

                            var iconLenght = reader.ReadInt32();
                            if (iconLenght != 0)
                            {
                                var buffer = reader.ReadBytes(iconLenght);
                                notificationApp.IconData = buffer;
                            }

                            notificationApps.Add(notificationApp);
                        }
                        catch (Exception)
                        {
                            break;
                        }
                    }

                    return(notificationApps);
                }
            }
        }
コード例 #2
0
        public static IAsyncAction UpdateNotifications()
        {
            return(Task.Run(async() =>
            {
                try
                {
                    // Get the toast notifications
                    var notifsInPlatform = await _listener.GetNotificationsAsync(NotificationKinds.Toast);

                    // Reverse their order since the platform returns them with oldest first, we want newest first
                    notifsInPlatform = notifsInPlatform.Reverse().ToList();

                    // First remove any notifications that no longer exist
                    for (var i = 0; i < Notifications.Count; i++)
                    {
                        var existingNotif = Notifications[i];

                        // If not in platform anymore, remove from our list
                        if (notifsInPlatform.Any(n => n.Id == existingNotif.Id))
                        {
                            continue;
                        }
                        Notifications.RemoveAt(i);
                        i--;

                        if ((bool)LocalSettings.Values["IsBackgroundTaskActive"] &&
                            existingNotif.AppInfo.PackageFamilyName != PackageFamilyName)
                        {
                            SendNotification(Type.Remove, existingNotif);
                        }
                    }

                    // Now our list only contains notifications that exist,
                    // but it might be missing new notifications.

                    for (var i = 0; i < notifsInPlatform.Count; i++)
                    {
                        var platNotif = notifsInPlatform[i];

                        var indexOfExisting = FindIndexOfNotification(platNotif.Id);

                        // If we have an existing
                        if (indexOfExisting != -1)
                        {
                            // And if it's in the wrong position
                            if (i == indexOfExisting)
                            {
                                continue;
                            }
                            // Move it to the right position
                            Notifications.Move(indexOfExisting, i);

                            if ((bool)LocalSettings.Values["IsBackgroundTaskActive"] &&
                                platNotif.AppInfo.PackageFamilyName != PackageFamilyName)
                            {
                                SendNotification(Type.Add, platNotif);
                            }

                            // Otherwise, leave it in its place
                        }

                        // Otherwise, notification is new
                        else
                        {
                            // Insert at that position
                            Notifications.Insert(i, platNotif);

                            if (LocalSettings.Values["newSettings"] != null && (bool)LocalSettings.Values["newSettings"])
                            {
                                _oldData = await ReadNotificationApps();
                                LocalSettings.Values["newSettings"] = false;
                            }

                            if (_notificationApps.All(x => x.Key != platNotif.AppInfo.AppUserModelId) &&
                                platNotif.AppInfo.PackageFamilyName != PackageFamilyName)
                            {
                                var notificationApp = new NotificationApp
                                {
                                    Key = platNotif.AppInfo.AppUserModelId,
                                    Name = platNotif.AppInfo.DisplayInfo.DisplayName,
                                    Allowed = true
                                };
                                var stream = Stream.Null;
                                try
                                {
                                    var icon = await platNotif.AppInfo.DisplayInfo.GetLogo(new Size(16, 16)).OpenReadAsync();
                                    stream = icon.AsStreamForRead();
                                }
                                catch (Exception)
                                {
                                    // ignored
                                }
                                var buffer = new byte[stream.Length];
                                await stream.ReadAsync(buffer, 0, buffer.Length);
                                notificationApp.IconData = buffer;
                                _notificationApps.Add(notificationApp);

                                var newData = notificationApp.Serialize();

                                var data = new byte[_oldData.Length + newData.Length];
                                System.Buffer.BlockCopy(_oldData, 0, data, 0, _oldData.Length);
                                System.Buffer.BlockCopy(newData, 0, data, _oldData.Length, newData.Length);

                                await FileIO.WriteBytesAsync(_notificationAppsFile, data);
                            }

                            if ((bool)LocalSettings.Values["IsBackgroundTaskActive"] &&
                                platNotif.AppInfo.PackageFamilyName != PackageFamilyName &&
                                _notificationApps.Any(x => x.Key == platNotif.AppInfo.AppUserModelId && x.Allowed))
                            {
                                SendNotification(Type.Add, platNotif);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }).AsAsyncAction());
        }
コード例 #3
0
 public InscriptionController()
 {
     _APP = new NotificationApp();
 }