Mahdi Taheri
Mahdi Taheri

Reputation: 93

Onesignal remote Notification Received method not called

I want to save notifications in my database so i follow two steps of the document from here https://documentation.onesignal.com/docs/android-native-sdk#notificationserviceextension but remoteNotificationReceived method is not called at all. I used setNotificationWillShowInForegroundHandler like below and it works but I want to receive notifications even when app is in the background

OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        // not called while in background
});

I also should mention that push notification works fine and I get notifications on device

Upvotes: 3

Views: 5245

Answers (3)

Osama Remlawi
Osama Remlawi

Reputation: 2990

Use it as an internal class. This is the code in Kotlin:

1- Create a new class

import android.util.Log
import com.onesignal.OSNotification
import com.onesignal.OSNotificationReceivedEvent
import com.onesignal.OneSignal


internal class OSNotificationReceivedHandler(

    private val application: Application

) : OneSignal.OSNotificationWillShowInForegroundHandler {


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


    override fun notificationReceived(notification: OSNotification) {

        val data = notification.additionalData

        val notificationID = notification.notificationId
        val title = notification.title
        val body = notification.body
        // ...

        Log.i(TAG, "NotificationID received: $notificationID")

        for (listener in application.onOSNotificationReceivedListeners)
            listener.onOSNotificationReceived(notification)
    }

    override fun notificationWillShowInForeground(event: OSNotificationReceivedEvent?) {
        OneSignal.onesignalLog(
        OneSignal.LOG_LEVEL.VERBOSE, "NotificationWillShowInForegroundHandler fired!" +
                " with notification event: " + event.toString()
    )

        val notification: OSNotification = event!!.notification
        val data = notification.additionalData

        // Complete with null means don't show a notification.
        event.complete(notification)

        for (listener in application.onOSNotificationReceivedListeners)
        listener.onOSNotificationReceived(notification)
    }

}

2- And then implement it in MainActivity

class MainActivity : AppCompatActivity(), OnOSNotificationReceivedListener {

    // ....

    override fun onOSNotificationReceived(notification: OSNotification) {

        // Your code goes here!

    }

    // ....
}

Upvotes: 0

Mahdi Taheri
Mahdi Taheri

Reputation: 93

I figure it out by implementing OSRemoteNotificationReceivedHandler in an empty and separate class

Upvotes: 1

Adeel Javed
Adeel Javed

Reputation: 372

You need to add implementation for the background notification handling.

Check below documentation links

I am using NotificationExtenderService and it's working fine.

Upvotes: 0

Related Questions