0

I have a problem with handling notification when app in background. Currently when I put the app in the background after clicking the notification it starts the first activity defined in manifest (in my case the LoginActivity). I want it to bring other activity to foreground if it's running.

This below is my activity declaration in manifest

    <activity
        android:name=".features.login.LoginActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name="co.blastlab.expo.features.event.EventActivity"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.NoActionBar"/>

And here below I have my sendNotification method.

private fun sendNotification(remoteMessage: RemoteMessage) {
    val notification = remoteMessage.notification
    val intent = Intent(this, EventActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)

    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this, defaultChannel)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.launcher_48dp))
            .setSmallIcon(R.drawable.icon)
            .setContentTitle(notification!!.title)
            .setContentText(notification.body)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setChannelId(defaultChannel)
            .setContentIntent(pendingIntent)

    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.notify(0, notificationBuilder.build())
}

I have no idea where to go next.

Vrushi Patel
  • 2,361
  • 1
  • 17
  • 30
Jakub Wisniewski
  • 2,189
  • 4
  • 20
  • 34
  • 1
    Something is wrong at your flags with a combination of "singleTop" launch mode. Check out this solution, https://stackoverflow.com/questions/50402625/pending-intent-opens-wrong-activity – Christos Themelis Mar 08 '19 at 09:42
  • @ChristosThemelis if I change SingleTop -> SingleTask then when I move app to background from EventActivity clicking icon restarts app but with SingleTop it brings back EventActivity – Jakub Wisniewski Mar 08 '19 at 09:58
  • Try changing the flags to intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); – Christos Themelis Mar 08 '19 at 10:04
  • @ChristosThemelis ok I think there is different problem - when app is in background methods from my service are not called – Jakub Wisniewski Mar 08 '19 at 10:33
  • I don't have all your project to know that. You know better than me. p.s. If you are running at Android 8+ you have to create JobService. – Christos Themelis Mar 08 '19 at 10:38
  • @ChristosThemelis https://wajahatkarim.com/2018/05/firebase-notifications-in-background--foreground-in-android/ – Jakub Wisniewski Mar 08 '19 at 10:50
  • You have also check about message priority (as described here: https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message ) and maybe you have to declare the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission in Manifest – Christos Themelis Mar 08 '19 at 11:09

0 Answers0