我试图将我的设备令牌从我的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");
}
}
答案 0 :(得分:0)
在异步类的构造函数中,传递Context
private Context mContext;
public RegisterIdForAWS (Context context){
mContext = context;
}
并实例化这样的类:
RegisterIdForAWS task = new RegisterIdForAWS (context);
希望它有所帮助。