ProximityDevice.PublishMessage ProximityDevice.PublishMessage ProximityDevice.PublishMessage ProximityDevice.PublishMessage ProximityDevice.PublishMessage Method

Definition

Overloads

PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String)

Publishes a message to subscribers of the specified message type.

PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler)

Publishes a message to subscribers of the specified message type. The specified handler is called when the message has been transmitted.

PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String) PublishMessage(String, String)

Publishes a message to subscribers of the specified message type.

public : long PublishMessage(Platform::String messageType, Platform::String message)
long PublishMessage(winrt::hstring messageType, winrt::hstring message) const;
public long PublishMessage(String messageType, String message)
Public Function PublishMessage(messageType As String, message As String) As long
var long = proximityDevice.publishMessage(messageType, message);

Parameters

messageType
String String String

The type of message to deliver to subscribers.

message
String String String

The message to deliver to subscribers.

Returns

long long long

A unique publication ID for the published message. Pass this value to the StopPublishingMessage method to stop publishing the message.

Additional features and requirements

Capabilities
ID_CAP_PROXIMITY [Windows Phone] proximity ID_CAP_PROXIMITY [Windows Phone]

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.
"); } } 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); } 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 + "
"; } 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; }); }
    function id(elementId) {
        return document.getElementById(elementId);
    }

    var proximityDevice;
    var publishedMessageId = -1;
    var subscribedMessageId = -1;

    function initializeProximityDevice() {
        proximityDevice = Windows.Networking.Proximity.ProximityDevice.getDefault();

        if (proximityDevice) {
            id("publishMessageButton").addEventListener("click", publishMessage);
            id("subscribeForMessageButton").addEventListener("click", subscribeForMessage);
    id("stopSubscribingForMessageButton").addEventListener("click", 
        stopSubScribingForMessage);
    id("stopPublishingMessageButton").addEventListener("click", 
        stopPublishingMessage);
        }
        else {
            id("messageDiv").innerHTML += "Failed to initialized proximity device.<br />";
        }
    }

    function publishMessage() {
        // Stop publishing the current message.
        if (publishedMessageId != -1) {
            proximityDevice.stopPublishingMessage(publishedMessageId);
        }

        publishedMessageId = 
    proximityDevice.publishMessage("Windows.SampleMessage", id("messageText").value);
    }

    function subscribeForMessage() {
        // Only subscribe for the message one time.
        if (subscribedMessageId === -1) {
            subscribedMessageId = 
        proximityDevice.subscribeForMessage("Windows.SampleMessage", messageReceived);
        }
    }

    function messageReceived(device, message) {
        id("messageDiv").innerHTML += "Message received: " + message.dataAsString + "<br />";
    }

function stopSubscribingForMessage() {
    proximityDevice.stopSubscribingForMessage(subscribedMessageId);
        subscribedMessageId = -1;
}

function stopPublishingMessage() {
    proximityDevice.stopPublishingMessage(publishedMessageId);
        publishedMessageId = -1;
}
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
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.
"); } } 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); } 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 + "
"; } 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; }); }
    function id(elementId) {
        return document.getElementById(elementId);
    }

    var proximityDevice;
    var publishedMessageId = -1;
    var subscribedMessageId = -1;

    function initializeProximityDevice() {
        proximityDevice = Windows.Networking.Proximity.ProximityDevice.getDefault();

        if (proximityDevice) {
            id("publishMessageButton").addEventListener("click", publishMessage);
            id("subscribeForMessageButton").addEventListener("click", subscribeForMessage);
    id("stopSubscribingForMessageButton").addEventListener("click", 
        stopSubScribingForMessage);
    id("stopPublishingMessageButton").addEventListener("click", 
        stopPublishingMessage);
        }
        else {
            id("messageDiv").innerHTML += "Failed to initialized proximity device.<br />";
        }
    }

    function publishMessage() {
        // Stop publishing the current message.
        if (publishedMessageId != -1) {
            proximityDevice.stopPublishingMessage(publishedMessageId);
        }

        publishedMessageId = 
    proximityDevice.publishMessage("Windows.SampleMessage", id("messageText").value);
    }

    function subscribeForMessage() {
        // Only subscribe for the message one time.
        if (subscribedMessageId === -1) {
            subscribedMessageId = 
        proximityDevice.subscribeForMessage("Windows.SampleMessage", messageReceived);
        }
    }

    function messageReceived(device, message) {
        id("messageDiv").innerHTML += "Message received: " + message.dataAsString + "<br />";
    }

function stopSubscribingForMessage() {
    proximityDevice.stopSubscribingForMessage(subscribedMessageId);
        subscribedMessageId = -1;
}

function stopPublishingMessage() {
    proximityDevice.stopPublishingMessage(publishedMessageId);
        publishedMessageId = -1;
}
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

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 UWP apps, publications and subscriptions are active only if the calling app is in the foreground.

Important

The proximity APIs do not provide authentication, encryption, or message integrity. Do not use proximity to exchange user sensitive information such as passwords, financial data, text messages, email, photographs, or government id numbers.

See also

PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler) PublishMessage(String, String, MessageTransmittedHandler)

Publishes a message to subscribers of the specified message type. The specified handler is called when the message has been transmitted.

public : long PublishMessage(Platform::String messageType, Platform::String message, MessageTransmittedHandler messageTransmittedHandler)
long PublishMessage(winrt::hstring messageType, winrt::hstring message, MessageTransmittedHandler messageTransmittedHandler) const;
public long PublishMessage(String messageType, String message, MessageTransmittedHandler messageTransmittedHandler)
Public Function PublishMessage(messageType As String, message As String, messageTransmittedHandler As MessageTransmittedHandler) As long
var long = proximityDevice.publishMessage(messageType, message, messageTransmittedHandler);

Parameters

messageType
String String String

The type of message to deliver to subscribers.

message
String String String

The message to deliver to subscribers.

messageTransmittedHandler
MessageTransmittedHandler MessageTransmittedHandler MessageTransmittedHandler

The handler to call when the message has been transmitted.

Returns

long long long

A unique publication ID for the published message.

Additional features and requirements

Capabilities
ID_CAP_PROXIMITY [Windows Phone] proximity ID_CAP_PROXIMITY [Windows Phone]

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.
"); } } 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 + "
"; } 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; }); }
    function id(elementId) {
        return document.getElementById(elementId);
    }

    var proximityDevice;
    var publishedMessageId = -1;
    var subscribedMessageId = -1;

    function initializeProximityDevice() {
        proximityDevice = Windows.Networking.Proximity.ProximityDevice.getDefault();

        if (proximityDevice) {
            id("publishMessageButton").addEventListener("click", publishMessage);
            id("subscribeForMessageButton").addEventListener("click", subscribeForMessage);
    id("stopSubscribingForMessageButton").addEventListener("click", 
        stopSubScribingForMessage);
    id("stopPublishingMessageButton").addEventListener("click", 
        stopPublishingMessage);
        }
        else {
            id("messageDiv").innerHTML += "Failed to initialized proximity device.<br />";
        }
    }

    function publishMessage() {
        // Stop publishing the current message.
        if (publishedMessageId != -1) {
            proximityDevice.stopPublishingMessage(publishedMessageId);
        }

        publishedMessageId = 
    proximityDevice.publishMessage("Windows.SampleMessage", id("messageText").value,
                                           messagePublished);
    }

function messagePublished(sender, messageId) {
    // The message has been successfully published.
    }

    function subscribeForMessage() {
        // Only subscribe for the message one time.
        if (subscribedMessageId === -1) {
            subscribedMessageId = 
        proximityDevice.subscribeForMessage("Windows.SampleMessage", messageReceived);
        }
    }

    function messageReceived(device, message) {
        id("messageDiv").innerHTML += "Message received: " + message.dataAsString + "<br />";
    }

function stopSubscribingForMessage() {
    proximityDevice.stopSubscribingForMessage(subscribedMessageId);
        subscribedMessageId = -1;
}

function stopPublishingMessage() {
    proximityDevice.stopPublishingMessage(publishedMessageId);
        publishedMessageId = -1;
}
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,
                                       AddressOf MessagePublished)
End Sub

Private Sub MessagePublished(
sender As Windows.Networking.Proximity.ProximityDevice,
    messageId As Long)

' The message has been successfully published.
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

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 UWP apps, publications and subscriptions are active only if the calling app is in the foreground.

Important

The proximity APIs do not provide authentication, encryption, or message integrity. Do not use proximity to exchange user sensitive information such as passwords, financial data, text messages, email, photographs, or government id numbers.

See also