1

I am using the Firebase Cloud Messaging, the problem is if application is on foreground, after clicking the application will open, but if application is not on foreground, then after clicking the application will not open.

I have tried almost every solution posted here and combination of every flag but it is not working:

This is fragment code of manifest:

    <activity
        android:name=".ui.activities.StartActivity"
        android:screenOrientation="portrait"
        android:theme="@style/LightThemeTransparentStatusBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

This my example code:

class MyFirebaseMessagingService: FirebaseMessagingService {

constructor() : super()

override fun onMessageReceived(p0: RemoteMessage?) {
    super.onMessageReceived(p0)
    try {
        val notificationManager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val notificationId = Random().nextInt(60000)
        val bitmap = getBitmapFromURL(p0!!.data.get("image-url"))

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationManager.createNotificationChannel(setupChannels())
        }

        val intent = Intent(this@MyFirebaseMessagingService,StartActivity::class.java)
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)

        val pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT)

        val notificationBuilder = NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
            .setLargeIcon(bitmap)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(p0.notification!!.title)
            .setContentText(p0.notification!!.body)
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent)
        notificationManager.notify(notificationId,notificationBuilder.build())

    }catch (ex:java.lang.Exception){
        Log.e(TAG,ex.message)
    }
}

fun getBitmapFromURL(imageURL: String?): Bitmap? {
    try {
        val url = URL(imageURL)
        val connection = url.openConnection() as HttpURLConnection
        connection.doInput = true
        connection.connect()
        val input = connection.inputStream
        return BitmapFactory.decodeStream(input)
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    }

}

@RequiresApi(api = Build.VERSION_CODES.O)
private fun setupChannels() : NotificationChannel {
    val adminChannelName = "Global channel"
    val adminChannel: NotificationChannel
    adminChannel = NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW)
    adminChannel.enableLights(true)
    adminChannel.lightColor = Color.RED
    adminChannel.enableVibration(true)
    return adminChannel
}


companion object {
    private val TAG=MyFirebaseMessagingService::class.java.name
    private val ADMIN_CHANNEL_ID = "admin_channel"
}

}

ABDUAHAD
  • 344
  • 2
  • 13
  • If your `RemoteMessage` contains notification, `onMessageReceived` won't be called while application is in foreground. Android system shows notification using notification object in json which is in `RemoteMessage`. – Bek Oct 21 '19 at 11:12
  • In the foreground case, your activity is not opening or wrong activity is opening? What is your problem? – Amit Tiwary Oct 21 '19 at 11:15
  • Remove notification object from json (i.e topic message) which you send to devices in order `onMessageReceived` to be called from foreground. – Bek Oct 21 '19 at 11:21
  • Amit Tiwary, activity not opening. – ABDUAHAD Oct 22 '19 at 04:05
  • can you show, how you are sending push notification? – Amit Tiwary Oct 22 '19 at 05:07
  • I can't, because I have not access for Back- end. We have a topic "news", I have subscribed on topic, and they send push with onclick_action. – ABDUAHAD Oct 23 '19 at 04:00

0 Answers0