0

I want to send and receive remote notification through Firebase but my custom class could not find getApplicationContext() method.

From my understanding, FirebaseMessagingService extends Service, which extendsContextWrapper` which extends Context.

So I don't know getApplicationContext() is not working. Any help will be appreciated!

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class FirebaseMessageService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if(remoteMessage.getNotification() != null){
            String title = remoteMessage.getNotification().getTitle();
            String text = remoteMessage.getNotification().getBody();

            NotificationHelper.displayNotification(getApplicationContext(), title, text);
        }
        super.onMessageReceived(remoteMessage);
    }
}

Here's my AndroidManifest.xml file.

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

    <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/AppTheme.NoActionBar">
        <activity android:name=".Activities.UserActivity"></activity>
        <activity android:name=".Activities.LoginActivity" />
        <activity android:name=".Activities.SignupActivity" />
        <activity android:name=".Activities.SearchRecipeActivity" />
        <activity android:name=".Activities.RecipeDetailActivity" />
        <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=".Notifications.FirebaseMessageService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
             </intent-filter>
        </service>
    </application>
</manifest>
UziHU
  • 13
  • 1
  • 4
  • 3
    Please do not use images of code. It's better to copy the code into the question so it's easier to read and search, and so it becomes a permanent part of the question. – Doug Stevenson Nov 15 '19 at 15:39
  • Do you have the service declared in in AndroidManifest? Maybe this helps https://stackoverflow.com/questions/20676233/getapplicationcontext-returns-null-in-a-service – Dmitri Borohhov Nov 15 '19 at 17:01
  • Please check the AndroidManifest file above, I think I added it correctly. – UziHU Nov 15 '19 at 18:50
  • As you pointed out, `FirebaseMessagingService` derivates from `Context`. Why not just use `this` instead of `getApplicationContext()`?. – yugidroid Nov 15 '19 at 19:10
  • Also tried this, not working either. – UziHU Nov 16 '19 at 01:36

1 Answers1

1

You can achieve this in many way one way is to keep a weak refrence of context make a app class extending android.app.application and on it's create method and make a variable of weak refrence of context and assign it to getApplicationContext() and you may use this context anywhere you want.

public class app extends android.app.Application{

        private static WeakReference<Context> referenceCntx;

        @Override
        public void onCreate() {
            super.onCreate();

            referenceCntx = new WeakReference<>(getApplicationContext());
        }

        public static Context getApplicationCntx(){
            return referenceCntx.get();
        }
    }

And don't forget to add this line in manifest

<application
        android:name=".app"
Pratyush
  • 401
  • 3
  • 11
  • The problem I'm having is if I make another class and create a variable called context and then assign it to getApplicationContext(), the variable needs to be non-static to be able to assigned to getApplicationContext(). However, I get `Non-static field cannot be referenced from a static context` error in onMessageReceived method. How can I resolve this conflict? Thank you! – UziHU Nov 15 '19 at 18:54