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:
- Open application after clicking on Notification
- GCM Notification onclick does not open specified activity
- FCM Notification onclick does not open desired activity
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"
}
}