Push Messaging

Account Setup

In order to start using push notifications for android applications. A trivial amount of setup is required. On Left Navigation, click on Account > Android Push, you will see the following screen.

Android Setup

Select either GCM or FCM as your provider depending on what service you’re currently using. Enter the GCM/FCM Server key and save

Client Setup

LivePinch push messaging can be integrated with either Google Cloud Messaging (GCM) or Firebase Cloud Messaging (FCM).

As of April 10, 2018, Google has deprecated Google Cloud Messaging. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019.

We strongly suggest that you use Firebase Cloud Messaging for push messaging in your application.

Firebase Cloud Messaging (FCM) integration

  1. Add Firebase to your project

Follow the instructions for Adding Firebase to Your Android Project . If you follow the manual integration process from these instructions, make sure that you follow both steps for Adding Firebase to your app and Adding the SDK.

  1. Add Firebase Cloud Messaging dependency

Add the dependency for Firebase Cloud Messaging in your module�s build.gradle.

dependencies {
  implementation 'com.google.firebase:firebase-messaging:17.3.2'
}
  1. Pass Firebase token & Push Message Data to LivePinch

Firebase tokens/data can be passed to LivePinch using a FirebaseMessagingService. Create a class inside your src folder (eg: app/src/main/java/com/livepinch/sampleapp/MyFirebaseMessagingService.java)that extends FirebaseMessagingService and pass the token to LivePinch.

Complete Example:

package com.livepinch.sampleapp;

import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.livepinch.sdk.LivePinch;

import java.util.Map;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]



@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
  Log.d(TAG, "Message data payload: " + remoteMessage.getData());
  Map<String, String> data = remoteMessage.getData();

  if(data.containsKey("source") && "livepinch".equals(data.get("source"))) {
      LivePinch.getInstance(getApplicationContext()).receive(data);
  }

}


}


/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);

LivePinch.getInstance(getApplicationContext()).setRegistrationId(token);
}

}

It is also recommended that you pass Firebase token to our sdk inside application class

LivePinch.getInstance(getApplicationContext()).setRegistrationId(FirebaseInstanceId.getInstance().getToken());
  1. Next, register the service to the application element of your AndroidManifest.xml as follows.
<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

GCM - Manual Integration

It is same like fcm, Integrate gcm from the console and follow the steps below

  • Paste Server key and package name to the dashboard.
  • Paste following code in manifest file
<meta-data
 android:name="com.livepinch.sdk.gcm_registration"
 android:value="false" />

<service
 android:name="com.livepinch.sdk.EventLoggerService"
 android:exported="false" />

Pass Token to Server

Pass Registration id to the server: Paste following code while getting registration token from the gcm server.

LivePinch.getInstance(getApplicationContext()).setRegistrationId(FirebaseInstanceId.getInstance().getToken());

Where device_token is obtained from InstanceId Listener service class.

Pass PushData to Server:

(e.g)

@Override
public void onMessageReceived(String from, Bundle data) {
  String source = data.getString("push_source");
  if(source!=null && source.equals("LivePinch")) {
  LivePinch.get().receive(data);
  }
 }
}

GCM - Auto Integration

If integrating this , our sdk will take care of registering device to the gcm server and receiving message from the GCM.

Steps to integrate Automatic GCM Integration:

  1. Add GCM dependency to your app/build.gradle file
dependencies {
      compile 'com.google.android.gms:play-services-gcm:10.0.1'
   }
  1. Paste following code in manifest file
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  <permission
android:name="YOURPACKAGENAME.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

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

 //Optional permission if location needs to track

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


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

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

<meta-data
android:name="com.livepinch.sdk.gcm_registration"
android:value="true" />
 //Replace $SENDER_ID with your SENDER_ID
<meta-data
android:name="com.livepinch.sdk.SenderID"
android:value="$SENDER_ID" />

<service
android:name="com.livepinch.sdk.gcm.PushInstanceIDListenerService"
android:exported="false">
<intent-filter>
    <action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.livepinch.sdk.gcm.PushMessageListenerService"
android:exported="false">
<intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.livepinch.sdk.gcm.RegistrationIntentService"
android:exported="false" />
<service
android:name="com.livepinch.sdk.EventLoggerService"
android:exported="false" />