Creating a PushService object

Our sample application creates a PushService object in order to create a push session and perform push-related operations. In your application, you should create one global instance of the PushService object, and associate it with one providerApplicationID and one invokeTargetID.

providerApplicationID and invokeTargetID are parameters of the PushService() constructor. The providerApplicationID parameter corresponds to the application ID that you specified when you configured our sample application. For details about configuring our sample application, see Configure our sample application.

The invokeTargetID parameter corresponds to the value that's specified in the bar-descriptor.xml file for our sample application. The invokeTargetID identifies the push-enabled application that's the target of a push message, and that's invoked when the push message arrives. For more information about updating your bar-descriptor.xml file, see Update your bar-descriptor.xml file.

Our sample application must create a push session to establish communication with the PNS agent running on the device, and before it can use any of the other Push Service APIs. Our application must create a session every time it starts. Since the call to create a session is asynchronous, our sample application connects a slot to the createSessionCompleted signal before the call to create a session.

Call initializePushService()

The initializePushService() function initializes one instance of bb::network::PushService. The function also connects the signals for the following events: create session, create channel, destroy channel, register to launch, unregister from launch, SIM card change, push transport ready, and a lost connection with the PNS agent.

The initializePushService() function is defined in the PushNotificationService object. This object is in the service layer of our application. To pass the PushService signals up to the main object of our headless application, we chain the PushService signals together by connecting them to the PushNotificationService signals.

void PushNotificationService::initializePushService()
{
  const Configuration config = m_configurationService.configuration();

  // ...
     m_previousApplicationId = config.providerApplicationId();

     m_pushService = new PushService(config.providerApplicationId(), 
        INVOKE_TARGET_KEY_PUSH, this);

     //Connect the signals
     checkConnectResult(QObject::connect(m_pushService, 
        SIGNAL(createSessionCompleted(const 
        bb::network::PushStatus&)), this, 
        SIGNAL(createSessionCompleted(const 
        bb::network::PushStatus&))));
     checkConnectResult(QObject::connect(m_pushService, 
        SIGNAL(createChannelCompleted(const bb::network::PushStatus&, 
        const QString)), this, SIGNAL(createChannelCompleted(const 
        bb::network::PushStatus&, const QString))));
     checkConnectResult(QObject::connect(m_pushService, 
        SIGNAL(destroyChannelCompleted(const 
        bb::network::PushStatus&)), this, 
        SIGNAL(destroyChannelCompleted(const 
        bb::network::PushStatus&))));
     checkConnectResult(QObject::connect(m_pushService, 
        SIGNAL(registerToLaunchCompleted(const 
        bb::network::PushStatus&)), this, 
        SIGNAL(registerToLaunchCompleted(const 
        bb::network::PushStatus&))));
     checkConnectResult(QObject::connect(m_pushService, 
        SIGNAL(unregisterFromLaunchCompleted(const 
        bb::network::PushStatus&)), this, 
        SIGNAL(unregisterFromLaunchCompleted(const 
        bb::network::PushStatus&))));
     checkConnectResult(QObject::connect(m_pushService, 
        SIGNAL(simChanged()), this, SIGNAL(simChanged())));
     checkConnectResult(QObject::connect(m_pushService, 
        SIGNAL(pushTransportReady(bb::network::PushCommand::Type)),
        this, 
        SIGNAL(pushTransportReady(bb::network::PushCommand::Type))));
     checkConnectResult(QObject::connect(m_pushService, 
        SIGNAL(connectionClosed()), this, 
        SLOT(onConnectionClosed())));
  }
}

Check the connect signal result

The initializePushService() function uses the checkConnectResult() function to check for signal and slot connection failures. Our sample application checks all signal and slot connections using the checkConnectResult() function.

void checkConnectResult(bool connectResult){
    // If any Q_ASSERT statement(s) indicate that the slot failed to 
    // connect to the signal, make sure you know exactly why this has 
    // happened. This is not normal, and will cause your app to stop 
    // working!!

    // This is only available in Debug builds.
    Q_ASSERT(connectResult);
}

Connect a slot to the createSessionCompleted() signal

Our sample application connects a slot to the createSessionCompleted() signal.

AppHeadless::AppHeadless()
{
// ...

// Connect the push notification service signals and slots.
checkConnectResult(QObject::connect(m_pushNotificationService, 
   SIGNAL(createSessionCompleted(const bb::network::PushStatus&)),
   this, 
   SLOT(onCreateSessionCompleted(const bb::network::PushStatus&))));
// ...
}

Call createSession()

Our sample application calls createSession() from the PushNotificationService object to create a push session.

void PushNotificationService::createSession()
{
  // Initialize the PushService if it has not been already.
  initializePushService();
 
  m_pushService->createSession();
}

Last modified: 2013-12-21

comments powered by Disqus