skip to Main Content

I am new to android and I’m having this problem
if the app is in the foreground the notification shows normally
if I put the app in the background the notification will show and it works normally also
BUT the problem is -> when I close the application(the app is killed) the notification does not show <-

solutions I tried

  • only send notification
  • only send data
  • not putting a custom view for the notification

what I understand is if I send only notification without data it will show when the app is in the foreground AKA when it's open and you can see it and
if I only send data without notification it will show in the background and when the app is killed and the data will be found in the main activity intent (but clearly this is NOT the case for me)
I am using an android 10 Infinix Note 5 stylus just in case there is something with this model
but I still can see notifications from any other app normally

edit
so I tried to run the app on other phones and it works as it supposed to but on my phone all the notifications that is sent when the app is closed will only show when I open the app

I close the app -> send some notifications -> no thing on the phone -> 
open the app again -> all the notifications I sent are are now there

it still works fine when i send the notifications when the app is on foreground and when i put it in the backgorund

code:


    public class FirebasePushNotificationConfig extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        sendNotification(remoteMessage);
    }  

    private void sendNotification(RemoteMessage remoteMessage) {

         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {  
            NotificationChannel channel = new NotificationChannel(AppStrings.MSG.name(), "Testing", NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableVibration(false);
            channel.enableLights(true);
            manager.createNotificationChannel(channel);
        }
        PendingIntent pendingIntent = getPendingIntent(remoteMessage);
     

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, AppStrings.MSG.name())
                .setSmallIcon(R.drawable.ic_notifications_black_24dp)
                .setVibrate(null)
                .setContentTitle(remoteMessage.getData().get(AppStrings.MSG.name()))
                .setContentText(remoteMessage.getData().get(AppStrings.MSG.name()))
                .setAutoCancel(true) 
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent);  
        Notification n = mBuilder.build();
        manager.notify(1, n);}


    private PendingIntent getPendingIntent(RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, LoginActivity.class);  
        intent.putExtra(AppStrings.Notifications_Data.name(), remoteMessage.getData().get(AppStrings.Notifications_Data.name()));  
        intent.putExtra(AppStrings.Is_There_Notification.name(), AppStrings.YES.name());  
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(AppStrings.Notifications_Destination.name(), remoteMessage.getData().get(AppStrings.Notifications_Destination.name()));  
        return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    }

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);
        System.out.println("log Token is " + token);
        Log.d("Test", "token: " + token);
    }}

manifest

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.SET_ALARM " />
    <uses-permission android:name="android.permission.READ_PHONE_STATE " />
    <uses-permission android:name="android.permission.READ_PHONE_NUMBERS " />
    <uses-permission android:name="android.permission.READ_SMS " />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />  
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application
        android:name=".Config.App"
        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"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".LocationActivity"></activity>
        <activity android:name=".MapsActivity" />
        <!--        <activity android:name=".ProfileActivity" />-->
        <activity android:name=".ChatManagerActivity" />
        <activity android:name=".RegisterActivity" />
        <activity android:name=".MainActivity" />
        <activity android:name=".AuthActivity" />
        <activity android:name=".ChatActivity" />
        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name=".LoginActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".broadcastRecivers.AlertReciver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <receiver
            android:name=".broadcastRecivers.LoginReciver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service
            android:name=".Config.FirebasePushNotificationConfig"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="key" />

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.test.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    </application>

</manifest>`

the data I send looks like this

{"to":"key"
,"notification":{
"title":"hello it's test"
,"body":"still a test"

},
"data": {
"Notifications_Destination" : "direction"
,"MSG":"this is a test notifiation",
"Notifications_Data":5
}}

2

Answers


  1. create FcmHelper class with extends FirebaseMessagingService.

    <!--Fcm Starts-->
    
            <service
                android:name="google_classes.FcmHelper"
                android:exported="false">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service>
    
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_icon"
                android:resource="@drawable/ic_noti" />
    
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_color"
                android:resource="@color/black" />
    
            <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
                android:value="@string/default_notification_channel_id" />
    
            <meta-data
               android:name="firebase_messaging_auto_init_enabled"
               android:value="true" />
    
            <!--Fcm End-->
    
    Login or Signup to reply.
  2. Try this:

        <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    

    and under application tag add:

    <service android:name=".FirebaseService"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
    
        </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>
    

    Change class names as per requirements.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search