Пример #1
0
        private void NotifyUsers(string eventID, CloudStorageAccount storageAccount)
        {
            NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(connectionStringNotification, hubName);
            WnsHeaderCollection   wnsHeaderCollection = new WnsHeaderCollection();

            wnsHeaderCollection.Add("X-WNS-Type", @"wns/raw");
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable       table       = tableClient.GetTableReference("registeredDevices");

            table.CreateIfNotExists();
            //TableQuery<UserEntity> query = new TableQuery<UserEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "registeredDevices"));

            //List<string> usersTag = new List<string>();
            //List<UserEntity> aa = table.ExecuteQuery(new TableQuery<UserEntity>()).ToList();
            //foreach (UserEntity entity in aa)
            //{
            //    usersTag.Add(entity.RowKey);
            //}
            ////var notif = "{ \"data\" : {\"message\":\"" + "EventId " + eventID + "\"}}";
            ////hub.SendGcmNativeNotificationAsync(notif, usersTag);
            //string a = "8736725417766488658-761683147264243211-3";
            var notif = "{\"data\" : {\"message\":\"" + "event added EventId " + eventID + "\"}}";

            //var notif = "{\"registration_ids\": [\""+ a + "\"], \"data\" : {\"message\":\"" + "event added EventId " + eventID + "\"}}";
            hub.SendGcmNativeNotificationAsync(notif);
        }
Пример #2
0
        // https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-windows-store-dotnet-get-started-push
        public async Task InitNotificationsAsync(string userId)
        {
            channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

            const string toastTemplate =
                "<toast>" +
                "<visual>" +
                "<binding template=\"ToastText02\">" +
                "<text id=\"1\">$(title)</text>" +
                "<text id=\"2\">$(content)</text>" +
                "</binding>" +
                "</visual>" +
                "</toast>";

            const string rawTemplate =
                "<root>" +
                "<Group>$(groupId)</Group>" +
                "<Message>$(messageId)</Message>" +
                "<Sender>$(senderId)</Sender>" +
                "<Action>$(action)</Action>" +
                "</root>";

            var hub = new NotificationHub("penscrib-notification-hub",
                                          "Endpoint=sb://penscrib-notifications.servicebus.windows.net/;SharedAccessKeyName=DefaultListenSharedAccessSignature;SharedAccessKey=ZRR3SXi3eQoTSPa5kpMDzeRJrWooFXbIBn5Vgs8lIdw=");

            string[] tags = new string[] { "userId:" + userId };

            await hub.UnregisterAllAsync(channel.Uri);


            var toastresult = await hub.RegisterTemplateAsync(channel.Uri, toastTemplate, "toastNotification", tags);

            WnsHeaderCollection headers = new WnsHeaderCollection();

            headers.Add("X-WNS-Type", @"wns/raw");
            TemplateRegistration registration = new TemplateRegistration(channel.Uri, rawTemplate, "rawNotification", tags, headers);

            var rawresult = await hub.RegisterAsync(registration);

            channel.PushNotificationReceived += OnPushNotification;
        }
Пример #3
0
        private void NotifyManager(string msg, CloudStorageAccount storageAccount)
        {
            //string[] message = msg.Split(' ');
            //string eventID = message[0];
            //string userID = message[1];
            //eventfinderEntities model = new eventfinderEntities();
            //Event eventEntity = model.Events.First(e => e.ID == Int32.Parse(eventID));
            //string managerID = eventEntity.User.ID.ToString();
            NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(connectionStringNotification, hubName);
            WnsHeaderCollection   wnsHeaderCollection = new WnsHeaderCollection();

            wnsHeaderCollection.Add("X-WNS-Type", @"wns/raw");
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            CloudTable       table       = tableClient.GetTableReference("registeredDevices");

            table.CreateIfNotExists();
            //TableQuery<UserEntity> query = new TableQuery<UserEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,"Users" )).Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, managerID));
            //UserEntity manager = table.ExecuteQuery(query).First();
            var notif = "{ \"data\" : {\"message\":\"" + "joined event " + msg + "\"}}";

            //hub.SendGcmNativeNotificationAsync(notif, manager.PhoneID);
            hub.SendGcmNativeNotificationAsync(notif);
        }
 private async void btnCreateRegistration_Click(object sender, EventArgs e)
 {
     using (var form = new RegistrationForm())
     {
         if (form.ShowDialog() != DialogResult.OK)
         {
             return;
         }
         if (form.RegistrationObject == null || form.RegistrationType == null)
         {
             return;
         }
         var bindingList = registrationsBindingSource.DataSource as SortableBindingList<RegistrationInfo>;
         if (bindingList == null)
         {
             return;
         }
         var tagsAsList = form.RegistrationObject[Tags] as List<TagInfo>;
         HashSet<string> tags = null;
         if (tagsAsList != null)
         {
             tags = new HashSet<string>(tagsAsList.Select(t => t.Tag));
         }
         RegistrationInfo registrationInfo = null;
         if (form.RegistrationType == typeof (MpnsRegistrationDescription))
         {
             var registration = tags == null || tags.Count == 0?
                                new MpnsRegistrationDescription(form.RegistrationObject[ChannelUri] as string) :
                                new MpnsRegistrationDescription(form.RegistrationObject[ChannelUri] as string, tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
                 {
                     ChannelUri = registration.ChannelUri.AbsoluteUri,
                     ETag = registration.ETag,
                     ExpirationTime = registration.ExpirationTime,
                     RegistrationId = registration.RegistrationId,
                     Registration = registration,
                     Tags = registration.Tags != null && registration.Tags.Any() ?
                            registration.Tags.Aggregate((a, t) => a + "," + t) :
                            null
                 };
         }
         else if (form.RegistrationType == typeof(MpnsTemplateRegistrationDescription))
         {
             var mpnsHeadersAsList = form.RegistrationObject[MpnsHeaders] as List<NotificationInfo>;
             var mpnsHeaders = new MpnsHeaderCollection();
             if (mpnsHeadersAsList != null)
             {
                 foreach (var item in mpnsHeadersAsList)
                 {
                     mpnsHeaders.Add(item.Name, item.Value);
                 }
             }
             var registration = tags == null || tags.Count == 0 ?
                                new MpnsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                        form.RegistrationObject[BodyTemplate] as string) :
                                mpnsHeaders.Count == 0 ?
                                new MpnsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                        form.RegistrationObject[BodyTemplate] as string,
                                                                        tags) :
                                new MpnsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                        form.RegistrationObject[BodyTemplate] as string,
                                                                        mpnsHeaders,
                                                                        tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
                 {
                 ChannelUri = registration.ChannelUri.AbsoluteUri,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(WindowsRegistrationDescription))
         {
             var registration = tags == null || tags.Count == 0 ?
                                new WindowsRegistrationDescription(form.RegistrationObject[ChannelUri] as string) :
                                new WindowsRegistrationDescription(form.RegistrationObject[ChannelUri] as string, tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
                 {
                 ChannelUri = registration.ChannelUri.AbsoluteUri,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(WindowsTemplateRegistrationDescription))
         {
             var wnsHeadersAsList = form.RegistrationObject[WnsHeaders] as List<NotificationInfo>;
             var wnsHeaders = new WnsHeaderCollection();
             if (wnsHeadersAsList != null)
             {
                 foreach (var item in wnsHeadersAsList)
                 {
                     wnsHeaders.Add(item.Name, item.Value);
                 }
             }
             var registration = tags == null || tags.Count == 0 ?
                                new WindowsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                          form.RegistrationObject[BodyTemplate] as string) :
                                wnsHeaders.Count == 0 ?
                                new WindowsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                           form.RegistrationObject[BodyTemplate] as string,
                                                                           tags) :
                                new WindowsTemplateRegistrationDescription(form.RegistrationObject[ChannelUri] as string,
                                                                           form.RegistrationObject[BodyTemplate] as string,
                                                                           wnsHeaders,
                                                                           tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
                 {
                 ChannelUri = registration.ChannelUri.AbsoluteUri,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(AppleRegistrationDescription))
         {
             var registration = tags == null || tags.Count == 0 ?
                                new AppleRegistrationDescription(form.RegistrationObject[DeviceToken] as string) :
                                new AppleRegistrationDescription(form.RegistrationObject[DeviceToken] as string, tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
             {
                 ChannelUri = registration.DeviceToken,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(AppleTemplateRegistrationDescription))
         {
             var bodyTemplate = form.RegistrationObject.Properties.Any(p => string.Compare(p.Name, BodyTemplate, StringComparison.InvariantCultureIgnoreCase) == 0)
                                    ? form.RegistrationObject[BodyTemplate] as string
                                    : null;
             var registration = tags != null && tags.Count > 0 ?
                                new AppleTemplateRegistrationDescription(form.RegistrationObject[DeviceToken] as string, bodyTemplate, tags) :
                                string.IsNullOrWhiteSpace(bodyTemplate) ?
                                new AppleTemplateRegistrationDescription(form.RegistrationObject[DeviceToken] as string) :
                                new AppleTemplateRegistrationDescription(form.RegistrationObject[DeviceToken] as string, bodyTemplate);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
             {
                 ChannelUri = registration.DeviceToken,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(GcmRegistrationDescription))
         {
             var registration = tags == null || tags.Count == 0 ?
                                new GcmRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string) :
                                new GcmRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string, tags);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
             {
                 ChannelUri = registration.GcmRegistrationId,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         else if (form.RegistrationType == typeof(GcmTemplateRegistrationDescription))
         {
             var bodyTemplate = form.RegistrationObject.Properties.Any(p => string.Compare(p.Name, BodyTemplate, StringComparison.InvariantCultureIgnoreCase) == 0)
                                    ? form.RegistrationObject[BodyTemplate] as string
                                    : null;
             var registration = tags != null && tags.Count > 0 ?
                                new GcmTemplateRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string, bodyTemplate, tags) :
                                string.IsNullOrWhiteSpace(bodyTemplate) ?
                                new GcmTemplateRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string) :
                                new GcmTemplateRegistrationDescription(form.RegistrationObject[GcmRegistrationId] as string, bodyTemplate);
             registration = await notificationHubClient.CreateRegistrationAsync(registration);
             registrationInfo = new RegistrationInfo
             {
                 ChannelUri = registration.GcmRegistrationId,
                 ETag = registration.ETag,
                 ExpirationTime = registration.ExpirationTime,
                 RegistrationId = registration.RegistrationId,
                 Registration = registration,
                 Tags = registration.Tags != null && registration.Tags.Any() ?
                        registration.Tags.Aggregate((a, t) => a + "," + t) :
                        null
             };
         }
         if (registrationInfo == null)
         {
             return;
         }
         writeToLog(string.Format(RegistrationCreatedMessage, 
                                  registrationInfo.PlatformType,
                                  notificationHubDescription.Path));
         bindingList.Add(registrationInfo);
         registrationsBindingSource.DataSource = null;
         registrationsBindingSource.DataSource = new SortableBindingList<RegistrationInfo>(RegistrationInfo.Registrations);
         registrationsDataGridView.DataSource = registrationsBindingSource;
     }
 }