Follow

Unity Android Push Notification Code Example

This section describes the steps involved in writing a Native Android Application that uses GCM. If you are using Native Android please follow this guide. 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:

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:

*Note: Inside the folder ApmetrixRes contains a SAMPLE res folder. You can start here as your reference point for 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:

In the ApmetrixStatic.cs file you will need to set the static variable GCMSenderID to your id that is obtained via the article Creating Your Android GCM Credentials 

The ApmetrixStatic.cs File

// ************* APMETRIX VARIABLES ****************** \\
public static int appDataset = 123456;
public static string appVersion = "";
public static string appUnifier = "123456789ABCDEFG";
public static int appPermissions = CHILD_SAFE;

//Used for Android Push Set this if using Android GCM
public static string GCMSenderID = "123456789058246";
// ************* END APMETRIX VARIABLES ****************** \\

 

The Apmetrix.cs File

*Note: The Apmetrix.cs file is designed to wok as an Instance Object. You can attach this script to any of your gameObjects. HOWEVER this object must not be destroyed. Destroying this object will reset the SDK when running in Non-Mobile mode. 

*Note: You may make changes to this file as you see fit but it is your responsibility to make sure the SDK still functions properly. Please inform your Apmetrix Account Manager if you are making changes to this file, include your reasoning and we will discuss the changes with the Apmetrix Engineering Team.

These are used inside the Apmetrix.cs file. More specifically inside the void Awake(). This function inits the Apmetrix SDK using the Apmetrix Variables and initializes Push Notifications. 

You can customize the Apmetrix Push Alert Dialog by changing the inputs to the following function which resides in the Apmetrix.cs file inside the void Awake().

This function MUST be called in order to correctly initialize Push Notifications. Failure to call this function will cause your Push Notifications to not work as intended.

ApmetrixInitMobilePushNotifications("Activate", "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 Dictionary<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