Follow

Android Native Push Notification Code Example

This section describes the steps involved in writing a Native Android Application that uses GCM. If you are using Unity please follow this guide. Unity Android Push Notification Code Example.

You will have needed to complete the steps in the article Creating your Android GCM Credentials in order to use the code in this example.

 

Updating your AndroidManifest.xml:

Setting Your Application Name:

<application
android:name="com.apmetrix.testapp.TestAppApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="Apmetrix Test App" *THIS MUST BE SET*
android:theme="@style/AppTheme" >

*Note: You will need to make sure that the android:label is set to your App's Name. As the SDK references this label for the Push Alert Activity.

Permissions:

You will need to make sure you add the following PERMISSIONS to your AndroidManifest.xml

 <!-- Apmetrix Required GCM Code: Gives app permission to access messages -->
<uses-permission android:name="com.apmetrix.testapp.permission.C2D_MESSAGE" />
<!-- Apmetrix Required GCM Code: Allows App to receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Apmetrix Required GCM Code: permission as GCM requires a Google account (necessary only if if the device is running a version lower than Android 4.0.4) -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Apmetrix Required GCM Code: Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Apmetrix Required GCM Code Permission necessary to check if application running in foreground -->
<uses-permission android:name="android.permission.GET_TASKS" />
<permission android:name="YOUR.PACKAGE.NAME.permission.C2D_MESSAGE" android:protectionLevel="signature" />

Activities:

You will need to make sure you add the following ACTIVITIES to your AndroidManifest.xml

<activity android:exported="true" android:name="com.apmetrix.sdk.ApmetrixNotificationReceiver"> </activity>

<activity android:name="com.apmetrix.sdk.ApmetrixPushAlertActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" > </activity>

Meta-Data:

You will need to make sure you add the following META-DATA to your AndroidManifest.xml

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

Receiver:

You will need to make sure you add the following RECEIVER to your AndroidManifest.xml

<receiver
android:name="com.apmetrix.sdk.ApmetrixGCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>

<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<category android:name="YOUR.PACKAGE.NAME" />
</intent-filter>
</receiver>

Service:

You will need to make sure you add the following SERVICE to your AndroidManifest.xml

<service android:name="com.apmetrix.sdk.ApmetrixGCMIntentService" />

 

Editing the Status Icons:

There are two GCM Status Icons that you can edit. The Status Bar Icon & the Push Alert Dialog Icon. Both of these Icons rely on the images located in the res/drawable-xxxx (where xxxx represents the dpi of the Android Screen). You will need to add the icon to each drawable folder.

Status Bar Icon: The SDK expects this icon to be named “ic_stat_gcm.png”.

Push Alert Dialog Icon: The SDK expects this icon to be named ““ic_dialog_gcm.png”.

*Note: The final artwork should be exported as a transparent PNG file without background color. You can see a sample icon file below for each Icon Type.

Status Bar Icon:

 

Push Alert Dialog Icon:

 

 

Enabling Push Notifications in the SDK:

To enable the SDK to use push notifications you will need to change the Apmetrix.init() call to take in the GCM Sender ID.

Example:

Apmetrix.init(appContext, gameDataset, gameVersion, gameUnifier, Apmetrix.permissions, gcmSenderId);

 

You can also customize the buttons for the Push Alert Dialog by calling the function:

Apmetrix.pushNotificationAlertConfig(String launchName, String closeName);

Example:

Apmetrix.pushNotificationAlertConfig("Access", "Deny");

 

Accessing the Push Notification Data:

To access the data that is contained within the Push Notification you will need to use Apmetrix.notificationHandler(). This function returns a Map<String, String>.

The Map contains at minimum the message accessible via the key "message".

You can expect this Map to contain additional optional Key/Value Pairs that are added via the Send Message UI in the Apmetrix Dashboard. 

 

Sending A Push Notification:

Next, log into our dashboard (www.apmetrix.com/login)  and click on your Profile Image->Messaging and Send Push Notification and send yourself some tests to verify everything is working properly.

If you are still having issues please contact Apmetrix Support at support@apmetrix.com

Have more questions? Submit a request