Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.util.Log
import com.huawei.hms.push.HmsMessageService
import com.huawei.hms.push.RemoteMessage
import com.onesignal.notifications.bridges.OneSignalHmsEventBridge
import com.onesignal.sdktest.util.LogManager

/**
* HMS Message Service for handling Huawei Push notifications.
Expand All @@ -14,10 +15,6 @@ import com.onesignal.notifications.bridges.OneSignalHmsEventBridge
*/
class HmsMessageServiceAppLevel : HmsMessageService() {

companion object {
private const val TAG = "OneSignalHMS"
}

/**
* When an app calls the getToken method to apply for a token from the server,
* if the server does not return the token during current method calling,
Expand All @@ -26,15 +23,15 @@ class HmsMessageServiceAppLevel : HmsMessageService() {
* Otherwise, you need to start a new Job for callback processing.
*/
override fun onNewToken(token: String, bundle: Bundle) {
Log.d(TAG, "HmsMessageServiceAppLevel onNewToken refresh token: $token bundle: $bundle")
Log.d(LogManager.TAG, "HmsMessageServiceAppLevel onNewToken refresh token: $token bundle: $bundle")

// Forward event on to OneSignal SDK
OneSignalHmsEventBridge.onNewToken(this, token, bundle)
}

@Deprecated("Deprecated in Java")
override fun onNewToken(token: String) {
Log.d(TAG, "HmsMessageServiceAppLevel onNewToken refresh token: $token")
Log.d(LogManager.TAG, "HmsMessageServiceAppLevel onNewToken refresh token: $token")

// Forward event on to OneSignal SDK
OneSignalHmsEventBridge.onNewToken(this, token)
Expand All @@ -48,18 +45,18 @@ class HmsMessageServiceAppLevel : HmsMessageService() {
* Start a new Job if more time is needed.
*/
override fun onMessageReceived(message: RemoteMessage) {
Log.d(TAG, "HMS onMessageReceived: $message")
Log.d(TAG, "HMS onMessageReceived.ttl: ${message.ttl}")
Log.d(TAG, "HMS onMessageReceived.data: ${message.data}")
Log.d(LogManager.TAG, "HMS onMessageReceived: $message")
Log.d(LogManager.TAG, "HMS onMessageReceived.ttl: ${message.ttl}")
Log.d(LogManager.TAG, "HMS onMessageReceived.data: ${message.data}")

message.notification?.let { notification ->
Log.d(TAG, "HMS onMessageReceived.title: ${notification.title}")
Log.d(TAG, "HMS onMessageReceived.body: ${notification.body}")
Log.d(TAG, "HMS onMessageReceived.icon: ${notification.icon}")
Log.d(TAG, "HMS onMessageReceived.color: ${notification.color}")
Log.d(TAG, "HMS onMessageReceived.channelId: ${notification.channelId}")
Log.d(TAG, "HMS onMessageReceived.imageURL: ${notification.imageUrl}")
Log.d(TAG, "HMS onMessageReceived.tag: ${notification.tag}")
Log.d(LogManager.TAG, "HMS onMessageReceived.title: ${notification.title}")
Log.d(LogManager.TAG, "HMS onMessageReceived.body: ${notification.body}")
Log.d(LogManager.TAG, "HMS onMessageReceived.icon: ${notification.icon}")
Log.d(LogManager.TAG, "HMS onMessageReceived.color: ${notification.color}")
Log.d(LogManager.TAG, "HMS onMessageReceived.channelId: ${notification.channelId}")
Log.d(LogManager.TAG, "HMS onMessageReceived.imageURL: ${notification.imageUrl}")
Log.d(LogManager.TAG, "HMS onMessageReceived.tag: ${notification.tag}")
}

// Forward event on to OneSignal SDK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.onesignal.user.state.UserChangedState
class MainApplication : MultiDexApplication() {

companion object {
private const val TAG = "OneSignalExample"
private const val SLEEP_TIME_TO_MIMIC_ASYNC_OPERATION = 2000L
}

Expand All @@ -51,7 +50,7 @@ class MainApplication : MultiDexApplication() {
LogLevel.ERROR, LogLevel.FATAL -> AppLogLevel.ERROR
LogLevel.NONE -> return@addLogListener
}
LogManager.log("SDK", event.entry, level)
LogManager.appendToPanel(event.entry, level)
}

// Get or set the OneSignal App ID
Expand All @@ -74,7 +73,7 @@ class MainApplication : MultiDexApplication() {
// Initialize OneSignal on main thread (required)
// Crash handler + ANR detector are initialized early inside initWithContext
OneSignal.initWithContext(this, appId)
LogManager.i(TAG, "OneSignal init completed (crash handler, ANR detector, and logging active)")
LogManager.i("OneSignal init completed (crash handler, ANR detector, and logging active)")

// Set up all OneSignal listeners
setupOneSignalListeners()
Expand All @@ -86,37 +85,37 @@ class MainApplication : MultiDexApplication() {
private fun setupOneSignalListeners() {
OneSignal.InAppMessages.addLifecycleListener(object : IInAppMessageLifecycleListener {
override fun onWillDisplay(event: IInAppMessageWillDisplayEvent) {
LogManager.d(TAG, "onWillDisplayInAppMessage")
LogManager.d("onWillDisplayInAppMessage")
}

override fun onDidDisplay(event: IInAppMessageDidDisplayEvent) {
LogManager.d(TAG, "onDidDisplayInAppMessage")
LogManager.d("onDidDisplayInAppMessage")
}

override fun onWillDismiss(event: IInAppMessageWillDismissEvent) {
LogManager.d(TAG, "onWillDismissInAppMessage")
LogManager.d("onWillDismissInAppMessage")
}

override fun onDidDismiss(event: IInAppMessageDidDismissEvent) {
LogManager.d(TAG, "onDidDismissInAppMessage")
LogManager.d("onDidDismissInAppMessage")
}
})

OneSignal.InAppMessages.addClickListener(object : IInAppMessageClickListener {
override fun onClick(event: IInAppMessageClickEvent) {
LogManager.d(TAG, "IInAppMessageClickListener.onClick")
LogManager.d("IInAppMessageClickListener.onClick")
}
})

OneSignal.Notifications.addClickListener(object : INotificationClickListener {
override fun onClick(event: INotificationClickEvent) {
LogManager.d(TAG, "INotificationClickListener.onClick fired with event: $event")
LogManager.d("INotificationClickListener.onClick fired with event: $event")
}
})

OneSignal.Notifications.addForegroundLifecycleListener(object : INotificationLifecycleListener {
override fun onWillDisplay(event: INotificationWillDisplayEvent) {
LogManager.d(TAG, "INotificationLifecycleListener.onWillDisplay fired with event: $event")
LogManager.d("INotificationLifecycleListener.onWillDisplay fired with event: $event")

val notification: IDisplayableNotification = event.notification

Expand All @@ -135,7 +134,7 @@ class MainApplication : MultiDexApplication() {

OneSignal.User.addObserver(object : IUserStateObserver {
override fun onUserStateChange(state: UserChangedState) {
LogManager.i(TAG, "User state changed: onesignalId=${state.current.onesignalId}, externalId=${state.current.externalId}")
LogManager.i("User state changed: onesignalId=${state.current.onesignalId}, externalId=${state.current.externalId}")
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import java.net.URL
*/
object OneSignalService {

private const val TAG = "OneSignalService"
private const val ONESIGNAL_API_URL = "https://onesignal.com/api/v1/notifications"
private const val ONESIGNAL_API_BASE_URL = "https://api.onesignal.com"

Expand All @@ -37,13 +36,13 @@ object OneSignalService {
val subscription = OneSignal.User.pushSubscription

if (!subscription.optedIn) {
LogManager.w(TAG, "Cannot send notification - user not opted in")
LogManager.w("Cannot send notification - user not opted in")
return@withContext false
}

val subscriptionId = subscription.id
if (subscriptionId.isNullOrEmpty()) {
LogManager.w(TAG, "Cannot send notification - no subscription ID")
LogManager.w("Cannot send notification - no subscription ID")
return@withContext false
}

Expand All @@ -59,17 +58,17 @@ object OneSignalService {
// Add large icon if available
type.largeIcon?.let {
put("large_icon", it)
LogManager.d(TAG, "Adding large_icon: $it")
LogManager.d("Adding large_icon: $it")
}
// Add big picture if available
type.bigPicture?.let {
put("big_picture", it)
LogManager.d(TAG, "Adding big_picture: $it")
LogManager.d("Adding big_picture: $it")
}
}

LogManager.d(TAG, "Sending notification: ${notificationJson.toString(2)}")
LogManager.d(TAG, "Request URL: $ONESIGNAL_API_URL")
LogManager.d("Sending notification: ${notificationJson.toString(2)}")
LogManager.d("Request URL: $ONESIGNAL_API_URL")

val connection = (URL(ONESIGNAL_API_URL).openConnection() as HttpURLConnection).apply {
useCaches = false
Expand All @@ -90,16 +89,16 @@ object OneSignalService {

if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_CREATED) {
val response = connection.inputStream.bufferedReader().use { it.readText() }
LogManager.d(TAG, "Notification sent successfully: $response")
LogManager.d("Notification sent successfully: $response")
return@withContext true
} else {
val errorResponse = connection.errorStream?.bufferedReader()?.use { it.readText() } ?: "Unknown error"
LogManager.e(TAG, "Failed to send notification (HTTP $responseCode): $errorResponse")
LogManager.e(TAG, "Request body was: ${notificationJson.toString()}")
LogManager.e("Failed to send notification (HTTP $responseCode): $errorResponse")
LogManager.e("Request body was: ${notificationJson.toString()}")
return@withContext false
}
} catch (e: Exception) {
LogManager.e(TAG, "Error sending notification", e)
LogManager.e("Error sending notification", e)
return@withContext false
}
}
Expand All @@ -111,13 +110,13 @@ object OneSignalService {
val subscription = OneSignal.User.pushSubscription

if (!subscription.optedIn) {
LogManager.w(TAG, "Cannot send notification - user not opted in")
LogManager.w("Cannot send notification - user not opted in")
return@withContext false
}

val subscriptionId = subscription.id
if (subscriptionId.isNullOrEmpty()) {
LogManager.w(TAG, "Cannot send notification - no subscription ID")
LogManager.w("Cannot send notification - no subscription ID")
return@withContext false
}

Expand Down Expand Up @@ -150,15 +149,15 @@ object OneSignalService {

if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED || responseCode == HttpURLConnection.HTTP_CREATED) {
val response = connection.inputStream.bufferedReader().use { it.readText() }
LogManager.d(TAG, "Custom notification sent successfully: $response")
LogManager.d("Custom notification sent successfully: $response")
return@withContext true
} else {
val errorResponse = connection.errorStream?.bufferedReader()?.use { it.readText() } ?: "Unknown error"
LogManager.e(TAG, "Failed to send custom notification (HTTP $responseCode): $errorResponse")
LogManager.e("Failed to send custom notification (HTTP $responseCode): $errorResponse")
return@withContext false
}
} catch (e: Exception) {
LogManager.e(TAG, "Error sending custom notification", e)
LogManager.e("Error sending custom notification", e)
return@withContext false
}
}
Expand All @@ -173,18 +172,18 @@ object OneSignalService {
*/
suspend fun fetchUser(aliasLabel: String, aliasValue: String, jwt: String? = null): UserData? = withContext(Dispatchers.IO) {
if (aliasValue.isEmpty()) {
LogManager.w(TAG, "Cannot fetch user - aliasValue is empty")
LogManager.w("Cannot fetch user - aliasValue is empty")
return@withContext null
}

if (appId.isEmpty()) {
LogManager.w(TAG, "Cannot fetch user - appId not set")
LogManager.w("Cannot fetch user - appId not set")
return@withContext null
}

try {
val url = "$ONESIGNAL_API_BASE_URL/apps/$appId/users/by/$aliasLabel/$aliasValue"
LogManager.d(TAG, "Fetching user data from: $url")
LogManager.d("Fetching user data from: $url")

val connection = (URL(url).openConnection() as HttpURLConnection).apply {
useCaches = false
Expand All @@ -201,22 +200,22 @@ object OneSignalService {

if (responseCode == HttpURLConnection.HTTP_OK) {
val response = connection.inputStream.bufferedReader().use { it.readText() }
LogManager.d(TAG, "User data fetched successfully, parsing response...")
LogManager.d("User data fetched successfully, parsing response...")
try {
val userData = parseUserResponse(response)
LogManager.d(TAG, "Parsed user data: aliases=${userData.aliases.size}, tags=${userData.tags.size}, emails=${userData.emails.size}, sms=${userData.smsNumbers.size}")
LogManager.d("Parsed user data: aliases=${userData.aliases.size}, tags=${userData.tags.size}, emails=${userData.emails.size}, sms=${userData.smsNumbers.size}")
return@withContext userData
} catch (e: Exception) {
LogManager.e(TAG, "Error parsing user response", e)
LogManager.e("Error parsing user response", e)
return@withContext null
}
} else {
val errorResponse = connection.errorStream?.bufferedReader()?.use { it.readText() } ?: "Unknown error"
LogManager.e(TAG, "Failed to fetch user (HTTP $responseCode): $errorResponse")
LogManager.e("Failed to fetch user (HTTP $responseCode): $errorResponse")
return@withContext null
}
} catch (e: Exception) {
LogManager.e(TAG, "Error fetching user", e)
LogManager.e("Error fetching user", e)
return@withContext null
}
}
Expand Down
Loading
Loading