android 未调用Onesignal远程通知接收方法

ioekq8ef  于 2022-12-16  发布在  Android
关注(0)|答案(3)|浏览(130)

我想在我的数据库中保存通知,因此我从www.example.com按照文档的两个步骤操作https://documentation.onesignal.com/docs/android-native-sdk#notificationserviceextension但根本没有调用remoteNotificationReceived方法。我使用了setNotificationWillShowInForegroundHandler,如下所示,它可以正常工作,但我想即使在应用程序处于后台时也能接收通知

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

我还应该提到,推送通知工作正常,我在设备上收到通知

atmip9wb

atmip9wb1#

我通过在一个单独的空类中实现OSRemoteNotificationReceivedHandler来解决这个问题

5w9g7ksd

5w9g7ksd2#

您需要为后台通知处理添加实现。
检查以下文档链接

我正在使用NotificationExtenderService,它工作正常。

hgncfbus

hgncfbus3#

将其作为内部类使用。Kotlin中的代码如下:

1-创建新类

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-然后在MainActivity中实现

class MainActivity : AppCompatActivity(), OnOSNotificationReceivedListener {

    // ....

    override fun onOSNotificationReceived(notification: OSNotification) {

        // Your code goes here!

    }

    // ....
}

相关问题