ProximityDevice.PublishMessage(String, String, MessageTransmittedHandler) | publishMessage(String, String, MessageTransmittedHandler) method
Publishes a message to subscribers of the specified message type. The specified handler is called when the message has been transmitted.
Syntax
public long PublishMessage( string messageType, string message, MessageTransmittedHandler messageTransmittedHandler )
Parameters
- messageType
-
Type: String [JavaScript] | System.String [.NET] | Platform::String [C++]
The type of message to deliver to subscribers.
- message
-
Type: String [JavaScript] | System.String [.NET] | Platform::String [C++]
The message to deliver to subscribers.
- messageTransmittedHandler
-
Type: MessageTransmittedHandler
The handler to call when the message has been transmitted.
Return value
Type: Number [JavaScript] | System.Int64 [.NET] | int64 [C++]
A unique publication ID for the published message.
Remarks
You can subscribe to published messages by using the SubscribeForMessage method.
Messages continue to be published until the StopPublishingMessage method is called or the ProximityDevice object is released.
Messages are delivered to all applications that have subscribed to the message type (as indicated by the messageType parameter). Message type values are case-sensitive strings that consist of two parts: the protocol and the subtype. The protocol is first, followed by a dot (.) and then the subtype. For the PublishMessage method, the protocol must always be "Windows.". The subtype is a string of alphanumeric characters and the following additional characters: . ( ) + , - : = @ ; $ _ ! * ’. The subtype cannot exceed a length of 250 characters.
To publish messages by using another message type, like “WindowsMime.” or “NDEF:WriteTag”, you must use the PublishBinaryMessage method.
You can use the PublishUriMessage method to publish a Uniform Resource Identifier (URI) to a proximate computer.
Messages are converted to UTF-8 characters before they're transmitted.
For Windows Store apps, publications and subscriptions are active only if the calling app is in the foreground.
Examples
Windows.Networking.Proximity.ProximityDevice proximityDevice; long publishedMessageId = -1; long subscribedMessageId = -1; private void initializeProximityDevice() { proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault(); if (proximityDevice != null) { PublishMessageButton.Click += PublishMessage; SubscribeForMessageButton.Click += SubscribeForMessage; StopSubscribingForMessageButton.Click += StopSubscribingForMessage; StopPublishingMessageButton.Click += StopPublishingMessage; } else { WriteMessageText("Failed to initialized proximity device.\n"); } } private void PublishMessage(object sender, RoutedEventArgs e) { // Stop publishing the current message. if (publishedMessageId != -1) { proximityDevice.StopPublishingMessage(publishedMessageId); } publishedMessageId = proximityDevice.PublishMessage("Windows.SampleMessage", MessageTextBlock.Text, MessagePublished); } private void MessagePublished( Windows.Networking.Proximity.ProximityDevice sender, long messageId) { // The message has been successfully published. } private void SubscribeForMessage(object sender, RoutedEventArgs e) { // Only subscribe for the message one time. if (subscribedMessageId == -1) { subscribedMessageId = proximityDevice.SubscribeForMessage("Windows.SampleMessage", messageReceived); } } private void messageReceived( Windows.Networking.Proximity.ProximityDevice device, Windows.Networking.Proximity.ProximityMessage message) { MessageBlock.Text += "Message received: " + message.DataAsString + "\n"; } private void StopSubscribingForMessage(object sender, RoutedEventArgs e) { proximityDevice.StopSubscribingForMessage(subscribedMessageId); subscribedMessageId = -1; } private void StopPublishingMessage(object sender, RoutedEventArgs e) { proximityDevice.StopPublishingMessage(publishedMessageId); publishedMessageId = -1; } // Write a message to MessageBlock on the UI thread. private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher; async private void WriteMessageText(string message, bool overwrite = false) { await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { if (overwrite) MessageBlock.Text = message; else MessageBlock.Text += message; }); }
Requirements
|
Minimum supported client | Windows 8 |
|---|---|
|
Minimum supported server | Windows Server 2012 |
|
Minimum supported phone | Windows Phone 8 |
|
Namespace |
|
|
Metadata |
|
|
Capabilities |
|
See also
- ProximityDevice
- PublishMessage(String, String)
- PublishBinaryMessage
- PublishUriMessage
- StopPublishingMessage
- Proximity and Tapping (JavaScript)
- Proximity and Tapping (C#/VB/C++)
- Samples
- Proximity sample
Build date: 2/25/2013