Home > Enterprise >  android background push -> intent-filter click_action not working
android background push -> intent-filter click_action not working

Time:08-25

When I send firebase cloud message, I touched the notification message in background of android device.

but app was not launched.

even if I set the click_action, and intent-filter in Manifest file.

I dont know what problem is.

I just want to launch app when I touch background push notification . but even if I touched background push notification, no launch the app

send message

{
    "message": {
        "android": {
            "data": {
                "scheme": "",
                "payload": "null",
                "background": "false",
                "userId": "userId"
            },
            "notification": {
                "body": "sdf",
                "click_action": "DeepLinkHandlerActivity",
                "default_light_settings": true,
                "default_sound": true,
                "default_vibrate_timings": true,
                "title": "test"
            }
        },
        "apns": {
            "payload": {
                "aps": {
                    "alert": {
                        "body": "sdf",
                        "title": "test"
                    },
                    "badge": 1,
                    "sound": "default"
                }
            }
        },
        "data": {
            "scheme": "",
            "payload": "null",
            "userId": "b8ebe243-7d3b-41d8-b986-6abcb0fbdebf",
            "background": "false"
        },
        "notification": {
            "body": "sdf",
            "title": "test"
        },
        "token": "tokendata"
    }
}

Manifestfile

<activity
            android:name=".ui.activity.login.SplashActivity"
            android:label="@string/app_name"
            android:exported="false"
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="DeepLinkHandlerActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

service in manifest file

 <service
            android:name=".firebase.MyFirebaseMessagingService"
            android:exported="false"
            android:directBootAware="true"
            android:stopWithTask="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

CodePudding user response:

did you create a service named FirebaseMessagingService or similarly named? if you have created it, you need to create intent and PendingIntent there and add it. If you do not have such a service, you need to prepare the service. While researching for you, I found a convenient example; https://www.geeksforgeeks.org/how-to-push-notification-in-android-using-firebase-cloud-messaging/

CodePudding user response:

FirevaseMessagingService.kt source file

remoteMessage.notification?.let { notification ->
            Log.d(CONST.LOG_DEBUG, "click action ${notification.clickAction}")
            if(!notification.title.isNullOrEmpty()) {
                val pattern = longArrayOf(100, 500, 100, 500, 100, 700)
                var intent = Intent(this@MyFirebaseMessagingService, MainActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                intent.putExtra("badge_count", 1)
                //intent.putExtra("scheme", remoteMessage.data["scheme"])

                if(remoteMessage.data != null) {
                    val scheme = remoteMessage.data["scheme"]
                    scheme?.let {
                        if(it.startsWith("WEBVIEW")) {
                            val url = it.split("_")[1]
                            intent = Intent(this@MyFirebaseMessagingService, MainActivity::class.java)
                            intent.putExtra("url", url)
                            intent.putExtra("scheme", it)
                        } else if(it.startsWith("TECHPOST")) {
                            val id = it.split("_")[1]
                            intent = Intent(this@MyFirebaseMessagingService, TechPostDetailActivity::class.java)
                            intent.putExtra("id", id)
                            intent.putExtra("scheme", it)
                        } else if(it.startsWith("HOTCLIP")) {
                            val id = it.split("_")[1]
                            intent = Intent(this@MyFirebaseMessagingService, HotClipDetailActivity::class.java)
                            intent.putExtra("id", id)
                            intent.putExtra("scheme", it)
                        } else if(it.startsWith("NOTICE")) {
                            val id = it.split("_")[1]
                            intent = Intent(this@MyFirebaseMessagingService, NotificationDetailActivity::class.java)
                            intent.putExtra("id", id)
                            intent.putExtra("scheme", it)
                        } else {

                        }
                    }
                }
                val mPendingIntent = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
                } else {
                    PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
                }

                var builder = NotificationCompat.Builder(this , "default")
                builder.setFullScreenIntent(mPendingIntent , true)
                builder.setSmallIcon(R.mipmap.appicon)
                builder.setContentTitle(remoteMessage.notification?.title)
                builder.setContentText(remoteMessage.notification?.body)
                //builder.setColor(Color.BLUE)
                builder.setAutoCancel(true)
                builder.setContentIntent(mPendingIntent)
                builder.setVibrate(pattern)

                val notificationManager: NotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    notificationManager.createNotificationChannel(NotificationChannel("default" , "default_channel" , NotificationManager.IMPORTANCE_DEFAULT))
                }
                notificationManager.notify(1 , builder.build())
            }
        }
  • Related