Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在从FirebaseMessagingService扩展的类中找不到getApplicationContext()_Android_Firebase_Firebase Cloud Messaging_Android Service - Fatal编程技术网

Android 在从FirebaseMessagingService扩展的类中找不到getApplicationContext()

Android 在从FirebaseMessagingService扩展的类中找不到getApplicationContext(),android,firebase,firebase-cloud-messaging,android-service,Android,Firebase,Firebase Cloud Messaging,Android Service,我想通过Firebase发送和接收远程通知,但我的自定义类找不到getApplicationContext()方法 据我所知,FirebaseMessagingService扩展了服务,它扩展了ContextWrapper`它扩展了上下文 所以我不知道getApplicationContext()不起作用。任何帮助都将不胜感激 import com.google.firebase.messaging.FirebaseMessagingService; import com.google.fire

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

据我所知,FirebaseMessagingService扩展了服务
,它扩展了
ContextWrapper`它扩展了上下文

所以我不知道
getApplicationContext()
不起作用。任何帮助都将不胜感激

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文件

<?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的应用程序类,并在其create方法上创建一个上下文的弱引用变量,并将其分配给getApplicationContext(),您可以在任何地方使用此上下文

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();
        }
    }
公共类应用程序扩展了android.app.Application{
私有静态WeakReference referencectx;
@凌驾
public void onCreate(){
super.onCreate();
referenceCntx=新的WeakReference(getApplicationContext());
}
公共静态上下文getApplicationCntx(){
返回referenceCntx.get();
}
}
别忘了在清单中添加这一行

<application
        android:name=".app"

请不要使用代码的图像。最好将代码复制到问题中,这样更易于阅读和搜索,从而成为问题的永久部分。您是否在AndroidManifest中声明了服务?也许这有助于检查上面的AndroidManifest文件,我想我添加的是正确的。正如您指出的,
FirebaseMessagingService
派生自
上下文。为什么不直接使用
this
而不是
getApplicationContext()
?。也尝试了这个方法,但都不起作用。我遇到的问题是,如果我创建了另一个类并创建了一个名为context的变量,然后将其分配给getApplicationContext(),那么该变量必须是非静态的,才能分配给getApplicationContext()。但是,在onMessageReceived方法中,无法从静态上下文引用I get
非静态字段。我如何解决这一冲突?非常感谢。