private static void UpdateChannelUriOnServer()
        {
            Guid deviceAppInstanceId = GetSettingValue <Guid>(DeviceAppIdKey, false);

            Context.Load(Context.Web, w => w.Title, w => w.Description);

            PushNotificationSubscriber subscriber = Context.Web.GetPushNotificationSubscriber(deviceAppInstanceId);

            Context.Load(subscriber);

            Context.ExecuteQueryAsync(
                (object sender1, ClientRequestSucceededEventArgs args1) =>
            {
                subscriber.ServiceToken = httpChannel.ChannelUri.AbsolutePath;
                subscriber.Update();
                Context.ExecuteQueryAsync(
                    (object sender2, ClientRequestSucceededEventArgs args2) =>
                {
                    ShowMessage("Channel URI updated on server.", "Success");
                },
                    (object sender2, ClientRequestFailedEventArgs args2) =>
                {
                    ShowMessage(args2.Exception.Message, "Error Upating Channel URI");
                });
            },
                (object sender1, ClientRequestFailedEventArgs args1) =>
            {
                // This condition can be ignored. Getting to this point means the subscriber
                // doesn't yet exist on the server, so updating the Channel URI is unnecessary.
                //ShowMessage("Subscriber doesn't exist on server.", "DEBUG");
            });
        }
        public PushNotificationResponse PushToast(PushNotificationSubscriber subscriber, string toastTitle, string toastMessage, string toastParam, ToastIntervalValuesEnum intervalValue)
        {
            // Construct toast notification message from parameter values.
                string toastNotification = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<wp:Notification xmlns:wp=\"WPNotification\">" +
                   "<wp:Toast>" +
                        "<wp:Text1>" + toastTitle + "</wp:Text1>" +
                        "<wp:Text2>" + toastMessage + "</wp:Text2>" +
                        "<wp:Param>" + toastParam + "</wp:Param>" +
                   "</wp:Toast> " +
                "</wp:Notification>";

                return SendPushNotification(NotificationTypeEnum.Toast, subscriber, toastNotification, (int)intervalValue);
        }
Ejemplo n.º 3
0
        public PushNotificationResponse PushToast(PushNotificationSubscriber subscriber, string toastTitle, string toastMessage, string toastParam, ToastIntervalValuesEnum intervalValue)
        {
            // Construct toast notification message from parameter values.
            string toastNotification = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                       "<wp:Notification xmlns:wp=\"WPNotification\">" +
                                       "<wp:Toast>" +
                                       "<wp:Text1>" + toastTitle + "</wp:Text1>" +
                                       "<wp:Text2>" + toastMessage + "</wp:Text2>" +
                                       "<wp:Param>" + toastParam + "</wp:Param>" +
                                       "</wp:Toast> " +
                                       "</wp:Notification>";

            return(SendPushNotification(NotificationTypeEnum.Toast, subscriber, toastNotification, (int)intervalValue));
        }
        private static void SubscribeToService()
        {
            Guid deviceAppInstanceId = GetSettingValue <Guid>(DeviceAppIdKey, false);

            Context.Load(Context.Web, w => w.Title, w => w.Description);

            PushNotificationSubscriber pushSubscriber = Context.Web.RegisterPushNotificationSubscriber(deviceAppInstanceId, httpChannel.ChannelUri.AbsoluteUri);

            Context.Load(pushSubscriber);

            Context.ExecuteQueryAsync
            (
                (object sender, ClientRequestSucceededEventArgs args) =>
            {
                SetRegistrationStatus(true);

                // Indicate that tile and toast notifications can be
                // received by phone shell when phone app is not running.
                if (!httpChannel.IsShellTileBound)
                {
                    httpChannel.BindToShellTile();
                }

                if (!httpChannel.IsShellToastBound)
                {
                    httpChannel.BindToShellToast();
                }

                Context.Load(pushSubscriber.User);
                Context.ExecuteQueryAsync
                (
                    (object sender1, ClientRequestSucceededEventArgs args1) =>
                {
                    ShowMessage(
                        string.Format("Subscriber successfully registered: {0}", pushSubscriber.User.LoginName),
                        "Success");
                },
                    (object sender1, ClientRequestFailedEventArgs args1) =>
                {
                    ShowMessage(args1.Exception.Message, "Error getting User details");
                });
            },
                (object sender, ClientRequestFailedEventArgs args) =>
            {
                ShowMessage(args.Exception.Message, "Error Subscribing");
            });
        }
Ejemplo n.º 5
0
        public async Task <PublishToTagResult> PublishToTagAsync()
        {
            if (!tagOperator.IsTaggingAvailable())
            {
                return(new PublishToTagFailedResult(new TaggingNotAvailableException()));
            }

            string    tag      = model.Tag;
            DynamoTag tagEntry = await tagOperator.tagTableOperator.GetTagAsync(tag);

            if (tagEntry == null)
            {
                return(new PublishToTagFailedResult(new TagNotFoundException(tag)));
            }
            switch (tagEntry.TaggingTypeEnum)
            {
            case PNTagType.Iterative:
                List <DynamoIterativeTag> tags = await tagOperator.iterativeTagTableOperator.GetAllSubscribersForTagAsync(tagEntry.Tag);

                IterativeTagResponseTuple resultTuples = await PublishToIterativeTagAsync(tags);
                await RemoveUnsuccessfulEndpoints(resultTuples);

                var returnTuples = resultTuples.Select(t => new Tuple <PublishToSNSResult, PushNotificationSubscriber>(t.Item1, PushNotificationSubscriber.From(t.Item2.Subscriber)));
                return(new PublishToIterativeTagResult(tag, returnTuples));

            case PNTagType.SNSTopic:
                PublishToSNSResult result = await PublishToSnsTopicTag(tagEntry.SnsTopicArn);

                return(new PublishToSNSTopicTagResult(tag, result));

            default:
                throw new TagNotFoundException(tag);
            }
        }
        private PushNotificationResponse SendPushNotification(NotificationTypeEnum notificationType, PushNotificationSubscriber subscriber, string message, int intervalValue)
        {
            // Create HTTP Web Request object.
                string subscriptionUri = subscriber.ServiceToken;
                HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);

                // MPNS expects a byte array, so convert message accordingly.
                byte[] notificationMessage = Encoding.Default.GetBytes(message);

                // Set the notification request properties.
                sendNotificationRequest.Method = WebRequestMethods.Http.Post;
                sendNotificationRequest.ContentLength = notificationMessage.Length;
                sendNotificationRequest.ContentType = "text/xml";
                sendNotificationRequest.Headers.Add("X-MessageID", Guid.NewGuid().ToString());

                switch (notificationType)
                {
                    case NotificationTypeEnum.Tile:
                        sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");
                        break;
                    case NotificationTypeEnum.Toast:
                        sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
                        break;
                    case NotificationTypeEnum.Raw:
                        // A value for the X-WindowsPhone-Target header is not specified for raw notifications.
                        break;
                }

                sendNotificationRequest.Headers.Add("X-NotificationClass", intervalValue.ToString());

                // Merge byte array payload with headers.
                using (Stream requestStream = sendNotificationRequest.GetRequestStream())
                {
                    requestStream.Write(notificationMessage, 0, notificationMessage.Length);
                }

                string statCode = string.Empty;
                PushNotificationResponse notificationResponse;

                try
                {
                    // Send the notification and get the response.
                    HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
                    statCode = Enum.GetName(typeof(HttpStatusCode), response.StatusCode);

                    // Create PushNotificationResponse object.
                    notificationResponse = new PushNotificationResponse((int)intervalValue, subscriber.ServiceToken);
                    notificationResponse.StatusCode = statCode;
                    foreach (string header in WP7Constants.WP_RESPONSE_HEADERS)
                    {
                        notificationResponse.Properties[header] = response.Headers[header];
                    }
                }
                catch (Exception ex)
                {
                    statCode = ex.Message;
                    notificationResponse = new PushNotificationResponse((int)intervalValue, subscriber.ServiceToken);
                    notificationResponse.StatusCode = statCode;
                }

                return notificationResponse;
        }
 public PushNotificationResponse PushRaw(PushNotificationSubscriber subscriber, string rawMessage, RawIntervalValuesEnum intervalValue)
 {
     return SendPushNotification(NotificationTypeEnum.Raw, subscriber, rawMessage, (int)intervalValue);
 }
Ejemplo n.º 8
0
        private PushNotificationResponse SendPushNotification(NotificationTypeEnum notificationType, PushNotificationSubscriber subscriber, string message, int intervalValue)
        {
            // Create HTTP Web Request object.
            string         subscriptionUri         = subscriber.ServiceToken;
            HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);

            // MPNS expects a byte array, so convert message accordingly.
            byte[] notificationMessage = Encoding.Default.GetBytes(message);

            // Set the notification request properties.
            sendNotificationRequest.Method        = WebRequestMethods.Http.Post;
            sendNotificationRequest.ContentLength = notificationMessage.Length;
            sendNotificationRequest.ContentType   = "text/xml";
            sendNotificationRequest.Headers.Add("X-MessageID", Guid.NewGuid().ToString());

            switch (notificationType)
            {
            case NotificationTypeEnum.Tile:
                sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");
                break;

            case NotificationTypeEnum.Toast:
                sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
                break;

            case NotificationTypeEnum.Raw:
                // A value for the X-WindowsPhone-Target header is not specified for raw notifications.
                break;
            }

            sendNotificationRequest.Headers.Add("X-NotificationClass", intervalValue.ToString());

            // Merge byte array payload with headers.
            using (Stream requestStream = sendNotificationRequest.GetRequestStream())
            {
                requestStream.Write(notificationMessage, 0, notificationMessage.Length);
            }

            string statCode = string.Empty;
            PushNotificationResponse notificationResponse;

            try
            {
                // Send the notification and get the response.
                HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
                statCode = Enum.GetName(typeof(HttpStatusCode), response.StatusCode);

                // Create PushNotificationResponse object.
                notificationResponse            = new PushNotificationResponse((int)intervalValue, subscriber.ServiceToken);
                notificationResponse.StatusCode = statCode;
                foreach (string header in WP7Constants.WP_RESPONSE_HEADERS)
                {
                    notificationResponse.Properties[header] = response.Headers[header];
                }
            }
            catch (Exception ex)
            {
                statCode                        = ex.Message;
                notificationResponse            = new PushNotificationResponse((int)intervalValue, subscriber.ServiceToken);
                notificationResponse.StatusCode = statCode;
            }

            return(notificationResponse);
        }
Ejemplo n.º 9
0
 public PushNotificationResponse PushRaw(PushNotificationSubscriber subscriber, string rawMessage, RawIntervalValuesEnum intervalValue)
 {
     return(SendPushNotification(NotificationTypeEnum.Raw, subscriber, rawMessage, (int)intervalValue));
 }