SDK

Scannables

This functionality will allow your app to present content after they have scanned a NFC Tag or QR Code (we use QR Codes whenever the device does not support NFC). This could be extremely useful if you're looking to extend physical objects with engaging content pretty much the same way you do when sending a notification.

Before you can start using this functionality you will need to create one or more tags in our dashboard. This is covered in our guides located here.

By default, we include an activity that is shown when a scannable session is started. If you need to customise it, for instance, the theme, add the following to your AndroidManifest.xml file:

<activity
    android:name="re.notifica.scannables.ScannableActivity"
    android:theme="@style/Theme.App" />

When NFC is not supported or disabled, our library will fall back to scanning QR codes.

In most cases you will want to start scanning a tag whenever the user opens your app and clicks a button in your UI. This would be done from one your app's activities or fragments like this:

findViewById<View>(R.id.button).setOnClickListener {
    Notificare.scannables().startScannableSession(this)
}
findViewById(R.id.button).setOnClickListener(view ->
        NotificareScannablesCompat.startScannableSession(activity)
);

You can also check manually if the device supports NFC scannable sessions and act accordingly.

if (Notificare.scannables().canStartNfcScannableSession) {
    Notificare.scannables().startNfcScannableSession(this)
} else {
    Notificare.scannables().startQrCodeScannableSession(this)
}
if (NotificareScannablesCompat.getCanStartNfcScannableSession()){
    NotificareScannablesCompat.startNfcScannableSession(this);
} else {
    NotificareScannablesCompat.startQrCodeScannableSession(this);
}

Once the user taps a NFC tag or scans a QR code, your app will be responsible for handling the results. In the activity that started the session you would then implement the following:

class MainActivity : AppCompatActivity(), NotificareScannables.ScannableSessionListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        // more code ...

        Notificare.scannables().addListener(this)
    }

    override fun onDestroy() {
        // more code ...

        Notificare.scannables().removeListener(this)
    }

    override fun onScannableDetected(scannable: NotificareScannable) {
        val notification = scannable.notification ?: run {
            // This scannable doesn't contain a notification.
            return
        }

        Notificare.pushUI().presentNotification(this, notification)
    }

    override fun onScannableSessionError(error: Exception) {
        if (error is NotificareUserCancelledScannableSessionException) {
            return
        }

        // Handle the error.
    }
}
public class MainActivity extends AppCompatActivity implements NotificareScannables.ScannableSessionListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // more code ...

        NotificareScannablesCompat.addListener(this);
    }

    @Override
    protected void onDestroy() {
        // more code ...

        NotificareScannablesCompat.removeListener(this);
    }

    @Override
    public void onScannableDetected(@NonNull NotificareScannable scannable) {
        NotificareNotification notification = scannable.getNotification();
        if (notification == null) {
            // This scannable doesn't contain a notification.
        } else {
            NotificarePushUICompat.presentNotification(this, notification);
        }
    }

    @Override
    public void onScannableSessionError(@NonNull Exception error) {
        if (error.equals(new NotificareUserCancelledScannableSessionException())) {
            return;
        }

        // Handle the error.
    }
}

This will make sure that as soon as the NFC tag is scanned your app will be able to handle results and present the content accordingly.