Toast Notification
Following are the steps to implement pushNotification in WindowsPhone
- You need to create a push channel
- Use this PushChannel to get the Channel URI, and also set some handlers like:
- ChannelUriUpdated
- ErrorOccurred
- ShellToastNotificationReceived
- Use this channel URI to get the MPNSTokenID.
- This MPNSTokenID will be used to send the push notification.
- Receive this notification and use the data accordingly.public void SetupPushChannel(){HttpNotificationChannel pushChannel;string channelName = "ToastSampleChannel"; // any NamepushChannel = 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 DeviceSetting.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 DeviceSetting.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 CodingThanks
nice very help full
ReplyDelete