skip to Main Content

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>

3

Answers


  1. Chosen as BEST ANSWER

    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())
                }
            }
    

  2. 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/

    Login or Signup to reply.
  3. I examined the codes and there are 2 types of messages in Google Firebase Messaging, one of them is "notification" and the other is "data" and you have done data control on the code. How do you send the notification when you send it? Notification can be sent through the firebase panel, but the data message is sent only through the server. When sending a message, you need to check which of the "Notification" – "Data" you are sending. In the "Notification" notifications, the codes you wrote while the application is in the kill state will not work, but if you send a "Data" notification, the codes you wrote when the application is killed will also work. Therefore, if the notification you sent is "Notification", you need to change it to "Data". As I said, "Data" Notifications cannot be sent through the Panel, they are only sent from the server.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search