0

I am New to StackOverflow, So ignore any mistake of Mine.

In my Java Application, Firebase Push Notifications working Fine When the app is in Background or the App is Closed But Not Working When the App is in Foreground. my MyFirebaseMessagingService class code:

public class MyFirebaseMessagingService extends FirebaseMessagingService
{
    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"myFirebaseChannel")
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle(remoteMessage.getNotification().getTitle())
                .setContentText(remoteMessage.getNotification().getBody())
                .setAutoCancel(true);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(121,builder.build());
    }
}

Code for My MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FirebaseMessaging.getInstance().subscribeToTopic("Weather")
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task)
                    {
                        String msg = "Firebase Task completed";
                        if (!task.isSuccessful())
                        {
                            msg = "Firebase Task Failed";
                        }
                        Log.d("Firebase", msg);
                        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }
}

Manifest Code:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Firebase">
        <activity android:name=".MainActivity">
            <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>

    </application>

</manifest>

Whenever i push notification request when the app is in forground , Codetrace have following logs

W/xample.firebas: Accessing hidden method Landroid/os/WorkSource;->add(I)Z (greylist,test-api, reflection, allowed)
    Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (greylist,test-api, reflection, allowed)
W/xample.firebas: Accessing hidden method Landroid/os/WorkSource;->get(I)I (greylist, reflection, allowed)
    Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (greylist, reflection, allowed)
V/FA: Connecting to remote service
V/FA: Connection attempt already in progress
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 2
V/FA: Inactivity, disconnecting from the service
Elango
  • 406
  • 3
  • 11
  • Is notification not generating on foreground or click on notification is not working? – Ebad Ahmed Jun 21 '21 at 08:41
  • Notification not generating when the app is in foreground. – Nikhil Gupta Jun 21 '21 at 08:42
  • Receiving FCM notifications require the use of the internet, I worries to see in your manifest – Elango Jun 21 '21 at 08:45
  • @AjithkumarMuthukumaran But the notifications are working fine when the app is in background. And I guess , Internet permissions are added automatically by the dependencies of android manifest. – Nikhil Gupta Jun 21 '21 at 08:47
  • check this out, https://stackoverflow.com/a/38451582/12709358 hope it works! – Elango Jun 21 '21 at 09:00
  • Confirm your payload. check this out, https://stackoverflow.com/a/58542019/3363789 – Amol Nage Jun 21 '21 at 09:16
  • @NikhilGupta you need to manually create notification when app is in foreground you have created the notification ,try to put pending intent in it and put internet permission in manifest file for safe side. – Ebad Ahmed Jul 02 '21 at 07:01

0 Answers0