ProximityDevice.PublishMessage(String, String) | publishMessage(String, String) method
Publishes a message to subscribers of the specified message type.
Syntax
Public Function PublishMessage( messageType As String, message As String ) As Long
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.
Return value
Type: Number [JavaScript] | System.Int64 [.NET] | int64 [C++]
A unique publication ID for the published message. Pass this value to the StopPublishingMessage method to stop publishing the 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
Private proximityDevice As Windows.Networking.Proximity.ProximityDevice Private publishedMessageId As Long = -1 Private subscribedMessageId As Long = -1 Private Sub initializeProximityDevice() proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault() If proximityDevice IsNot Nothing Then AddHandler PublishMessageButton.Click, AddressOf PublishMessage AddHandler SubscribeForMessageButton.Click, AddressOf SubscribeForMessage AddHandler StopSubscribingForMessageButton.Click, AddressOf StopSubscribingForMessage AddHandler StopPublishingMessageButton.Click, AddressOf StopPublishingMessage Else WriteMessageText("Failed to initialized proximity device." & vbTab) End If End Sub Private Sub PublishMessage(sender As Object, e As RoutedEventArgs) ' Stop publishing the current message. If publishedMessageId <> -1 Then proximityDevice.StopPublishingMessage(publishedMessageId) End If publishedMessageId = proximityDevice.PublishMessage("Windows.SampleMessage", MessageTextBlock.Text) End Sub Private Sub SubscribeForMessage(sender As Object, e As RoutedEventArgs) ' Only subscribe for the message one time. If subscribedMessageId = -1 Then subscribedMessageId = proximityDevice.SubscribeForMessage("Windows.SampleMessage", AddressOf messageReceived) End If End Sub Private Sub messageReceived( device As Windows.Networking.Proximity.ProximityDevice, message As Windows.Networking.Proximity.ProximityMessage) MessageBlock.Text &= "Message received: " & message.DataAsString & vbTab End Sub Private Sub StopSubscribingForMessage(sender As Object, e As RoutedEventArgs) proximityDevice.StopSubscribingForMessage(subscribedMessageId) subscribedMessageId = -1 End Sub Private Sub StopPublishingMessage(sender As Object, e As RoutedEventArgs) proximityDevice.StopPublishingMessage(publishedMessageId) publishedMessageId = -1 End Sub ' Write a message to MessageBlock on the UI thread. Private Async Sub WriteMessageText(message As String, Optional overwrite As Boolean = False) Await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, Sub() If overwrite Then MessageBlock.Text = message Else MessageBlock.Text &= message End If End Sub) End Sub
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, MessageTransmittedHandler)
- PublishBinaryMessage
- PublishUriMessage
- StopPublishingMessage
- Proximity and Tapping (JavaScript)
- Proximity and Tapping (C#/VB/C++)
- Samples
- Proximity sample
Build date: 2/25/2013
