Ido Naveh Ido Naveh - 1 month ago 26
Android Question

Error with FCM + SNS Token on Android app

I've followed AWS Mobile Hub push integration guide, and integrated AWS SNS push services in my app. When I open the app I get this error log:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.intap.appme, PID: 23576
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.intap.name/com.intap.name.MainActivity}: com.amazonaws.AmazonServiceException: 1 validation error detected: Value null at 'token' failed to satisfy constraint: Member must not be null (Service: AmazonSNS; Status Code: 400; Error Code: ValidationError; Request ID: 21d6a3b2-0459-513a-bf7a-f3c1d99d41ac)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: com.amazonaws.AmazonServiceException: 1 validation error detected: Value null at 'token' failed to satisfy constraint: Member must not be null (Service: AmazonSNS; Status Code: 400; Error Code: ValidationError; Request ID: 21d6a3b2-0459-513a-bf7a-f3c1d99d41ac)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:712)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:388)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199)
at com.amazonaws.services.sns.AmazonSNSClient.invoke(AmazonSNSClient.java:2262)
at com.amazonaws.services.sns.AmazonSNSClient.createPlatformEndpoint(AmazonSNSClient.java:447)
at com.amazonaws.mobile.push.PushManager.subscribeToTopic(PushManager.java:264)
at com.intap.name.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 


The code lines mentioned in the app are those lines:


PushManager.java:264 (The whole method)

public void subscribeToTopic(final SnsTopic topic) {
final CreatePlatformEndpointRequest endpointRequest = new CreatePlatformEndpointRequest();
endpointRequest.setPlatformApplicationArn(platformApplicationArn);
try {
endpointRequest.setToken(InstanceID.getInstance(context).getToken(sharedPreferences.getString("deviceToken", ""), GoogleCloudMessaging.INSTANCE_ID_SCOPE));
} catch (IOException e) {
Log.e("Error", e.getMessage());
}
/* This is line 264 -> */ final CreatePlatformEndpointResult endpointResult = sns.createPlatformEndpoint(endpointRequest);

final SubscribeRequest request = new SubscribeRequest();
request.setEndpoint(endpointResult.getEndpointArn());
request.setTopicArn(topic.getTopicArn());
request.setProtocol(SNS_PROTOCOL_APPLICATION);
final SubscribeResult result = sns.subscribe(request);

// update topic and save subscription in shared preferences
final String subscriptionArn = result.getSubscriptionArn();
topic.setSubscriptionArn(subscriptionArn);
sharedPreferences.edit().putString(topic.getTopicArn(), subscriptionArn).apply();
}


MainActivity.java:50

pushManager.subscribeToTopic(pushManager.getDefaultTopic());


When I'm trying to send a push message through the online Firebase console, The device gets the push messages, and when I click on the message to open the app it keeps crashing.

When I'm trying to send a push message through the online SNS console, I don't get any push notfiications, which means the error is in the registration to SNS.

How can I solve it? I have no idea about it...

Answer

The token is coming back as null from Google's SDK. The reason is due to this line:

endpointRequest.setToken(InstanceID.getInstance(context)
    .getToken(sharedPreferences.getString("deviceToken", ""),
       GoogleCloudMessaging.INSTANCE_ID_SCOPE));

The first parameter to getToken() should be the GCM Sender ID rather than the device token that is being passed as pulled from sharedPreferences. Changing your code to pass the sender ID as follows, should fix the issue:

endpointRequest.setToken(InstanceID.getInstance(context)
    .getToken(gcmSenderID, GoogleCloudMessaging.INSTANCE_ID_SCOPE));

Also, it appears that the code you pasted was modified from a Mobile Hub project that was generated in the fairly distant past. There have been improvements in the PushManager since then. The latest versions generated by the Mobile Hub would use the GCMTokenHelper class, which is not present in the code you pasted. Please try generating the sample app or SDK using the Mobile Hub again and updating your app to use the more recently generated code and report back if you are still experiencing issues.

Comments