Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question]: Issues showing notification from background #2228

Open
1 task done
arbyruns opened this issue Dec 6, 2024 · 0 comments
Open
1 task done

[question]: Issues showing notification from background #2228

arbyruns opened this issue Dec 6, 2024 · 0 comments

Comments

@arbyruns
Copy link

arbyruns commented Dec 6, 2024

How can we help?

I'm trying to prompt a notification from the background. Currently, I'm not sending any contents or headings and I am sending content_available: true

I've added the service extension and I can update the content as needed, but it does not display.

@Keep // Keep is required to prevent minification from renaming or removing your class
class NotificationServiceExtension : INotificationServiceExtension {

    override fun onNotificationReceived(event: INotificationReceivedEvent) {
        val notification = event.notification
        Log.d("NotificationServiceExtension", "Notification received with additional data: ${notification.additionalData}")

        // Customize the notification
        val builder = NotificationCompat.Builder(event.context, "default_channel_id")
            .setColor(-0xc87c66) // Example color
            .setContentTitle("New ${notification.additionalData?.get("type") ?: ""} review")
            .setContentText("Check out the latest review for ${notification.additionalData?.get("location") ?: ""}!")
            .setSmallIcon(android.R.drawable.ic_notification_overlay) // Set your own icon here
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

        // Add custom actions (optional)
        notification.actionButtons?.forEach { button ->
            builder.addAction(0, button.text, null) // You can add actions here if needed
        }

        // Trigger the notification to display
        val notificationManager = event.context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(123, builder.build())
        Log.d("NotificationServiceExtension", "Post builder")
        // Optionally, if you need to perform additional background work or stop the notification from showing immediately
        // Use event.preventDefault() to delay or stop its display until you've finished custom logic
        event.notification.display()
    }
}

Code of Conduct

  • I agree to follow this project's Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant