How to fix notification not showing up when app is running?

Amine

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

Sam.

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Beacon Notification when the app is not running on Android

分類Dev

Espresso test for Notification to showing up

分類Dev

Show popup from push notification when app is running

分類Dev

While my app is running, how can I update the current activity with the contents a GCM notification, without clicking on the notification?

分類Dev

how top layout fix in screen when keyboard up in android

分類Dev

Intercept push notification from showing in app

分類Dev

How to open iOS app when Push Notification custom button clicked?

分類Dev

App not resuming when clicking notification

分類Dev

How to show up a notification if user haven't used the app for a while (1 week)?

分類Dev

How to fix Xiaomi specific RemoteServiceException with notification icon?

分類Dev

How to keep an IntentService running even when app is closed?

分類Dev

Progressbar not showing up when indeterminate (WPF)?

分類Dev

How to handle msg when the app is in background but the user doesn’t click the received notification

分類Dev

Notification Listener Service not started when running in debug mode

分類Dev

how to fix error "No task registered for key RNFirebaseBackgroundMessage" when my app is closed for getting fcm message

分類Dev

Loop through dummy data not showing up in Express app

分類Dev

facebook social plug-in not showing up when added dynamically

分類Dev

AlertDialog inside service not moving up when showing keyboard

分類Dev

Mongo::Error::NoServerAvailable when running Rails app

分類Dev

Disable screen lock when Cordova app is running

分類Dev

Encoding issue when running Gradle app distributions

分類Dev

Swift UI 2.0: How can i set up this Notification?

分類Dev

How to set build .env variables when running create-react-app build script?

分類Dev

How to authenticate programmatically when running an Apps Script Web App from another Apps Script project

分類Dev

How to delete androidTest & test folders for ever that not show up when creating new app

分類Dev

how to open particular fragment when notification is clicked

分類Dev

Open a specific view when opening the App through a Notification

分類Dev

Open a specific view when opening the App through a Notification

分類Dev

Flutter FCM Push Notification Not Working when app is in foreground

Related 関連記事

  1. 1

    Beacon Notification when the app is not running on Android

  2. 2

    Espresso test for Notification to showing up

  3. 3

    Show popup from push notification when app is running

  4. 4

    While my app is running, how can I update the current activity with the contents a GCM notification, without clicking on the notification?

  5. 5

    how top layout fix in screen when keyboard up in android

  6. 6

    Intercept push notification from showing in app

  7. 7

    How to open iOS app when Push Notification custom button clicked?

  8. 8

    App not resuming when clicking notification

  9. 9

    How to show up a notification if user haven't used the app for a while (1 week)?

  10. 10

    How to fix Xiaomi specific RemoteServiceException with notification icon?

  11. 11

    How to keep an IntentService running even when app is closed?

  12. 12

    Progressbar not showing up when indeterminate (WPF)?

  13. 13

    How to handle msg when the app is in background but the user doesn’t click the received notification

  14. 14

    Notification Listener Service not started when running in debug mode

  15. 15

    how to fix error "No task registered for key RNFirebaseBackgroundMessage" when my app is closed for getting fcm message

  16. 16

    Loop through dummy data not showing up in Express app

  17. 17

    facebook social plug-in not showing up when added dynamically

  18. 18

    AlertDialog inside service not moving up when showing keyboard

  19. 19

    Mongo::Error::NoServerAvailable when running Rails app

  20. 20

    Disable screen lock when Cordova app is running

  21. 21

    Encoding issue when running Gradle app distributions

  22. 22

    Swift UI 2.0: How can i set up this Notification?

  23. 23

    How to set build .env variables when running create-react-app build script?

  24. 24

    How to authenticate programmatically when running an Apps Script Web App from another Apps Script project

  25. 25

    How to delete androidTest & test folders for ever that not show up when creating new app

  26. 26

    how to open particular fragment when notification is clicked

  27. 27

    Open a specific view when opening the App through a Notification

  28. 28

    Open a specific view when opening the App through a Notification

  29. 29

    Flutter FCM Push Notification Not Working when app is in foreground

ホットタグ

アーカイブ