These docs are for v1.0. Click to read the latest docs for v2.0.

Push Notification

Push Notifications are messaged which are displayed on users device as pop up to provide user with information. It's a great tool to keep in contact with users.

📘

Note

You can skip these steps if you don't want to send push notifications via Smartech

Integrate Firebase Cloud Messaging in your app.

You can follow Google’s Documentation or the steps mentioned below.

  • Sign in to Firebase using your Google account.

  • Create a Firebase project.

  • Register your app with Firebase.
    Enter your app's package name in the Android package name field. You can copy the package name from Manifest file.

  • Add a Firebase configuration file.

    • Click Download google-services.json to obtain your Firebase Android config file (google-services.json).
    • Move your config file into the module (app-level) directory of your app. (app folder)
  • Go to the settings -> Cloud Messaging -> Copy the server key.
  • Add Firebase SDK
    The Google services plugin for Gradle loads the google-services.json file that you just downloaded. Modify your build.gradle files to use the plugin.
  1. Project-level build.gradle (/build.gradle):
buildscript {
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }
  dependencies {
    ...
    // Add this line
    classpath 'com.google.gms:google-services:4.3.2'
  }
}
allprojects {
  ...
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    ...
  }
}
  1. App-level build.gradle (//build.gradle):
apply plugin: 'com.android.application'
dependencies {
  // add the Firebase SDK for Firebase mesaging.
  implementation 'com.google.firebase:firebase-messaging:20.0.0'
  // add SDKs for any other desired Firebase products
  // https://firebase.google.com/docs/android/setup#available-libraries
}
...
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'
  1. Finally, press 'Sync now' in the bar that appears in the IDE.
  2. Run your application.

Implement push notifications in existing FCM class

Scenario 1: When the user has not implemented push notifications.
If App doesn’t have any push notification implementation from FCM then just define the SMTFbMessagingService in the Manifest file as mentioned below. Then the SDK will take care of the rest.

<service
   android:name="com.netcore.android.notification.SMTFbMessagingService"
   android:exported="false">
   <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>

Scenario 2: App already has push notifications implemented.
In this case, the user needs to extend SMTFirebaseMessagingService instead of FirebaseMessagingService. Internal firebase service class will consume Smartech related notifications, others it will pass it to the App’s Messaging Service class.

class FbMessagingService extends SMTFirebaseMessagingService {
  @Override
    public void onNewToken(Context context, @Nullable String token) {
        //TODO app code here.
    }

    @Override
    public void onMessageReceived(Context context, @Nullable RemoteMessage remoteMessage) {
        //TODO app code here.
    }
}
class FbMessagingService : SMTFirebaseMessagingService() {
   override fun onNewToken(context: Context, token: String?) {
       //TODO app code here

   }

   override fun onMessageReceived(context: Context, remoteMessage: RemoteMessage?) {
       //TODO app code here
   }


   override fun onDeletedMessages(context: Context) {
       super.onDeletedMessages(context)

   }

   override fun onMessageSent(context: Context, value: String?) {
       super.onMessageSent(context, value)
   }

   override fun onSendError(context: Context, var1: String?, var2: Exception?) {
       super.onSendError(context, var1, var2)
   }
}

Step two will be declaring services in the manifest. Declare the SMTFbMessagingService class before your service (a class which has extended SMTFbMessagingService) in the manifest.

<service
   android:name="com.netcore.android.notification.SMTFbMessagingService"
   android:exported="false">
   <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>

<service
   android:name="com.app_name.android.AppServiceClassName"
   android:exported="false">
   <intent-filter>
       <action android:name="com.google.firebase.MESSAGING_EVENT" />
   </intent-filter>
</service>

Scenario 3: Using your own FirebaseMessagingService.
If you don't want to extend your application's FirebaseMessaging class with SDK's FirebaseMessaging class then you need to provide us the token via setDevicePushToken() and notification payload via handlePushNotification() methods to our SDK.

handlePushNotification() method will only display a notification if the Notification payload originated from Netcore and will safely ignore if not.

public class AppsFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(@NonNull String token) {
        super.onNewToken(token);
        Smartech.getInstance(new WeakReference<Context>(this)).setDevicePushToken(token);

    }

    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Smartech.getInstance(new WeakReference<>(getApplicationContext())).handlePushNotification(remoteMessage.getData().toString());
    }
}
class AppsFirebaseMessagingService : FirebaseMessagingService() {

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        Smartech.getInstance(WeakReference(this)).setDevicePushToken(token)
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
        Smartech.getInstance(WeakReference(applicationContext)).handlePushNotification(remoteMessage.data.toString())
    }
}

Get FCM Token

To obtain the FCM token of the user from the SDK, add given snippet as per the requirement.

Smartech.getInstance(new WeakReference<>(context)).getDevicePushToken();
Smartech.getInstance(WeakReference(context)).getDevicePushToken()

Fetching Existing token from FCM

To obtain an already generated token by Application, we can use the below method to get the existing token.

Smartech.getInstance(new WeakReference<>(context)).fetchAlreadyGeneratedTokenFromFCM();
Smartech.getInstance(WeakReference(context)).fetchAlreadyGeneratedTokenFromFCM()

Customizing Notifications Appereance

We have created SMTNotificationOptions to provide you the functionality to change your Notifications icons, sound, and color. You can create an object of this class to set the notifications icon, color, and sound and apply these changes using setNotificationOptions() methods that take SMTNotificationOptions object as a parameter.

Push Notification Icon

You need to use SMTNotificationOptions.setSmallIconTransparentId(resource_id) method in order to change the push notification icon.

SMTNotificationOptions options = new SMTNotificationOptions(this);
options.setSmallIconTransparentId(R.drawable.ic_notif);
Smartech.getInstance(new WeakReference<>(context)).setNotificationOptions(options);
val options = SMTNotificationOptions(this)
options.smallIconTransparentId = R.drawable.ic_notif
Smartech.getInstance(WeakReference(context)).setNotificationOptions(options)

Note:

  1. The notification icon being used should strictly be in .png format as per Google’s UI guidelines​. Preferable size for the push notification icons is mentioned below.
drawable-mdpi 		: 	24 x 24
drawable-hdpi 		: 	36 x 36 
drawable-xhdpi 		: 	48 x 48
drawable-xxhdpi 	: 	72 x 72
drawable-xxxhdpi 	: 	96 x 96

Changing Push Notification Icon Color

To change the color of notification icon, add given snippet inside the onCreate() method of the Application Class​​.

SMTNotificationOptions options = new SMTNotificationOptions(this);
options.setTransparentIconBgColor("#007AFF");
Smartech.getInstance(new WeakReference<>(context)).setNotificationOptions(options);
val options = SMTNotificationOptions(this)
options.transparentIconBgColor = "#007AFF"
Smartech.getInstance(WeakReference(context)).setNotificationOptions(options)

Retrieving Deeplink data

Use Case 1: If the App is in the Background or in the Killed state.

The Deeplink data will be received in the Launcher activity of App.
Write the following code in launcher activity.

Bundle bundleExtra = getIntent().getExtras();
        if (bundleExtra != null) {

            if (bundleExtra.containsKey(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_DEEPLINK)) {
                String deepLinkvalue = bundleExtra.getString(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_DEEPLINK);
            } else {
                Log.v("Activity", "does not have deeplink path.");
            }

            if (bundleExtra.containsKey(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_CUSTOM_PAYLOAD)) {
                String customPayloadvalue = bundleExtra.getString(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_CUSTOM_PAYLOAD);
            } else {
                Log.v("Activity", "does not have custom payload.");
            }

        }
val bundleExtra = intent.extras
        if (bundleExtra != null) {
            if (bundleExtra.containsKey(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_DEEPLINK)) {
                val deepLinkvalue = bundleExtra.getString(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_DEEPLINK)
            } else {
                Log.v("Activity", "Does not have deeplink path.")
            }
            if (bundleExtra.containsKey(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_CUSTOM_PAYLOAD)) {
                val customPayloadvalue = bundleExtra.getString(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_CUSTOM_PAYLOAD)
            } else {
                Log.v("Activity", "Does not have custom payload.")
            }
        }

Use Case 2: If Application is in the foreground and the user clicks on notification.

In this case, App has to register a BroadcastReceiver with the following intent filter and the data will be received in the onReceive method of the service class.

Register the BroadcastReceiver inside the Application class.

DeeplinkReceiver deeplinkReceiver = new DeeplinkReceiver();
        IntentFilter filter = new IntentFilter("com.smartech.EVENT_PN_INBOX_CLICK");
        context.registerReceiver(deeplinkReceiver, filter);
val deeplinkReceiver = DeeplinkReceiver()
        val filter = IntentFilter("com.smartech.EVENT_PN_INBOX_CLICK")
        registerReceiver(deeplinkReceiver, filter)

Implementation for BroadcastReciever class.

public class DeeplinkReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundleExtra = getIntent().getExtras();
        if (bundleExtra != null) {

            if (bundleExtra.containsKey(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_DEEPLINK)) {
                String deepLinkvalue = bundleExtra.getString(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_DEEPLINK);
            } else {
                Log.v("Activity", "does not have deeplink path.");
            }

            if (bundleExtra.containsKey(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_CUSTOM_PAYLOAD)) {
                String customPayloadvalue = bundleExtra.getString(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_CUSTOM_PAYLOAD);
            } else {
                Log.v("Activity", "does not have custom payload.");
            }

        }
    }
}
class DeeplinkReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        val bundleExtra = intent.extras
        if (bundleExtra != null) {
            if (bundleExtra.containsKey(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_DEEPLINK)) {
                val deepLinkvalue = bundleExtra.getString(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_DEEPLINK)
            } else {
                Log.v("Activity", "Does not have deeplink path.")
            }
            if (bundleExtra.containsKey(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_CUSTOM_PAYLOAD)) {
                val customPayloadvalue = bundleExtra.getString(SMTBundleKeys.SMT_BUNDLE_KEY_CLICK_CUSTOM_PAYLOAD)
            } else {
                Log.v("Activity", "Does not have custom payload.")
            }
        }
    }
}