Push notifications for Android are made possible by Google’s Firebase platform. Through the use of push notifications, an app user can receive a notification about any event like a chat message, promotion, or reminder. Push notifications can also be used to signal a user about an incoming call, even if their app is turned off.
We have created a series of tutorials to explain these different aspects of setting up and using push notifications on an Android App. In our first blog we outlined how to get your Android App ready for push notifications by configuring Firebase. In the second blog, we explained the next stage of how to implement push notifications once the configuration was complete. Now with this third blog in the series we explain how to use push notifications to notify your user of an incoming call when an application is closed or in the background mode.
So that we can explain this set-up in some detail, we won’t have space to discuss call related code and details on application state management below. But don’t worry, if you want to learn more about these topics check out our Android SDK documentation.
Read on to find out more about managing in-coming calls.
To understand how push notifications work with call functionality we can use a simple schema. Let’s imagine that we have two logged Android users in our application. Of course we have used the Quickblox Android SDK to build our video and audio calling app and it works fine when the app is in foreground mode. But when one of the users goes to background mode he can no longer receive a call. Let’s take a closer look of this in action.
For a video and audio call to work, we need a REST connection to a server so that we can log-in/log-out users, get chat messages, get users, etc., and we need an XMPP connection for sending/receiving chat messages and for making calls. We need both of these primary connections (REST and XMPP), as outlined in the scheme below, so that our audio and video calls work.
Before being able to receive a call, the user needs to be connected to both the Rest Server and XMPP server. The following code snippet shows how to establish this connection.
Once the user receives a push notification, their application will automatically establish a connection to both servers.
Let’s look at this in a little more detail. When User A wishes to call User B , User A needs to send a push notification and initiate the call. On the other side, when User B receives the notification, their app will establish a connection with the REST server, which will enable them the receive an incoming call, as detailed in the diagram below.
When we need to call somebody we should send a push notification and make a call. On another side, we will receive a call, establish connections and receive an incoming call like in a picture below.
As we can see we have 7 steps. Let’s describe them in detail.
1. UserA sends a push notification to an opponent (REST)
2. Rest server sends push notification to opponent (specific device)
3. UserA makes a call (XMPP)
4. UserB receives a push notification
5. UserB log-in (REST)
6. UserB log-in (XMPP)
7. UserB receives a call event
Let’s see how it works and how we can implement specific steps in a code.
When we send a push notification, we can include additional information to help identify whether it’s a notification for a new message, advertisement, new chat, or incoming call. In the code snippet above the information is included in the JSONObject.
On the other side, when the push notification is received, the JSONObject is parsed so that the information about the type of push notification is identified.
In this case, the server sends a message to the Google server (Firebase or another, it depends on what server we choose). This is covered by QuickBlox and no action from the developer is required.
Here is a normal flow for making calls without any additional information. To know how to make calls, check out our documentation.
How you receive and handle a push notification depends on how your push notification has been implemented. The code snippet below shows the default implementation for Firebase in QuickBlox.
onMessageReceived we can parse the message and identify the message type (e.g. new message, new chat, or incoming call).
Here we should login-in our user to REST and XMPP, as shown in the code below:
The QuickBlox server will send a message via XMPP about an incoming call to UserB and it will trigger a
onReceiveNewSession callback that will initiate a standard call flow. If you want to learn more about video call flow check out our documentation.
As you can see from the description above, setting up push notifications so that they can inform app users of incoming calls is a pretty straightforward process that shouldn’t require too many modification to your code. In short, you need to send push notifications from the call’s side, receive notification on the recipients side, log into QuickBlox’s servers and subscribe to incoming calls.
Once you have these steps in place, you will have fully-functional push notifications enabled on your Android app!
Got more questions? Please add them to the comments below, or contact customer support.