getApplicationContext 错误

getApplicationContext error

我正在尝试以编程方式将我的设备令牌从我的 android 应用程序存储到 AWS SNS 平台应用程序。

我在 getApplicationContext() 方法中遇到错误。有人对此错误有解决方案吗?

这是我的代码:

public class RegisterIdForAWS extends AsyncTask<String, Void, Void> {
    private Exception exception;

    @Override
    protected Void doInBackground(String... strings) {
        try {
            String pushNotificationRegId = FirebaseInstanceId.getInstance().getToken();

            if (pushNotificationRegId != null) {

                CognitoCachingCredentialsProvider provider = new CognitoCachingCredentialsProvider(
                       getApplicationContext(),
                        "us-west-2:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                        Regions.US_WEST_2);
                String platformApplicationArn = "arn:aws:sns:us-west-2:11111111111:app/GCM/123";
                AmazonSNSClient pushClient = new AmazonSNSClient(provider);
                pushClient.setRegion(Region.getRegion(Regions.US_WEST_2));

                String customPushData = "";
                CreatePlatformEndpointRequest platformEndpointRequest = new CreatePlatformEndpointRequest();
                platformEndpointRequest.setCustomUserData(customPushData);
                platformEndpointRequest.setToken(pushNotificationRegId);
                platformEndpointRequest.setPlatformApplicationArn(platformApplicationArn);
                CreatePlatformEndpointResult result = pushClient.createPlatformEndpoint(platformEndpointRequest);
                Log.w(TAG, "Amazon Push reg result: " + result);
            }
        } catch (Exception e) {
            this.exception = e;
        }

        return null;
    }

    protected void onPostExecute(String text) {
        Log.w(TAG, "Amazon Push reg Finished");
    }


}

在异步 class 的构造函数中,传递 Context

private Context mContext;

public RegisterIdForAWS (Context context){
     mContext = context;
}

并像这样实例化 class:

RegisterIdForAWS task = new RegisterIdForAWS (context);

希望对您有所帮助。