Creating a push channel

Our sample application creates a push channel to the PPG to receive push messages. Our application only needs to create a channel the first time it starts or if a destroy channel occurred previously and we want to start receiving push messages again.

If you chose to subscribe with the Push Service SDK when you configured our sample application, the Push Initiator requires a username and password that it authenticates before allowing the subscription. Our application provides a Register dialog box 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 Register dialog box by tapping Register at the bottom of the device screen. The Register dialog box appears.

Register dialog box where you type a username and password.

After you type the username and password, and tap Register in the dialog box, our sample application tries to create a channel.

If you're not subscribing with the Push Service SDK, you don't need to provide a username and password. You can simply tap Register at the bottom of the device screen, and our application tries to create a channel.

Connect a slot to the createChannelCompleted() signal

Since the call to create a channel is asynchronous, our sample application connects a slot to the createChannelCompleted() signal.

AppHeadless::AppHeadless()
{
// ...
 checkConnectResult(QObject::connect(m_pushNotificationService, 
   SIGNAL(createChannelCompleted(const bb::network::PushStatus&, 
   const QString)), this, SLOT(onCreateChannelCompleted(const 
   bb::network::PushStatus&, const QString))));
// ...
}

Call createChannel()

Our sample application calls createChannel() from the PushNotificationService object to create a channel, and passes in the PPG URL in the parameter list that you specified when you configured our application. If you're using the BlackBerry Device Service as the PPG, the PPG URL is an empty string. If the channel creation is successful, the createChannelCompleted signal returns a token that represents the address of the device. The Push Initiator uses the token to send push messages to the device. If the channel creation is unsuccessful, our sample application displays an error message.

void PushNotificationService::createChannel()   
{
 m_pushService->createChannel(m_configurationService.configuration().
   ppgUrl());   
}

Call subscribeToPushInitiator()

If the channel creation is successful, our sample application calls subscribeToPushInitiator() from the PushNotificationService object to subscribe with the Push Initiator, and passes off the call to the RegisterService object. The subscribeToPushInitiator() parameter list includes the token to the Push Initiator and the username and password that you typed in the Register dialog box. If you're not subscribing with the Push Service SDK, our sample application doesn't call subscribeToPushInitiator().

void PushNotificationService::subscribeToPushInitiator(
    const User& user, const QString &token)
{
  m_registerService.subscribeToPushInitiator(user,token);
}

Handle the subscription with the Push Initiator

The RegisterService object handles the subscription with the Push Initiator.

void RegisterService::subscribeToPushInitiator(const User& user, const 
   QString& token)
{
   // Keep track of the current user's information so it can be stored 
   // later on a success
   m_currentUser = user;

   const Configuration config = m_configurationService.configuration();

   QUrl url(config.pushInitiatorUrl() + "/subscribe");
   url.addQueryItem("appid",config.providerApplicationId());
   url.addQueryItem("address",token);
   url.addQueryItem("osversion",deviceVersion());
   url.addQueryItem("model",deviceModel());
   url.addQueryItem("username",user.userId());
   url.addQueryItem("password",user.password());
   if (config.usingPublicPushProxyGateway()) {
       url.addQueryItem("type","public");
   } else {
       url.addQueryItem("type","bds");
   }

   m_reply = m_accessManager.get(QNetworkRequest(url));

   // Connect to the reply finished signal.
    
   checkConnectResult(connect(m_reply, SIGNAL(finished()), this, 
     SLOT(httpFinished())));
}

Last modified: 2013-12-21

comments powered by Disqus