【问题标题】:InvalidConfigurationException: Identity pool isn't set up for SNS using AmazonCognitoSync serviceInvalidConfigurationException:未使用 AmazonCognitoSync 服务为 SNS 设置身份池
【发布时间】:2020-09-29 17:11:04
【问题描述】:

我一直在尝试将 AWS 开发工具包 用于推送通知。但是我遇到了错误。试图找到解决方案,但找不到太多支持。

iOS 和网络推送通知工作正常

所有已经设置和完成的事情:

  • AWS 后端和控制台设置到位。
  • 身份池 ID 和其他密钥到位。
  • ARN 主题已到位。

Android 端:

  • AWS 开发工具包依赖项:

    implementation 'com.amazonaws:aws-android-sdk-core:2.16.8'
    implementation 'com.amazonaws:aws-android-sdk-cognito:2.6.23'
    implementation 'com.amazonaws:aws-android-sdk-s3:2.15.1'
    implementation 'com.amazonaws:aws-android-sdk-ddb:2.2.0'
    implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.16.8') { transitive = true; }
    

minSdkVersion 21

targetSdkVersion 29

  • onCreate 内部:

    CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                    getApplicationContext(),
                    "My Pool Id here", // Identity pool ID
                    Regions.US_EAST_1 // Region
            );
    
    CognitoSyncManager client = new CognitoSyncManager(
                            LoginActivateActivity.this,
                            Regions.US_EAST_1,
                            credentialsProvider);
    
                String registrationId = "MY_FCM_DEVICE_TOKEN"; **Instead of GCM ID, I am passing my unique FCM device token here. I searched, & it seems that wherever GCM is required, it is being replaced by FCM.**
                try {
                    client.registerDevice("GCM", registrationId);
                } catch (RegistrationFailedException rfe) {
                    Log.e("TAG", "Failed to register device for silent sync", rfe);
                } catch (AmazonClientException ace) {
                    Log.e("TAG", "An unknown error caused registration for silent sync to fail", ace);
                }
    
                Dataset trackedDataset = client.openOrCreateDataset("My Topic here");
                if (client.isDeviceRegistered()) {
                    try {
                        trackedDataset.subscribe();
                    } catch (SubscribeFailedException sfe) {
                        Log.e("TAG", "Failed to subscribe to datasets", sfe);
                    } catch (AmazonClientException ace) {
                        Log.e("TAG", "An unknown error caused the subscription to fail", ace);
                    }
                }
    

我在 client.registerDevice("GCM", registrationId) 上遇到错误;

原因:com.amazonaws.services.cognitosync.model.InvalidConfigurationException:未为 SNS 设置身份池(服务:AmazonCognitoSync;状态代码:400;错误代码:InvalidConfigurationException;请求 ID:a858aaa2-*** ***********************)

注意:

我尝试使用 Amplify 库,但即使这样也没有用。此外,在 iOS 和 Web 端,他们使用 AWS SDK。所以我也一定会使用相同的。这甚至不是特定于设备的错误。

我需要做的就是设置我的项目以获取推送通知。但我被困在最初的一步。无法为 Android 设备创建端点。

【问题讨论】:

    标签: android push-notification aws-sdk android-notifications aws-sdk-android


    【解决方案1】:

    我实际上找到了问题的解决方案,感谢分享此链接的朋友:

    https://aws.amazon.com/premiumsupport/knowledge-center/create-android-push-messaging-sns/

    这个 Youtube 视频也有很大帮助:

    https://www.youtube.com/watch?v=9QSO3ghSUNk&list=WL&index=3

    修改后的代码

    private void registerWithSNS() {
    
            CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                    getApplicationContext(),
                    "Your Identity Pool ID",
                    Regions.US_EAST_1 // Region
            );
    
            client = new AmazonSNSClient(credentialsProvider);
    
            String endpointArn = retrieveEndpointArn();
            String token = "Your FCM Registration ID generated for the device";
    
            boolean updateNeeded = false;
            boolean createNeeded = (null == endpointArn || "".equalsIgnoreCase(endpointArn));
    
            if (createNeeded) {
                // No platform endpoint ARN is stored; need to call createEndpoint.
                endpointArn = createEndpoint(token);
                createNeeded = false;
            }
    
            System.out.println("Retrieving platform endpoint data...");
            // Look up the platform endpoint and make sure the data in it is current, even if
            // it was just created.
            try {
                GetEndpointAttributesRequest geaReq =
                        new GetEndpointAttributesRequest()
                                .withEndpointArn(endpointArn);
                GetEndpointAttributesResult geaRes =
                        client.getEndpointAttributes(geaReq);
    
                updateNeeded = !geaRes.getAttributes().get("Token").equals(token)
                        || !geaRes.getAttributes().get("Enabled").equalsIgnoreCase("true");
    
            } catch (NotFoundException nfe) {
                // We had a stored ARN, but the platform endpoint associated with it
                // disappeared. Recreate it.
                createNeeded = true;
            } catch (AmazonClientException e) {
                createNeeded = true;
            }
    
            if (createNeeded) {
                createEndpoint(token);
            }
    
            System.out.println("updateNeeded = " + updateNeeded);
    
            if (updateNeeded) {
                // The platform endpoint is out of sync with the current data;
                // update the token and enable it.
                System.out.println("Updating platform endpoint " + endpointArn);
                Map attribs = new HashMap();
                attribs.put("Token", token);
                attribs.put("Enabled", "true");
                SetEndpointAttributesRequest saeReq =
                        new SetEndpointAttributesRequest()
                                .withEndpointArn(endpointArn)
                                .withAttributes(attribs);
                client.setEndpointAttributes(saeReq);
            }
        }
    
        /**
         * @return never null
         * */
        private String createEndpoint(String token) {
    
            String endpointArn = null;
            try {
                System.out.println("Creating platform endpoint with token " + token);
                CreatePlatformEndpointRequest cpeReq =
                        new CreatePlatformEndpointRequest()
                                .withPlatformApplicationArn("Your Platform ARN. This you get from AWS Console. Unique for all devices for a platform.")
                                .withToken(token);
                CreatePlatformEndpointResult cpeRes = client
                        .createPlatformEndpoint(cpeReq);
                endpointArn = cpeRes.getEndpointArn();
            } catch (InvalidParameterException ipe) {
                String message = ipe.getErrorMessage();
                System.out.println("Exception message: " + message);
                Pattern p = Pattern
                        .compile(".*Endpoint (arn:aws:sns[^ ]+) already exists " +
                                "with the same [Tt]oken.*");
                Matcher m = p.matcher(message);
                if (m.matches()) {
                    // The platform endpoint already exists for this token, but with
                    // additional custom data that
                    // createEndpoint doesn't want to overwrite. Just use the
                    // existing platform endpoint.
                    endpointArn = m.group(1);
                } else {
                    // Rethrow the exception, the input is actually bad.
                    throw ipe;
                }
            }
            storeEndpointArn(endpointArn);
            return endpointArn;
        }
    
        /**
         * @return the ARN the app was registered under previously, or null if no
         *         platform endpoint ARN is stored.
         */
        private String retrieveEndpointArn() {
            // Retrieve the platform endpoint ARN from permanent storage,
            // or return null if null is stored.
            return endpointArn;
        }
    
        /**
         * Stores the platform endpoint ARN in permanent storage for lookup next time.
         * */
        private void storeEndpointArn(String endpointArn) {
            // Write the platform endpoint ARN to permanent storage.
            UserSession.getSession(LoginActivateActivity.this).setARN(endpointArn); //Your platform endpoint ARN. This is unique for each device, but changes when 
        }
    

    为设备创建端点后,您需要将端点Arn 和 FCM 注册 ID 存储到服务器端的数据库中。其余代码将是您用于接收通知的 FCM 实现代码。

    希望这对某人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 2022-01-10
      • 2018-12-20
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多