Skip to content

Push

Introduction

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

Pre-requisite

  • The documents assume that you have downloaded and added the boiler-plate code described in the introduction document. You will require IStreetHawkPush.cs, StreetHawkPush.cs (iOS) and StreetHawkPush.cs (Android) in particular for StreetHawkPush component.
  • StreetHawk Push component depends on StreetHawk Analytics components. This document assumes that you have already integrated StreetHawk Analytics. If not then click here for detailed documentation which describes steps to integrate Analytics component.
  • For Android applications, Push modules depends on 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.
  • For iOS application, click here to configure push notifications.

Integration Steps

  • Add StreetHawk Push Component

  1. In iOS project, right click on component folder
  2. Click on "Get More Components" and search for StreetHawk Push. Click Add to App.
  3. Once downloaded, click on Add to Project.
  4. Repeat the above steps for adding the component in .Droid project of your application

  • Add Platform Specific code

    • Android
      Copy and paste the below code in AndroidManifest.xml located under properties folder of your application's .Droid project
      <manifest>
      ...
      <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
          <uses-permission android:name="android.permission.WAKE_LOCK" />
          <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" />
      
       <permission
              android:name="<REPLACE_WITH_PACKAGE_NAME>.permission.C2D_MESSAGE"
              android:protectionLevel="signature" />
          <uses-permission android:name="<REPLACE_WITH_PACKAGE_NAME>.permission.C2D_MESSAGE" />
      <application>
      ...
      <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="<REPLACE_WITH_PACKAGE_NAME>" />
                  </intent-filter>
              </receiver>
      
       <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" />
                      <action android:name="com.streethawk.intent.action.MSG_FROM_CORE" />
                  </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>
      

      Add the below-mentioned code in OnCreate function.
      public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
      {
       protected override void OnCreate(Bundle bundle)
      {
      ...
      new StreetHawkAnalytics(Application);
      new StreetHawkPush(Application);
      ...
      
  • Register for push messaging

Open App.Xaml.cs in your Xamarin.forms project and the following code in OnStart function as shown below (before StreetHawk..

protected override void OnStart()
{
...
DependencyService.Get<IStreetHawkPush>().RegisterForPushMessaging("<PROJECT_NUMBER>");

// Make sure you have installed and Followed the Analytics Tutorial ! 
DependencyService.Get<IStreetHawkAnalytics>().SetAppKey("<APP KEY>");

DependencyService.Get<IStreetHawkAnalytics>().Init();

}

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.

Supporting Custom Dialogs Themes

StreetHawk is capable of customizing the confirm dialog of push notification, and response to the decision result

  • Handle raw JSON push
using StreetHawkCrossplatform;

protected override void OnStart()
{
......
DependencyService.Get<IStreetHawkPush>().RegisterForRawJSON( delegate (string title, string message, string JSON)
{
Device.BeginInvokeOnMainThread(() =>
{
string msg = string.Format("title: {0}\r\nmessage: {1}\r\njson: {2}", title, message, JSON);
MainPage.DisplayAlert("Receive json push:", msg, "OK");
});
});
  • Handle push data arrives
using StreetHawkCrossplatform;

protected override void OnStart()
{
......
DependencyService.Get<IStreetHawkPush>().OnReceivePushData(delegate (PushDataForApplication pushData)
   {
   Device.BeginInvokeOnMainThread(() =>
   {
   string message = string.Format("msgid: {0}; code: {1}; action: {2};\r\ntitle: {3}\r\nmessage: {4}\r\ndata: {5}", pushData.msgID, pushData.code, pushData.action, pushData.title, pushData.message, pushData.data);
   MainPage.DisplayAlert("Show custom dialog:", message, "Continue");
//Mandatory: Send push result.
DependencyService.Get<IStreetHawkPush>().SendPushResult(pushData.msgID, SHPushResult.SHPushResult_Accept);
   });
   });
  • Handle push result
using StreetHawkCrossplatform;

protected override void OnStart()
{
......
DependencyService.Get<IStreetHawkPush>().OnReceiveResult(delegate (PushDataForApplication pushData, SHPushResult result)
 {
   Device.BeginInvokeOnMainThread(() =>
   {
   string title = string.Format("Push result: {0}", result);
   string message = string.Format("msgid: {0}; code: {1}; action: {2};\r\ntitle: {3}\r\nmessage: {4}\r\ndata: {5}", pushData.msgID, pushData.code, pushData.action, pushData.title, pushData.message, pushData.data);
   MainPage.DisplayAlert(title, message, "OK");
   });
   });
  • Handle none StreetHawk payload
using StreetHawkCrossplatform;

protected override void OnStart()
{
......
DependencyService.Get<IStreetHawkPush>().OnReceiveNonSHPushPayload( delegate (string payload)
{
Device.BeginInvokeOnMainThread(() =>
{
MainPage.DisplayAlert("Receive none StreetHawk push:", payload, "OK");
});
});

Delay asking for Permission

By default, StreetHawk SDK registers push notification when “Init” is called in the “OnStart”. In case you may wish to delay asking the user for push notification permission, you can add the code be as shown below.

using StreetHawkCrossplatform;

//optional to delay ask for permission
DependencyService.Get<IStreetHawkPush>().SetIsDefaultNotificationServiceEnabled(false);  //make streethawkinit not register for user notification, delay for later time.

//do DependencyService.Get<IStreetHawkAnalytics>().Init(); and other things.

DependencyService.Get<IStreetHawkPush>().SetIsNotificationServiceEnabled(true);  //later when ready to register for user notification, call this.

Add Interactive Pair Buttons

To add interactive pair buttons to App, use code sample:

using StreetHawkCrossplatform;

List<InteractivePush> pairs = new List<InteractivePush>();
InteractivePush pair = new InteractivePush("FacebookTwitter", "Facebook", "icon1", "Twitter", "icon2");
pairs.Add(pair);
InteractivePush pair = new InteractivePush("EmailPhone", "Email", "icon3", "Phone", "icon4");
pairs.Add(pair);
DependencyService.Get<IStreetHawkPush>().SetInteractivePushBtnPairs(pairs);