在从FirebaseMessagingService扩展的类中找不到getApplicationContext() [英] Cannot find getApplicationContext() in class extended from FirebaseMessagingService

查看:72
本文介绍了在从FirebaseMessagingService扩展的类中找不到getApplicationContext()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Firebase发送和接收远程通知,但是我的自定义类找不到getApplicationContext()方法.

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

据我了解,FirebaseMessagingService扩展了Service , which extends ContextWrapper`,后者扩展了Context.

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

所以我不知道getApplicationContext()无法正常工作.任何帮助将不胜感激!

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);
    }
}

这是我的AndroidManifest.xml文件.

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>

推荐答案

您可以通过多种方式实现此目的,一种方法是保持上下文的弱引用,使应用程序类扩展android.app.application并在其上创建方法并创建一个上下文弱引用的变量并将其分配给getApplicationContext(),您可以在所需的任何位置使用此上下文.

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"

这篇关于在从FirebaseMessagingService扩展的类中找不到getApplicationContext()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆