Start by finding the file you want to add nudge to
We recommend adding the library in your MainActivity file or the file defined as the app’s launch activity in your app’s AndroidManifest.
Import the package alongside your other imports
import com.larky.nudge.Nudge;
Declare the nudge object
This is usually done at the activity level scope
private var myNudge: Nudge? = null
You can initialize nudge by using NudgeBuilder
In the OnCreate method of your activity, initialize the nudge library as follows
| Basic Implementation | Location-Aware Implementation |
myNudge = Nudge.NudgeBuilder()
.setNudgeVersion(Nudge.NudgeVersion.NUDGE_STANDARD)
.setActivity(this)
.setApplicationContext(getApplicationContext)
.setApiKey("<YOUR_API_KEY_HERE>")
.setResourceId(int)
.setStatusUpdateCallback(MyNudgeStatusNotifier())
.build()
|
myNudge = Nudge.NudgeBuilder()
.setNudgeVersion(Nudge.NudgeVersion.NUDGE_GEO)
.setActivity(this)
.setApplicationContext(getApplicationContext)
.setApiKey("<YOUR_API_KEY_HERE>")
.setResourceId(int)
.setStatusUpdateCallback(MyNudgeStatusNotifier())
.build() |
Parameters:
Common parameters for both basic and location-aware implementations:
-
setNudgeVersion(Nudge.NudgeVersion): Required* - Determines which version of nudge will be used by the library- Two options:
Nudge.NudgeVersion.NUDGE_STANDARDandNudge.NudgeVersion.NUDGE_GEO - * If you are using a Legacy Implementation do not include this parameter
- Two options:
-
setActivity(Activity): Required - The current Android activity is needed by the library to display location permissions dialogs -
setApplicationContext(Context): Required - Your app’s current Application Context -
setApiKey(String): Required - This is your Larky provided nudge API key -
setResourceId(int): ID of icon to be displayed in nudge notifications. NOTE: The icon must be white and have a transparent background (.png) -
setStatusUpdateCallback(MyNudgeStatusNotifier): Optional - Provide the listener to the client when the Active status of the library changes.
Monitoring for change in Nudge status
If you want, you can register a callback to get notified whenever the status of the Nudge instance changes. To do so, use the NudgeStatusNotifier public interface.
internal inner class myNudgeStatusListener :
Nudge.NudgeStatusNotifier {
override fun onNudgeStatusChange(active: Boolean,
status: String) {
Log.i(TAG, (if (active) "Active - "
else "Inactive - ") + status)
}
} You can then set the callback using setStatusUpdateCallback() in NudgeBuilder.
myNudge = Nudge.NudgeBuilder() ... .setStatusUpdateCallback(myNudgeStatusListener()) ...
That's it! You can now distribute your app to your users and they'll be registered within the nudge ecosystem. You'll be able to send them notifications through your nudge dashboard!
Additional notes
If you already have Firebase Cloud Messenger installed
If you already use FCM, there's no need to worry! You can still use nudge alongside your existing project. You just need to edit your onMessageReceived method as follows in your FirebaseMessagingService. Make sure you include the line import com.larky.nudge.Nudge at the top of the file alongside your other import statements.
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.data != null) {
val data = remoteMessage.data
val isNudge = data["isNudge"]
if (java.lang.Boolean.parseBoolean(isNudge)) {
Nudge.handlePush(getApplicationContext(), remoteMessage)
Log.d(TAG,"nudge notification received")
}
else {
//Your code to handle your non-nudge data notifications
Log.d(TAG, "Non-nudge data notification received")
}
}
//Handle non-data notifications
Log.d(TAG,"Non-data notification received")
} Deprecated Firebase APIs
Also, we have found that developers may have problems getting the correct token if they implement the deprecated service FirebaseInstanceIDService or method getToken(). Please only use the FirebaseMessagingService method onNewToken() to check for new tokens.
If you're having trouble retrieving the correct token for other Firebase project(s), this is likely the source of the problem.
Comments
0 comments
Article is closed for comments.