Android Consumer Authentication Setup
Setup your application for Consumer Authentication (Web Account Unlock) using Firebase Cloud Messaging for push notifications.
DO THIS FIRST
This article assumes you've completed Android General Setup.
Enable Google Services
- At the top of
app/build.gradle
, add the following:
apply plugin: 'com.google.gms.google-services'
- In the project
build.gradle
, add the following:
buildscript {
dependencies {
// ...
classpath 'com.google.gms:google-services:4.0.1'
}
}
Firebase Integration
- Reference the Firebase libraries for push notifications. In
app/build.gradle
, add the following:
dependencies {
// ...
// For Push Notifications in Consumer Access Authentication
implementation 'com.google.firebase:firebase-core:17.4.3'
implementation 'com.google.firebase:firebase-messaging:20.2.1'
implementation 'com.google.android.gms:play-services-vision:20.1.0'
implementation 'com.google.android.gms:play-services-location:17.0.0'
}
- Create the Firebase Messaging Service subclass to handle the push notifications:
class CustomFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
HyprPushNotificationAdapter.getInstance()
.setOnNotificationPressedLaunchActivity(MainActivity::class.java)
.processRemoteMessage(applicationContext, remoteMessage.data)
}
override fun onNewToken(token: String) {
HyprPushNotificationAdapter.getInstance().onNewPushChannelId(applicationContext, token)
}
}
- Add the Firebase Messaging Service subclass to the
AndroidManifest.xml
.
<application>
// ...
<service android:name=".CustomFirebaseMessagingService"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
- Ensure that the Firebase Service can continue to run even when the app is closed by using the following permissions in
AndroidManifest.xml
:
// Ensures that the Firebase Service can continue to run even when the app is closed by using the following permissions in the AndroidManifest
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
HYPR Initialization
- When the app has started, initialize the HYPR SDK for Android v2 during
onCreate()
of your first activity.
fun initializeHYPR() {
HyprApp.initializeApp(context, object : HyprInit.InitTrustDataCallback {
override fun onInstallComplete() {
setupForPushNotifications()
}
override fun onInstallError(errorString: String, p1: Throwable?) {
// Log the error
}
})
}
fun setupForPushNotifications() {
FirebaseApp.initializeApp(this)
HyprPushNotificationAdapter.getInstance().setPushId(this, <firebaseGcmSenderId>)
}
Processing the Push Notification
- Process the push notification when the App is in the foreground. We will be registering a broadcast receiver to handle the push notification in the current activity.
override fun onResume() {
super.onResume()
HyprPushNotificationAdapter.getInstance().registerBroadcastReceiver(this, object : HyprActionCallbacks.HyprWebLoginCallback {
override fun onSuccess(webLoginType: HyprActionCallbacks.WebLoginType, p1: HyprStatusResult) {
// Success!
}
override fun onFailure(webLoginType: HyprActionCallbacks.WebLoginType, statusResult: HyprStatusResult) {
// Fail!
}
})
}
override fun onPause() {
super.onPause()
HyprPushNotificationAdapter.getInstance().unregisterBroadcastReceiver(this)
}
- Process the push notification when the App is not in the foreground. The push notification will arrive and will open the App. The push notification must be processed by the currently foregrounded Activity.
override fun onResume() {
super.onResume()
if (HyprApp.getDbAdapter().isPushPayloadAvailable(this)) {
HyprActions.getInstance().startPushNotificationIntent(this,
object : HyprActionCallbacks.HyprWebLoginCallback {
override fun onSuccess(webLoginType: HyprActionCallbacks.WebLoginType,
statusResult: HyprStatusResult) {
// Success!
}
override fun onFailure(webLoginType: HyprActionCallbacks.WebLoginType,
statusResult: HyprStatusResult
) {
// Error!
}
})
}
}
- If push notifications are not available, retrieve the push payload explicitly through this API call.
PUSH PAYLOAD RETRIEVAL CONSTRAINTS
To retrieve the push notification payload to Web Authenticate, a push to Authenticate must have been initiated for that particular web account within 60s of the method being called.
HyprActions.getInstance().checkServerForPendingAuthentication(activity, webAccount.dbId, object: HyprActionCallbacks.HyprActionCallback {
override fun onSuccess(statusResult: HyprStatusResult) {
HyprActions.getInstance().startPendingAuthentication(activity, object: HyprActionCallbacks.HyprWebLoginCallback {
override fun onSuccess(webLoginType: HyprActionCallbacks.WebLoginType, statusResult: HyprStatusResult) {
// Success!
}
override fun onFailure(webLoginType: HyprActionCallbacks.WebLoginType, statusResult: HyprStatusResult) {
// Fail!
}
})
}
override fun onFailure(statusResult: HyprStatusResult) {
// Fail!
}
})
...