Destroying a push channel

The sample application destroys the push channel to the PPG to stop receiving push messages.

If you chose to subscribe with the Push Service SDK when you configured the sample application, the Push Initiator requires a username and password that it authenticates before unsubscribing. The sample application provides an Unregister screen where the user can type any username and password. For your application, check with your push content provider to get a valid username and password.

You can open the Unregister screen box by tapping Unregister at the bottom of the device screen. The Unregister screen appears with the currently registered user's name and password.

Unregister dialog box where you type a username and password.

After you tap Submit, the sample application tries to destroy the channel.

If you're not subscribing with the Push Service SDK as the Push Initiator, you don't need to provide a username and password. You can tap Unregister and Submit, and the sample application tries to destroy the channel.

Call destroyChannel()

The sample application calls destroyChannel() to destroy the channel to the PPG. The destroyChannel() function includes the destroyChannelCallback callback function as an argument.

sample.pushcapture.pushService.destroyChannel(
      sample.pushcapture.destroyChannelCallback);

The destroyChannelCallback callback function shows the error handling if the destroy channel operation fails. In your application, you might want to handle the errors differently. For example, you might want to retry an operation first before displaying an error message. The API Reference lists the possible actions that you can take for each error constant. The API Reference also shows which error codes apply to which function calls.

sample.pushcapture.constructor.prototype.destroyChannelCallback = 
       function(result) {
   if (result == blackberry.push.PushService.SUCCESS || 
       result == blackberry.push.PushService.
                 CHANNEL_ALREADY_DESTROYED || 
       result == blackberry.push.PushService.
                 CHANNEL_ALREADY_DESTROYED_BY_PROVIDER ||
       result == blackberry.push.PushService.
                 CHANNEL_SUSPENDED_BY_PROVIDER ||
       result == blackberry.push.PushService.
                 PPG_SUBSCRIBER_NOT_FOUND ||
       result == blackberry.push.PushService.
                 CREATE_CHANNEL_NOT_DONE) {
   // Success!     
   } else {
       .        
       .
       .
      // Error handling 
   }             
};

Call unsubscribeFromPushInitiator()

If the destroy channel operation is successful, and you're subscribing with the Push Service SDK, the sample application calls unsubscribeFromPushInitiator() to unsubscribe from the Push Initiator. The unsubscribe function passes in the username and password that you typed on the Unregister screen. If you're not subscribing with the Push Service SDK, the sample application doesn't call unsubscribeFromPushInitiator().

sample.pushcapture.constructor.prototype.unsubscribeFromPushInitiator 
                                        = function() {
    document.getElementById("progressinfo").innerHTML = "Unsubscribing 
             from Push Initiator...";

    var username = document.getElementById("unreguserid").value.trim();
    var password = document.getElementById("unregpwd").value.trim();

    var params = "appid=" + encodeURIComponent(sample.pushcapture.appid) 
                  + "&";
    params += "username=" + encodeURIComponent(username) + "&";
    params += "password=" + encodeURIComponent(password);

    var unsubscribeUrl = sample.pushcapture.piurl + "/unsubscribe?" 
                         + params;

    var xmlHttp = new XMLHttpRequest();

    xmlHttp.open("GET", unsubscribeUrl);

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            var status = xmlHttp.status;
            var returnCode = xmlHttp.responseText;

            sample.pushcapture.pushInitiatorUnsubscribeHandler(status, 
                                            returnCode);
        }
    };

    xmlHttp.send();
};

Last modified: 2014-10-09



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus