2017-07-27 66 views
0

我试图以编程方式将我的设备令牌从我的Android应用程序存储到AWS SNS平台应用程序。getApplicationContext错误

我在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"); 
    } 


} 
+0

在哪个Activity中你使用这个AsyncTask并给出正确的流程以获得详细信息,这样可以正确地传递答案。否则,@Andrea Ebano给出的答案是正确的。 –

回答

0

在你的异步类的构造函数,通过上下文

private Context mContext; 

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

和实例类是这样的:

RegisterIdForAWS task = new RegisterIdForAWS (context); 

希望它能帮助。

相关问题