Relaying push notifications to your own API

We recently made a campaign for Loka mineral water, where users together reveal the new flavors. This is done by using Snapchat. We send users snaps containing puzzle pieces, and when they make a screen grab of a specific piece – we put that piece in the puzzle on the website.

Because of legal reasons we aren’t allowed to use the Snapchat API, but we still need to have some automation. For example we wanted to know immediately when a user made a screen grab of our puzzle piece snap, so we got around the problem by building an Android application.

In the latest Android SDK 4.4 (API 19) the “NotificationListenerService” was released. Using it, we can listen for incoming push notifications on our Nexus tablet. In the app, we’re just checking if the package name from the StatusBarNotification equals “com.snapchat.android” and if the notification ID matches the ID you get when someone is screenshoting your snap.

Then we post the username to our API, and cancel the notification from the NotificationListenerService.

code example:

@Override
   public void onNotificationPosted(StatusBarNotification statusBarNotif) {

       Notification notif = statusBarNotif.getNotification();

       if (notif != null){

           Bundle extras = notif.extras;
           Intent intent = new Intent(MainActivity.INTENT_NOTIFICATION);
           intent.putExtras(notif.extras);
           sendBroadcast(intent);

           Bundle intentExtras = intent.getExtras();
           String notifTitle = intentExtras.getString(Notification.EXTRA_TITLE);
           CharSequence notifText = intentExtras.getCharSequence(Notification.EXTRA_TEXT);

           int notificationId = statusBarNotif.getId();

           //Check if the notification is from snapchat, and the id matches the printscreen id from notification.
           if(statusBarNotif.getPackageName().equals(snapchatPackageName)){
               if(notificationId == printScreenId){
                  
                   //Split the String to get the username. 
                   String arr[] = notifText.toString().split(" ", 2);
                   String userName = arr[0];
                   
                   //Send to api.
                   runHttpPost(userName);
                  
                   //Cancel all notifications.
                   NLService.this.cancelAllNotifications();
               }
           }
           else{
               Log.i("Other", "Another type of notification.");
           }

       }
   }