Monday, March 25, 2013

Working With Push Notification


Toast Notification
Following are the steps to implement pushNotification in WindowsPhone
  1. You need to create a push channel
  2. Use this PushChannel to get the Channel URI, and also set some handlers like:
    • ChannelUriUpdated
    • ErrorOccurred
    • ShellToastNotificationReceived
  1. Use this channel URI to get the MPNSTokenID.
  2. This MPNSTokenID will be used to send the push notification.
  3. Receive this notification and use the data accordingly.


    public void SetupPushChannel()
    {
    HttpNotificationChannel pushChannel;
    string channelName = "ToastSampleChannel"; // any Name
    pushChannel = HttpNotificationChannel.Find(channelName);


    // If the channel was not found, then create a new connection to the push service.
    if (pushChannel == null)
    {
    pushChannel = new HttpNotificationChannel(channelName);

    pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
    pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);

    // Register for this notification only if you need to receive the notifications while your application is running.
    pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);

    pushChannel.Open();

    // Bind this new channel for toast events.
    pushChannel.BindToShellToast();


    }
    else
    {
    // The channel was already open, so just register for all the events.
    pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
    pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);

    // Register for this notification only if you need to receive the notifications while your application is running.
    pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(PushChannel_ShellToastNotificationReceived);
    if (pushChannel.ChannelUri != null)
    {
    // Display the URI for testing purposes. Normally, the URI would be passed back to your web service at this point.
    Debug.WriteLine(pushChannel.ChannelUri.ToString());

    Setting.ChannelUri = pushChannel.ChannelUri.ToString();
    int start = Setting.ChannelUri.LastIndexOf('/') + 1;
    int length = Setting.ChannelUri.Length – start;
    // This MPNSTokenID should be used to send push on Device
    Setting.MPNSTokenID = Setting.ChannelUri.Substring(start, length);
    }
    }
    }

    void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {
    Setting.ChannelUri = e.ChannelUri.ToString();
    int start = Setting.ChannelUri.LastIndexOf('/') + 1;
    int length = Setting.ChannelUri.Length – start;

    // This MPNSTokenID should be used to send push on Device
    Setting.MPNSTokenID = Setting.ChannelUri.Substring(start, length);
    }

    void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {
    //Error handling logic for your particular application would be here.
    Deployment.Current.Dispatcher.BeginInvoke(() =>
    {
    MessageBox.Show(String.Format("A push notification {0} error occurred. {1} ({2}) {3}", e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData));
    SetupPushChannel();
    });
    }




    void PushChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
    {
    StringBuilder message = new StringBuilder();
    foreach(KeyValuePair<string, string> keyValue in e.Collection)
    message.AppendFormat("Key {0}:, Value {1}\n", keyValue.Key, keyValue.Value);
    MessageBox.Show(message.ToString());

    //To-Do now you got the push in keyvalue-pair use them to show your custom messages
    }

    I Think This post will be helpful to you. Post your comments for make it better.

    Keep Coding

    Thanks


Thursday, January 17, 2013

Find the IPAddress of any User


protected void Page_Load(object sender, EventArgs e)

       {

            string ipadd = string.Empty;

            //"HTTP_X_FORWARDED_FOR" will return the user ipadd if client is behind proxy server          

            ipadd = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (string.IsNullOrEmpty(ipadd))

            {

                //"REMOTE_ADDR" will return the ipaddress of router                

                ipadd = Request.ServerVariables["REMOTE_ADDR"];

                Label2.Text ="your ipaddress is" + ipadd;

            

            }

       }

Total Pageviews