Skip to content
pedroduran edited this page May 21, 2013 · 5 revisions

Notifications Module

This module allows the Malcom device registering in order to receive Notifications from the Google GCM (Google Cloud Messaging).

###Configure Notifications

Follow the steps:

1- Specify the senderId obtained from Google:

    MCMCoreAdapter.getInstance().setSenderId(SENDER_ID);

2 - In AndroidManifest.xml, configure the following things where is your application package:

    <permission android:name="<PACKAGE>.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="<PACKAGE>.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />


    <receiver android:name="com.malcom.library.android.module.notifications.gcm.MalcomGCMBroadcastReceiver"
           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="<PACKAGE>" />
           </intent-filter>
         </receiver>


    <service android:name="com.malcom.library.android.module.notifications.gcm.GCMIntentService" />
    <service android:name="com.malcom.library.android.module.notifications.services.PendingAcksDeliveryService" />

###Using this module

Once your application is configured you'll need to use the module like this:

1- Include in OnResume() method the following:

    MCMCoreAdapter.getInstance().moduleNotificationsRegister(this, EnvironmentType.SANDBOX,"<Title>",<MainActivity>.class);

This line is in charge of registering the device in Malcom and the module activation.

  • The <Title> tag will be the title text that is going to appear in the Android tabbar once the notification is received.
  • is the Main activity of your application, which is going to be launched once the notification is opened.
  • The EnvironmentType is the environment where we want to register the device. You can choose SANDBOX or PRODUCTION.

Also, you can execute the registering without indicate the environment:

    MCMCoreAdapter.getInstance().moduleNotificationsRegister(getApplicationContext(),"<Title>",<MainActivity>.class);

The environment is fetched from the "debuggable" flag value in the AndroidManifest.xml file where if it is activated it will point to SANDBOX and if not it will point to PRODUCTION.

You'll need to activate the declarations:

    MalcomNotificationReceiver notReceiver = null;
    private Intent intent = null;

and the following line:

    MCMCoreAdapter.getInstance().moduleNotificationsCheckForNewNotifications(getApplicationContext(),intent);

2- Include in onCreate() method the following:

    notReceiver = new MalcomNotificationReceiver();
           registerReceiver(notReceiver, new IntentFilter(MCMNotificationModule.SHOW_NOTIFICATION_ACTION));
           intent = getIntent();

3- Override the onNewIntent() method:

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        this.intent = intent;
    }

4- Override the onDestroy() method:

    @Override
    protected void onDestroy() {
         super.onDestroy();
         unregisterReceiver(notReceiver);
         GCMRegistrar.onDestroy(getApplicationContext());
    }

When a notification is opened, Malcom will send the ACK to proccess it. In case it cannot send it (because of no internet connection) it will send it when device recovers the connection.

###Unregistering the device

In case you want to unregister the device you should use the following method:

    MCMCoreAdapter.getInstance().moduleNotificationsUnregister(getApplicationContext());
Clone this wiki locally