Skip to content

Commit

Permalink
Merge pull request #25 from rees46/feat/mobile-notification-kotlin
Browse files Browse the repository at this point in the history
docs(notification_service): android push description kotlin
  • Loading branch information
TorinAsakura authored Jun 17, 2024
2 parents 97907b4 + d389936 commit f777438
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions source/includes/_notification_service.md.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Handling Mobile Push Notifications

```swift
# The first step is to make sure that `autoSendPushToken = true` during SDK initialization
# it's a default value.
# `NotificationService` object is initializing with `SimplePersonalizationSDK`
Expand Down Expand Up @@ -36,3 +39,58 @@
# Additional custom code
notificationService?.didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: deviceToken)
}
```



```kotlin
// To provide a comprehensive answer and cover all methods for handling mobile push notifications from your code, here's an extended response:

// User received on mobile push**:
// The `notificationReceived` method in the `SDK` class handles the event of receiving a push notification on a mobile device. This method is called upon receiving a notification, and it sends an asynchronous request to track this event.

/**
* Handles the event of receiving a push notification on a mobile device.
* @param data Data from the notification
*/

fun notificationReceived(data: Map<String, String>) {
val params = JSONObject()
try {
val type = data["type"]
if (type != null) {
params.put("type", type)
}
val id = data["id"]
if (id != null) {
params.put("code", id)
}
if (params.length() > 0) {
sendAsync("track/received", params)
}
} catch (e: JSONException) {
Log.e(TAG, e.message, e)
}
}

/**
* Handles the event of a user clicking on a push notification.
* @param extras Additional data from the notification
*/

fun notificationClicked(extras: Bundle?) {
val type = extras?.getString(NOTIFICATION_TYPE, null)
val code = extras?.getString(NOTIFICATION_ID, null)
if (type != null && code != null) {
val params = JSONObject()
try {
params.put("type", type)
params.put("code", code)
sendAsync("track/clicked", params)
source.update(type, code, prefs())
} catch (e: JSONException) {
Log.e(TAG, e.message, e)
}
}
}
```

0 comments on commit f777438

Please sign in to comment.