Handling a lost connection with the PNS agent

There might be rare occasions when our sample application loses its connection to the PNS agent running on the device. The PNS agent maintains a connection between the PPG and the application, and forwards push notifications from the PPG to the appropriate application instance.

When the application loses the connection, it can't perform these operations: create a session, create or destroy a channel, register to start in the background when a push message arrives, or unregister from starting in the background. When these operations fail because of the lost connection, the Push Service issues a ConnectionClosed error code.

To handle this error, our sample application connects a slot to the connectionClosed signal.

checkConnectResult(QObject::connect(m_pushService, 
   SIGNAL(connectionClosed()), this, SLOT(onConnectionClosed())));

When the connection is lost, our application notifies the user, and then tries to re-establish the connection to the PNS agent by using the m_timer backoff timer to call reconnect(). The timer calls reconnect() periodically, and the interval of time between each call to reconnect() gets longer with each call.

void PushNotificationService::onConnectionClosed()
{
	if (!m_timer->isActive()){
		emit noPushServiceConnection();
		m_timer->start(m_retryTime);
		return;
	}

	// Try to reconnect to the Push Agent
	if (!m_pushService->reconnect()){

		// Reconnect was unsuccessful.
		// Check to see if the max wait time has been reached.
		// If it has, then just stop and notify the user that  
        // reconnecting to the PNS Agent was unsuccessful. Otherwise,  
        // double the amount of time to wait before retrying, and then 
        // try again.
		if (m_retryTime >= MAX_WAIT_TIME){
			qWarning() << "Max number of retry attempts reached. Could 
              not re-establish connection to Push Agent.";
			emit pushAgentConnectionStatusChanged(false);
			m_timer->stop();
		} else {
			qDebug() << "No connection to Push Agent";
			m_retryTime = m_retryTime * 2;
			m_timer->setInterval(m_retryTime);
		}
	} else {
		// Reconnect was successful.
		// Notify the user and call createSession.
		qDebug() << "Connection to Push Agent re-established!";
		m_timer->stop();
		m_retryTime = INITIAL_WAIT_TIME_MILLISECONDS;
		emit pushAgentConnectionStatusChanged(true);
		createSession();
	}

}

Our sample application handles the lost connection by explicitly telling the user that the application is trying to recover from the error. In your application, we recommend that you try to recover from the error silently, and only notify the user if the connection can't be re-established.

Last modified: 2013-12-21

comments powered by Disqus