Destroying a push channel

The sample app 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 app, the server-side push initiator requires a username and password that it authenticates before unsubscribing. The sample app provides an Unregister screen where the user can type any username and password. For your app, check with your push content provider to get a valid username and password.

You can open the Unregister screen 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 app tries to destroy the channel.

If you don't subscribe with the Push Service SDK as the server-side push initiator, you don't need to provide a username and password. You can tap Unregister and Submit, and the sample app tries to destroy the channel.

Call destroyChannel()

The sample app 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);

If the destroy channel operation fails, the destroyChannelCallback callback function shows the error handling. In your app, 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 app calls unsubscribeFromPushInitiator() to unsubscribe from the server-side push initiator. The unsubscribe function passes in the username and password that you typed on the Unregister screen. If you don't subscribe with the Push Service SDK, the sample app 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: 2015-06-11



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

comments powered by Disqus