TheJadav

Android – Handle additional parameter in onesignal

This post is to demonstrate how we can handle additional parameters in onesignal. If you don't know how to add onesignal in android, please visit my previous post. We need to override OneSignal.NotificationOpenedHandler.

Handle additional parameter in onesignal

This post is to demonstrate how we can handle additional parameters in onesignal. If you don’t know how to add onesignal in android, please visit my previous post.
We need to override OneSignal.NotificationOpenedHandler.

NotificationOpenedHandler

import android.app.Application
import android.content.Intent
import com.onesignal.OSNotificationOpenResult
import com.onesignal.OneSignal

class ITradeZonNotificationHandler(private val app: Application) : OneSignal.NotificationOpenedHandler{
    override fun notificationOpened(result: OSNotificationOpenResult?) {
        if(result == null) return
        val jsonData = result.notification.payload.additionalData

        if(jsonData != null){
            val orderId = jsonData.optString("orderId", null)
            if(orderId != null){
                //We are ready to open order details for this orderId
                app.startActivity(Intent(app, OrderDetailsActivity::class.java).putExtra("orderId", orderId))
            }
        }
    }
}

Note that, here we are passing application in class, it is required to start activity from Handler. This class will handle all push-notifications. We also need to add this class, when we init oneSignal. Look below code

      // This must be added in Application class to init onesignal
      OneSignal.startInit(this)
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
            .unsubscribeWhenNotificationsAreDisabled(true)
            .setNotificationOpenedHandler(ITradeZonNotificationHandler(this))
            .init()

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message