Skip to content

Push

Introduction

Push modules let's you send push messages to your application. Depending on your application's current state, if your application is in Foreground, the push notification overlays on the current screen as a notification dialog with the user to take action, and for application in Background, the SDK generates a notification in the notification bar. Push modules also support application specific themes and custom dialog.

Prerequisite

  • Push modules depends on Google API project registered for your application. Click here to configure push messaging on Google Developer's console. Alternatively, you can follow the steps here.
  • Or Click here for application registered on Firebase.
  • Download google-service.json file generated in the above step and place it in the root directory (app) of your application

Integration Steps

Integration of StreetHawk's Analytics module is a prerequisite for this module. Click here for the documentation on StreetHawk Analytics module.

  • Add Push module dependencies

Add the following code in application's build.gradle under dependencies.

dependencies {
...
...
    compile 'com.streethawk:core:latest.release'
    compile 'com.streethawk:push:latest.release'
}
- Modify AndroidManifest.xml

Copy the following code snippet inside the manifest tag of your application’s manifest file. In the snippet below, replace <PACKAGE_NAME> by the package name of your application.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ......
    ......

 <!-- Start copying from below -->
<permission
        android:name="<PACKAGE_NAME>.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

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

<!-- End copying -->

    <application
        ......
        ......
    </application>
</manifest>

Copy following code snippets inside the application tag of your application’s manifest file. Replace <YOUR_APPLICATIONS_APP_KEY> with application’s API key registered with StreetHawk server and <PACKAGE_NAME> with the package name of your application.

<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="<PACKAGE_NAME>" />
        </intent-filter>
    </receiver>
    <!-- End copying -->
        .....
        .....

    <activity
        ......
    </activity>
  </application>
</manifest>
- Manifest Merge permissions If you have manifest merge set on in the app level from the Gradle Build tools then you can skip this step. If you turned off the manifest merger from the Gradle build tools then please add these additional permissions.
<application
    ......
    .......
    <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.SHGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <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>
         <service
            android:name="com.streethawk.library.push.SHJobScheduler"
            android:exported="false"
            android:permission="android.permission.BIND_JOB_SERVICE"/>
    <!-- End copying -->
        .....
        .....

    <activity
        ......
    </activity>
  </application>
</manifest>

  • Register Push module inside your application

Add the following code in onCreate function of your application's Launcher Activity.

import com.streethawk.library.core.StreetHawk;
import com.streethawk.library.push.Push;

...
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Application app = getApplication();

        StreetHawk.INSTANCE.setAppKey("<APP_KEY>");
        StreetHawk.INSTANCE.init(app);
        // Double check your project number, entering wrong project number will not allow push to work.
        Push.getInstance(this).registerForPushMessaging(<PROJECT_NUMBER>"); 

    }

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.

Setting Statusbar icons

Customize the big and small status bar icons (background notification icons) as shown below

...

  Push.getInstance(this).setLargeIconResID("ic_launcher");   // Large icon file name
  Push.getInstance(this).setSmallIconResID("ic_stat_notification"); // Small icon file name

   StreetHawk.INSTANCE.init(getApplication());

...

Supporting Custom Dialogs Themes

For a push message, by default SDK generates Android's default notification dialog. However, you can override this behavior by following the steps as shown below.

  • Implements ISHObserver

Implement ISHObserver in your code as shown below

public class MySHObserver implements ISHObserver {
    @Override
    public void shReceivedRawJSON(String title, String message, String json) {

    }

    @Override
    public void shNotifyAppPage(String pageName) {

    }

    @Override
    public void onReceivePushData(PushDataForApplication pushData) {

    }

    @Override
    public void onReceiveResult(PushDataForApplication resultData, int result) {

    }
}

Where

shReceiveRawJSON Function to receive RAW JSON received from StreetHawk server
shNotifyAppPage Function to receive app page received from StreetHawk server (Cross platforms only)
onReceivePushData Function to receive Push Data received from StreetHawk server
onReceivePushResult Function to receive Push result (Action taken by user on push notification)
  • Register ISHObserver implemented class with SDK

Register implementation ISHObserver interface with StreetHawk SDK using registerSHObserver API as shown in code snippet below. Call registerSHObserver before init function.

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Application app = getApplication();
...
...
Push.getInstance(this).setUseCustomDialog(true);
        Push.getInstance(this).registerSHObserver(new MySHObserver());  
...
...       
 StreetHawk.INSTANCE.setAppKey("<REPLACE_YOUR_APP_KEY>");
        StreetHawk.INSTANCE.init(app);
    }

Note that omitting setUseCustomDialog API or setting it with false will generate StreetHawk's default dialog. This technique can be used for tracking Push Data and Push result (Actions by the user on the notification dialog) inside your application.

Knowledge Base Articles