×

One APK with Nokia Notifications and Google Cloud Messaging

Since C2DM/GCM and Nokia Notifications Intent APIs and helper libraries have different namespaces for all relevant intents, permissions, and classes, they can coexist in one application. This makes it possible to build applications that can run unmodified on all devices that provide either of the notification services.

If you have an application that uses C2DM/GCM and wish to add support for Nokia Notifications, it is recommended to proceed as follows:

  1. Port your application to use Nokia Notifications exclusively and test it.
  2. Merge the resulting two applications and add the logic required to select one of the two notification services (see subchapter Service selection later in this topic for more details).

Handling service callbacks

An application using the helper libraries needs to implement dedicated intent services for both notification providers. By default these should be:

    public class MyGCMIntentService extends com.google.android.gcm.GCMBaseIntentService

and

    public class MyPushIntentService extends com.nokia.push.PushBaseIntentService

The application logic for handling callbacks in both of these classes will be quite similar, it is often a good idea to implement the above two classes as thin redirectors to service-agnostic handlers (see figure 1). Note that the base classes for the intent services (PushBaseIntentService and GCMBaseIntentService) are provided by the notification frameworks.

Figure 1. Common implementation for both intent service classes.

Figure 1. Common implementation for both intent service classes.

Similarly, an application that does not use the helper libraries will need to handle broadcast intents from both notification services.

Typically all applications will store received registration ID locally and send it to application's own network service. When using multiple notification services, the registration ID should be paired with metadata identifying the notification service.

Application manifest (AndroidManifest.xml)

The Manifest should contain separate service-specific permission, broadcast receiver, and service definitions for both C2DM/GCM and Nokia Notifications. The only manifest elements that are shared by both services are the following application-specific permission elements:

    <permission
        android:name="(your_application_package).permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    
    <uses-permission android:name="(your_application_package).permission.C2D_MESSAGE" />

An example of a multi-service AndroidManifest.xml (using default helper library classes):

    <permission
        android:name="(your_application_package).permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="(your_application_package).permission.C2D_MESSAGE" />
    <uses-permission android:name="com.nokia.pushnotifications.permission.RECEIVE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <application ... >
        ...
        <receiver android:name="com.nokia.push.PushBroadcastReceiver"
            android:permission="com.nokia.pushnotifications.permission.SEND">
            <intent-filter>
                <action android:name="com.nokia.pushnotifications.intent.RECEIVE" />
                <action android:name="com.nokia.pushnotifications.intent.REGISTRATION" />
                <category android:name="(your_application_package)" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="(your_application_package)" />
            </intent-filter>
        </receiver>
        <service android:name=".MyPushIntentService" />
        <service android:name=".MyGCMIntentService" />
    </application>

Service selection

When application registers to a notification service, it needs to decide which service to use. Recommended approach is to first check if Nokia Notifications is supported and then fall back to C2DM/GCM. The following code snippet gives an example of how application could select the notification service API to use:

    try {
        PushRegistrar.checkDevice(this);
        // (continue with Nokia Notifications logic)
    }
    catch (UnsupportedOperationException e1) {
        try {
            GCMRegistrar.checkDevice(this);
            // (continue with GCM logic)
        }
        catch (UnsupportedOperationException e2) {
            // neither notification service is available
        }
    }

When application unregisters, the service should be selected based on the currently registered service (for example, by storing notification service type along with the registration ID).

Sample application

The Nokia Notifications API co-development sample demonstrates how to implement a dynamic application which adapts to a device and selects the supported notifications framework (GCM or Nokia Notifications). The sample is provided with the SDK, located in <Android SDK path>/extras/nokia/nokia_x_services/samples/nna/NokiaNotificationsSingleApkSample, and it is also available in GitHub.

Video: One APK solution with both Nokia Notifications and GCM

The media player is loading...


Last updated 25 June 2014

Back to top

×