Push Notifications For Android

If you wish to use any of the Push Campaign or Push Point features of User Hook, you will need to follow a few additional steps while setting up your app.

First, you will need to create a Google Firebase project and add the google-services.json file to your project.

Once you have created your Firebase project, we need to find your Cloud Messaging Server key. In the Firebase Console, click on the “gear” settings icon and then click on the “Cloud Messaging” tab.

You will need to copy this survey key and add it to your User Hook Push Settings through the User Hook admin website.

Initialize User Hook for Push Notifications

If you installed User Hook through the gradle dependency, you do not need to make any further changes. If you installed the User Hook source manually, you will need to make a few changes inside your AndroidManifest.xml file.

<manifest>

    ... 

        //add the following lines inside your application entry


        <!-- User Hook Push Setup -->
        <service
            android:name="com.userhook.push.UHFirebaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

        <service
            android:name="com.userhook.push.UHFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

    </application>

</manifest>

Handling Push Notification Payloads

When your android app receives a push notification, it will have access to the Payload data that was defined in the User Hook admin site. If your app will be using this payload data, you will need to setup a payload handler.

Tracking Opens

Opens are automatically tracked by User Hook, but you will need to make a minor code change. To ensure that push notification opens are tracked properly, please make the following change to the Activity class that the push notification will open.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

Custom Notification Icon

By default, User Hook will use your application’s icon as the icon shown in push notifications. You may set a custom notification icon while initializing User Hook in your application class:

UserHook.initialize(this, "YOUR_USERHOOK_APP_ID", "YOUR_USERHOOK_APP_KEY", true);
UserHook.setPushNotificationIcon(R.drawable.notification_icon);

Handling Multiple Push Providers

There may be scenarios where your app needs to use another push message provider at the same time as using User Hook. That is not a problem. Inside of your UHFirebaseMessagingService you can inspect the push message to determine if it was sent from User Hook or from another push provider.

 public class UHFirebaseMessagingService extends FirebaseMessagingService {

     @Override
     public void onMessageReceived(RemoteMessage remoteMessage) {


         // check if this message originated from User Hook or another push provider
         if (UserHook.isPushFromUserHook(remoteMessage.getData())) {

             Notification notification = UserHook.handlePushMessage(remoteMessage.getData());

             if (notification != null) {
                 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                 notificationManager.notify(0, notification);
             }
         }
         else {
             // push is from a different push provider
             // use the appropriate logic from that push provider
             // to handle push message
         }

     }

 }