0

I am using firebase cloud messaging and created this service class in android

class MyFirebaseMessagingService : FirebaseMessagingService() {

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        val receiver = remoteMessage.data["receiver"]



        if(receiver.equals("Teacher"))
        {
            val applicationBasedPref =
                getSharedPreferences("Teacher", Context.MODE_PRIVATE)


            val intent = Intent(this, Teacher::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            intent.putExtra(
                "Name",
                applicationBasedPref.getString("TeacherName", null)
            )

            intent.putExtra("Id", applicationBasedPref.getString("TeacherId", null))
            intent.putExtra("isPushNotification",true)
            val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)



            val notification = NotificationCompat.Builder(this)
                .setContentTitle(remoteMessage.data["title"])
                .setContentText(remoteMessage.data["body"])
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .build()
            val manager = NotificationManagerCompat.from(applicationContext)

            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.POST_NOTIFICATIONS
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return
            }

            manager.notify(123, notification)


        }

        if(receiver.equals("Parent"))
        {
            val applicationBasedPref =
                getSharedPreferences("Parent", Context.MODE_PRIVATE)

            val intent = Intent(this, Teacher::class.java)

            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            intent.putExtra(
                "Name",
                applicationBasedPref.getString("ParentName", null)
            )
            intent.putExtra("isPushNotification",true)
            intent.putExtra("Id", applicationBasedPref.getString("ParentId", null))
            val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)



            val notification = NotificationCompat.Builder(this)
                .setContentTitle(remoteMessage.data["title"])
                .setContentText(remoteMessage.data["body"])
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .build()
            val manager = NotificationManagerCompat.from(applicationContext)

            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.POST_NOTIFICATIONS
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                return
            }

            manager.notify(123, notification)

        }



    }


}


The issue is when I'm sending notification then application is only receiving when it's closed but it's not receiving when it is running!

This is My Menifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AsaanAssessment"
        tools:targetApi="31">
        <activity
            android:name=".Teacher"
            android:exported="false" />
        <activity
            android:name=".Parent"
            android:exported="false" />
        <activity
            android:name=".Teacher_Log_In"
            android:exported="false" />
        <activity
            android:name=".Parent_Log_In"
            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".MyFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />


    </application>

</manifest>

I did try few suggestions but they did not work! I would really appreciate if someone guide me to resolve this. Thanks

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Does this answer your question? [Firebase Notification not working in background](https://stackoverflow.com/questions/44643866/firebase-notification-not-working-in-background) – Youssef Wagih May 12 '23 at 22:43

0 Answers0