Skip to content

Latest commit

 

History

History
119 lines (82 loc) · 4.55 KB

File metadata and controls

119 lines (82 loc) · 4.55 KB

#Notifications Module

This module allows your app to receive notifications sent from Malcom via GCM (Google Cloud Messaging).

First, we will explain how to configure the module. Once it is configured, you can send notifications to the users of your app from the Malcom web.

##How to configure the module

###Create an application in the Google apis console

Go to of the Google apis console and create an application. In the "Services" section, activate the "Google Cloud Messaging for Android". Then, in the "API Access" section, create a new server key (leave the server IPs box empty).

Now you should have a key for server apps. Check that it says "IPs: Any IP allowed". Copy the API key because you will need to configure it in Malcom web.

Login to Malcom web, go to the "Administration" section of your app and then go to "Push config". There you have to copy the API key you generated before. Copy it in both text inputs (production and sandbox).

###Add the required permisions in the manifest

In AndroidManifest.xml, add the following (replace <PACKAGE> with 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" />

###Set the project number

In onCreate of your Application class, specify the project number that GCM has assigned to your app. You can find it in the "Overview" section of the Google apis console.

MalcomLib.setSenderId(<PROJECT_NUMBER>);

###Register for notifications

Also in onCreate of your application class, call this method:

MalcomLib.notificationsRegister(this, <Title>, <TargetActivity>.class);

That call will register the device in Malcom so it can receive notifications.

  • <Title> is the title that will appear in all the notifications. You can set the name of your app or a generic message like "New message".
  • <TargetActivity> is the activity that is going to be opened when the user clicks a notification. This will usually be the same main activity.

###Check notification

In onResume of your "TargetActivity" class, add this call to check for new notifications:

MalcomLib.checkNotification(this);

That method will check whether the activity was opened because the user clicked on a notification. If so, the message of the notification will be displayed in an alert dialog.

If you want to handle the notification message in a custom way, use the following method:

MalcomLib.checkNotification(this, handler);

###Other necessary calls

Override the onNewIntent method. This method is called when a notification is clicked and the user was already on the "TargetActivity". We set the intent so we don't lose the notification message that comes with it.

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

Override the onDestroy method to free resources:

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

###Unregistering the device (optional)

In case you want to unregister the device so it doesn't receive notifications you can use the following method:

MCMCoreAdapter.getInstance().moduleNotificationsUnregister(getApplicationContext());

##How to send notifications

Login to Malcom web, click on "Marketing" tab and then go to Campaigns - Push. There you can send notifications to your apps. Either to all users or to some of them using filters or segments.

You can also send notifications via API. Refer to the API documentation: https://github.com/MyMalcom/MalcomAPI