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");
}
}
在哪個Activity中你使用這個AsyncTask並給出正確的流程以獲得詳細信息,這樣可以正確地傳遞答案。否則,@Andrea Ebano給出的答案是正確的。 –