Ad

How To Fix Notification Not Showing Up When App Is Running?

I made a test app that has Firebase implemented, i'm using the notifications composer from Firebase, it works okay but not as intended since the notification only shows up when the app isn't active (on the screen) and i need it to show up even when the app is active and running

MainActivity.java

package com.gci.gestioncapteursincendie;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= findViewById(R.id.textViewToken);
        FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                      if(task.isSuccessful()){
                          String token = task.getResult().getToken();
                          System.out.println("token:"+token);
                          textView.setText("Token : " + token);
                        }
                        else
                      {
                          textView.setText("Token Not Generated");
                      }
                    }
                });
    }

}

AndroidManifest.xml

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

    <!-- Granting Internet access permission to app -->
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name="com.gci.gestioncapteursincendie.FirebaseActivity"
        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/AppTheme">
        <service
            android:name=".FirebaseMessagingServiceExtended"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Edit: added manifest

Ad

Answer

When the app is closed, your notifications are processed by the Google Service process, which take care of displaying your notifications as required, including the default click action (opening the app) and the notification icon.

When the app is in foreground, the received messages are processed by the app, and since there’s no logic to handle it, nothing will happen!

You can create a custom FirebaseMessagingService which implement the onMessageReceivedand then create the notification in it..

public class NotificationService extends FirebaseMessagingService { 
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Notification notification = new NotificationCompat.Builder(this)
     .setContentTitle(remoteMessage.getNotification().getTitle())
     .setContentText(remoteMessage.getNotification().getBody())
     .setSmallIcon(R.mipmap.ic_launcher)
     .build();

    NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
    manager.notify(some_int_number (eg : 123), notification);
   }
 }

And add this to your manifest file

<service android:name=”.NotificationService”>
  <intent-filter>
    <action android:name=”com.google.firebase.MESSAGING_EVENT”/>
  </intent-filter>
</service>

Also make sure you give proper permission in manifest file. After implementing try again, you should see the notification while your app is in foreground.

Ad
source: stackoverflow.com
Ad