InvalidConfigurationException:未使用 AmazonCognitoSync 服务为 SNS 设置身份池

InvalidConfigurationException: Identity pool isn't set up for SNS using AmazonCognitoSync service

我一直在尝试使用 AWS SDK 进行推送通知。但我收到错误。试图找到解决方案,但找不到太多支持。

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

已经设置并完成的内容:

Android 边:

minSdkVersion 21

targetSdkVersion 29

我在 client.registerDevice("GCM", registrationId);

上出错

Caused by: com.amazonaws.services.cognitosync.model.InvalidConfigurationException: Identity pool isn't set up for SNS (Service: AmazonCognitoSync; Status Code: 400; Error Code: InvalidConfigurationException; Request ID: a858aaa2-**************************)

注:

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

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

我实际上找到了问题的解决方案,感谢分享这个的朋友link:

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 存储到 server-side 上的数据库中。其余代码将是您接收通知的 FCM 实现代码。

希望这对某人有所帮助