OneSignal-Flutter-SDK icon indicating copy to clipboard operation
OneSignal-Flutter-SDK copied to clipboard

[question]: No method of notification received in background and app terminated state?

Open mhammadraza137 opened this issue 1 year ago • 8 comments

How can we help?

There is no method that will be triggered when notification is received in background and terminated state. Like in FCM we have method like FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); Please let me know if this method is already present and if it does not exists then please make this one.

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

mhammadraza137 avatar Jun 27 '24 11:06 mhammadraza137

Same question here

arioul89 avatar Aug 09 '24 11:08 arioul89

Same question here

mmustafaget avatar Sep 12 '24 09:09 mmustafaget

any update on this?

is there no way to execute something on the event of when a notification is received while the app is terminated?

muhammad67uzair avatar Sep 12 '24 11:09 muhammad67uzair

did anyone find any solution same problme with me how to capture background data when app is in killed state

sunil-singh-chaudhary avatar Jan 02 '25 09:01 sunil-singh-chaudhary

any update on this?

did anyone find any solution, how to capture background data when app is in killed state

JiyadAhammad avatar Mar 07 '25 19:03 JiyadAhammad

any update on this?

did anyone find any solution, how to capture background data when app is in killed state

only way i found is use firebase for backrgound data and for notificaiton use onesignal , else there is way to do it direactly in android and use it but it is not a good idea using like that becuase there is lot of issue after using that way

sunil-singh-chaudhary avatar Mar 10 '25 13:03 sunil-singh-chaudhary

on Android, when the app is in background or terminated, need to create another Flutter engine then invoke the callback, I suggest to check the firebase_messaging source code and implement the same for one signal, the code is already there so there's not much effort.

https://github.com/firebase/flutterfire/blob/main/packages/firebase_messaging/firebase_messaging/android/src/main/java/io/flutter/plugins/firebase/messaging/FlutterFirebaseMessagingBackgroundExecutor.java

quan-ngo-ts avatar May 21 '25 09:05 quan-ngo-ts

for temporary solution, can follow this document https://documentation.onesignal.com/docs/service-extensions#troubleshooting-the-ios-notification-service-extension the content of the extension is

package com.example.flutter_callkit_incoming_example

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.onesignal.notifications.INotification
import com.onesignal.notifications.INotificationReceivedEvent
import com.onesignal.notifications.INotificationServiceExtension
import org.json.JSONObject


class NotificationServiceExtension: INotificationServiceExtension {
    companion object {
        const val FIREBASE_MESSAGING_ACTION = "com.google.android.c2dm.intent.RECEIVE"
        const val FIREBASE_MESSAGING_RECEIVER = "io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingReceiver"
    }

    override fun onNotificationReceived(event: INotificationReceivedEvent) {
        val notification = event.notification
        val context = event.context

        Log.d("OneSignalForward", "OneSignal notification received")
        Log.d("OneSignalForward", "Title: ${notification.title}")
        Log.d("OneSignalForward", "Body: ${notification.body}")
        Log.d("OneSignalForward", "Notification ID: ${notification.notificationId}")

        // Create Firebase Cloud Messaging compatible intent
        val firebaseIntent = Intent(FIREBASE_MESSAGING_ACTION).apply {
            // Set the component to target FlutterFirebaseMessagingReceiver
            component = ComponentName(context.packageName, FIREBASE_MESSAGING_RECEIVER)

            // Create Firebase-compatible extras bundle
            val extras = Bundle().apply {
                // Firebase messaging format - notification data
                notification.title?.let { putString("gcm.notification.title", it) }
                notification.body?.let { putString("gcm.notification.body", it) }

                // Add OneSignal notification ID as message ID
                putString("google.message_id", notification.notificationId)

                // Add timestamp (Firebase expects this in milliseconds)
                putString("google.sent_time", System.currentTimeMillis().toString())

                // Add required Firebase fields
                putString("from", "OneSignal")
                putString("collapse_key", "onesignal_notification")

                // Add message type to indicate this is a notification
                putString("google.message_type", "gcm")

                // Add custom data fields - these will be available in RemoteMessage.getData()
                notification.additionalData?.let { additionalData ->
                    val keys = additionalData.keys()
                    while (keys.hasNext()) {
                        val key = keys.next()
                        try {
                            val value = additionalData.get(key)
                            putString(key, value.toString())
                        } catch (e: Exception) {
                            Log.w("OneSignalForward", "Error extracting additional data key: $key", e)
                        }
                    }
                }

                // Add OneSignal specific metadata as data fields
                putString("onesignal_notification_id", notification.notificationId)
                notification.launchURL?.let { putString("onesignal_launch_url", it) }
                notification.sound?.let { putString("onesignal_sound", it) }
                putString("onesignal_priority", notification.priority.toString())
                putString("onesignal_source", "true")
            }

            // Add the extras to the intent
            putExtras(extras)
        }
        // Send to FlutterFirebaseMessagingReceiver
        try {
            context.sendBroadcast(firebaseIntent)
            Log.d("OneSignalForward", "OneSignal notification forwarded to FlutterFirebaseMessagingReceiver successfully")
            Log.d("OneSignalForward", "Intent action: ${firebaseIntent.action}")
            Log.d("OneSignalForward", "Intent component: ${firebaseIntent.component}")
            Log.d("OneSignalForward", "Intent extras count: ${firebaseIntent.extras?.size()}")
        } catch (e: Exception) {
            Log.e("OneSignalForward", "Error forwarding notification to FlutterFirebaseMessagingReceiver", e)
        }
    }
}

then add firebase_messaging to your app, your dart code FCM onMessage, onBackgroundMessage will be triggered when ever receive the notification from OneSignal in background, terminated (Android).

quan-ngo-ts avatar May 26 '25 02:05 quan-ngo-ts