Just a second...

Example: Send a request message to the Push Notification Bridge

The following examples use the Unified API to send a request message on a topic path to communicate with the Push Notification Bridge. The request message is in JSON and can be used to subscribe or unsubscribe from receiving push notifications when specific topics are updated.

Apple
/**
 * Compose a URI understood by the Push Notification Bridge from an APNs device token.
 * @param deviceID APNS device token.
 * @return string in format expected by the push notification bridge.
 */
-(NSString*)formatAsURI:(NSData*)deviceID {
    return [NSString stringWithFormat:@"apns://%@", [deviceID base64EncodedStringWithOptions:0]];
}

/**
 * Compose and send a subscription request to the Push Notification bridge
 * @param paths topic paths within the subscription request
 */
- (void)doPnSubscribe:(NSArray<NSString*> *)paths deviceToken:(NSData*)deviceToken {
    // Compose the JSON request from Obj-C literals
    NSString *const correlation = [[NSUUID UUID] UUIDString];
    PTDiffusionTopicSelector *const selector = [PTDiffusionTopicSelector topicSelectorWithAnyExpression:paths];
    NSDictionary *const request = 
        @{@"request": @{
           @"correlation": correlation,
           @"content": @{
              @"pnsub": @{
                @"destination": [self formatAsURI:deviceToken],
                @"topic": selector.description}
              }
           }};
    NSData *const requestData = [NSJSONSerialization dataWithJSONObject:request options:0 error:nil];

    // Send a message to `SERVICE_TOPIC`
    [_session.messaging sendWithTopicPath:SERVICE_TOPIC
                                    value:[[PTDiffusionContent alloc] initWithData:requestData]
                        completionHandler:^(NSError * _Nullable error)
                        {
                                if(error != nil) {
                                    NSLog(@"Send to topic %@ failed: %@", SERVICE_TOPIC, error);
                                }
                        }];
}
                    
Android
import static java.util.UUID.randomUUID;

import java.io.PrintStream;

import org.json.JSONObject;
import org.json.JSONTokener;

import com.pushtechnology.diffusion.client.Diffusion;
import com.pushtechnology.diffusion.client.content.Content;
import com.pushtechnology.diffusion.client.features.Messaging;
import com.pushtechnology.diffusion.client.features.Messaging.MessageStream;
import com.pushtechnology.diffusion.client.features.Messaging.SendCallback;
import com.pushtechnology.diffusion.client.session.Session;
import com.pushtechnology.diffusion.client.types.ReceiveContext;

/**
 * An example of a client using the 'Messaging' feature to request the Push Notification Bridge
 * subscribe to a topic and relay updates to a GCM registration ID.
 *
 * @author Push Technology Limited
 * @since 5.9
 */
public class ClientSendingPushNotificationSubscription {

    private static final PrintStream OUT = System.out;

    private final String pushServiceTopicPath;
    private final Session session;
    private final Messaging messaging;
    private final MessageStream messageStream = new MessageStream.Default() {
        @Override
        public void onMessageReceived(String topicPath, Content content, ReceiveContext context) {
            final JSONObject response = (JSONObject) new JSONTokener(content.asString()).nextValue();
            final String correlation = response.getJSONObject("response").getString("correlation");

            OUT.printf("Received response with correlation '%s': %s", correlation, response);
        } };

    /**
     * Constructs message sending application.
     * @param pushServiceTopicPath topic path on which the Push Notification Bridgre is taking requests.
     */
    public ClientSendingPushNotificationSubscription(String pushServiceTopicPath) {
        this.pushServiceTopicPath = pushServiceTopicPath;
        this.session =
            Diffusion.sessions().principal("client").password("password")
                .open("ws://diffusion.example.com:80");
        this.messaging = session.feature(Messaging.class);
         messaging.addMessageStream(pushServiceTopicPath, messageStream);
    }

    /**
     * Close the session.
     */
    public void close() {
        messaging.removeMessageStream(messageStream);
        session.close();
    }

    /**
     * Compose & send a subscription request to the Push Notification Bridge.
     *
     * @param subscribedTopic topic to which the bridge subscribes.
     * @param gcmRegistrationID GCM registration ID to which the bridge relays updates.
     */
    public void requestPNSubscription(String gcmRegistrationID, String subscribedTopic) {
        // Compose the request
        final String gcmDestination = "gcm://" + gcmRegistrationID;
        final String correlation = randomUUID().toString();
        final JSONObject request = buildSubscriptionRequest(gcmDestination, subscribedTopic, correlation);

        // Send the request
        messaging.send(pushServiceTopicPath, request.toString(), new SendCallback.Default());
    }

    /**
     * Compose a subscription request.
     * <P>
     * @param destination The {@code gcm://} or {@code apns://} destination for any push notifications.
     * @param topic Diffusion topic subscribed-to by the Push Notification Bridge.
     * @param correlation value embedded in the response by the bridge relating it back to the request.
     * @return a complete request
     */
    private static JSONObject buildSubscriptionRequest(String destination, String topic, String correlation) {
        final JSONObject subObject = new JSONObject();
        subObject.put("destination", destination);
        subObject.put("topic", topic);

        final JSONObject contentObj = new JSONObject();
        contentObj.put("pnsub", subObject);

        final JSONObject requestObj = new JSONObject();
        requestObj.put("correlation", correlation);
        requestObj.put("content", contentObj);

        final JSONObject rootObject = new JSONObject();
        rootObject.put("request", requestObj);
        return rootObject;
    }
}