Skip to content

Push

Introduction

StreetHawk's Push component lets you send push messages to your application. Depending on your application's current state, if your application in foreground, the push notification overlays on the current screen as a notification dialog with user to take action and for application in background, the SDK generates a notification in notification bar. Push modules also support custom dialog.

Prerequisite

  • Push modules requires Google API project registered for your application. Configure push messaging(Android) describes steps for creating Google API project for your application to obtain SenderId (Project Number) and register API key with StreetHawk.

  • StreetHawkPush component depends on StreetHawk's Analytics components. This document assumes that you have already integrated StreetHawkAnalytics component. if not then click here for detailed documentation on StreetHawkAnalytics components which describes steps to integrate Analytics component.

Integration Steps


  • Include StreetHawk Push Component

1.In Xamarin Studio, double click on Components folder
2.Click on "Get More Components", and search for StreetHawk Push. Click Add to App
3.Once downloaded, click on Add to Project

Similarly add Google Play Services - Cloud Messaging (GCM) Component as well

  • Modify AndroidManifest.xml

Add the following permissions

<permission
        android:name="<PACKAGE_NAME>.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

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

<!-- Permissions for displaying badges -->

    <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />
    <uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT" />
    <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />
    <uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
    <uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

Now add the following code under application tag of AndroidManifest.xml

<manifest>

<application>
...
...
...
<service
            android:name="com.streethawk.library.push.SHGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

        <receiver android:name="com.streethawk.library.push.PushNotificationBroadcastReceiver">
            <intent-filter>
                <action android:name="com.streethawk.intent.action.pushnotification" />
                <action android:name="com.streethawk.intent.action.gcm.STREETHAWK_DECLINED" />
                <action android:name="com.streethawk.intent.action.gcm.STREETHAWK_POSTPONED" />
                <action android:name="com.streethawk.intent.action.gcm.STREETHAWK_ACCEPTED" />
                <action android:name="com.streethawk.intent.action.APP_STATUS_NOTIFICATION" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="<PACKAGE_NAME>" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.streethawk.library.push.SHInstanceIDListenerService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
</application>
<manifest>

Note: In the above code snippet, replace <PACKAGE_NAME> with package name of your application.

  • Modify AssemblyInfo.cs

Add the below mentioned code in AssemblyInfo.cs. Omitting the code below will crash your application whenever your application will receive push notification while in background.

[assembly: Application(Icon = "@mipmap/icon", Label = "<APP LABEL>")]
  • Include Statements

Add the following include statements

using Com.Streethawk.Library.Core;
using Com.Streethawk.Library.Push;
  • Add Push Module

Add the following code in your application's launcher class

namespace XamHawkPush
{
[Activity (Label = "XamHawkPush", MainLauncher = true, Icon = "@mipmap/icon")]

// Implement ISHOBSERVER
public class MainActivity : Activity,ISHEventObserver
{
int count = 1;

protected override void OnCreate (Bundle savedInstanceState)
{
Xamarin.Insights.Initialize (XamarinInsights.ApiKey, this);
base.OnCreate (savedInstanceState);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
// Pass App's project number as provided by GCM
Push.GetInstance (this).RegisterForPushMessaging ("<PROJECT NUMBER>");
StreetHawk.Instance.SetAppKey ("<APP_KEY>");
StreetHawk.Instance.Init (Application);
}

public void OnInstallRegistered(string installId) {
// Add push module 
Push.GetInstance (this).AddPushModule ();
}

}
} 

Send Your First Push Message


After the install is successfully registered with StreetHawk, you can send a test push message by clicking User search under Growth campaigns, click on your test install and select Send Notification Tab. Enter any title and message and press send button. You should see a test message (Simple push message) arriving on your device.

RAW JSON PUSH Example

StreetHawk’s raw JSON features lets your application receive and handle Raw JSON sent from streetHawk’s web console. After you have configured your Push notification and confirmed it is working you can try sending JSON notifications using Campaigns.

To get started, make sure you implement the ISHObserver interface by adding ",ISHObserver" see below.

This will implement the methods automatically:

-OnReceiveNonSHPushPayload()

-OnReceivePushData()

-OnReceiveResult()

-ShNotifyAppPage()

-ShReceivedRawJSON()

*IMPORTANT: Make you you have imported the StreetHawk Push 1.8.7 library to generate the methods. Or follow the tutorial above to learn more.

So far we have:

using Android.App;
using Android.Widget;
using Android.OS;
using Com.Streethawk.Library.Push;
using System;

namespace finals
{
    [Activity(Label = "finals", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity , ISHObserver
    {
        public void OnReceiveNonSHPushPayload(Bundle p0)
        {
        }

        public void OnReceivePushData(PushDataForApplication p0)
        {

        }

        public void OnReceiveResult(PushDataForApplication p0, int p1)
        {


        }

        public void ShNotifyAppPage(string p0)
        {

        }

        public void ShReceivedRawJSON(string p0, string p1, string p2)
        {
           String getTitle = p0;
           String getDescrip = p1;
           String getJsonRaw = p2;

        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
             SetContentView (Resource.Layout.Main);

            Push.GetInstance(this).RegisterSHObserver(this);

        }
    }
}

The important bit is this:

 Push.GetInstance(this).RegisterSHObserver(this);

Include this to registered the SHObserver class.

Our next step is to get the App Key and Project key, these keys you will find in your StreetHawk Dashboard. Please follow this tutorial if you haven't yet.

Your APP KEY should be located here:

(Don't get it confused with API key)

This should be something of a name e.g. for this example our APP Key is Terminator.

While your Project Key is here: This should be a number, but If it's blank, again follow this tutorial.

Now back to our code, we need to import the StreetHawk's Analytics Component version 1.8.7 and the Google Play Service Cloud Messaging. To learn more, follow this tutorial here.

After importing it to the project, add the lines:

 protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
             SetContentView (Resource.Layout.Main);


            StreetHawk.Instance.SetAppKey("Our App Key");
            Push.GetInstance(this).RegisterForPushMessaging("Our Project Key");
            StreetHawk.Instance.Init(Application);


            Push.GetInstance(this).RegisterSHObserver(this);

        }

Add the App key and Project key you obtained earlier.

Next up your AndroidManifest.XML permission for the services:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="finals.finals" android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:minSdkVersion="16" />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.GET_TASKS" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="com.htc.launcher.permission.READ_SETTINGS" />
  <uses-permission android:name="com.htc.launcher.permission.UPDATE_SHORTCUT" />
  <uses-permission android:name="com.sonyericsson.home.permission.BROADCAST_BADGE" />
  <uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
  <uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  <uses-permission android:name="android.permission.CALL_PHONE" />
  <!-- Start copying from below -->
  <permission android:name="<package name>.permission.C2D_MESSAGE" 
              android:protectionLevel="signature" />

  <uses-permission android:name="<package name>.permission.C2D_MESSAGE" />




    <application android:label="finals">
      <!-- Start copying from below -->
      <meta-data
          android:name="app_key"
          android:value="<APP KEY>"/>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <receiver
            android:name="com.streethawk.library.push.GCMReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
          <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!--Add  package_name here-->
            <category android:name="<package name>/>       
          </intent-filter>

        </receiver>
        <!-- End copying -->
      </application>



  </manifest>

Then:

                              //(your drawable icon location) 
[assembly: Application(Icon = "@drawable/icon", Label = "<project name>")]

In the Assemblyinfo.cs

Finish !

-Try testing a simple push notification to your app to make sure "OnReceivePushData" or "OnReceiveResult" will trigger a response to test if you set everything correctly.

-If you get any out of memory errors when building, then make sure to set the Java Max Heap size to a bigger size.

-Additionally, make sure to delete any old Streethawk Library versions below 1.7.0.