2013-11-05 100 views
9

我使用Amazon Web Service直接向設備發送推送通知。安裝應用程序後,我得到設備ID,我需要手動添加到Amazon SNS。我想知道在用戶啓動應用程序的那一刻是否有註冊設備ID直接與亞馬遜服務器。我看過this,但發現很難理解。有沒有人有任何以前的經驗如何做到這一點?直接向亞馬遜SNS註冊設備ID

EDIT 2(我迄今所做的)

我已經按照從this link

  1. 說明我下載snspobilepush.zip文件的指示並提取並導入項目進入日食。我添加GCM項目編號,添加jar文件並運行應用程序。我拿到我的設備註冊ID。

  2. 我打開Amazon SNS,添加我的設備ID併發布消息。我在手機上收到消息。迄今爲止效果很好。

    我的問題

我會爲我的應用程序有很多潛在用戶。因此,將每個設備ID手動添加到SNS是沒有意義的。我需要Amazon SNS在我啓動應用程序時直接註冊我的設備ID。我有什麼選擇嗎?我無法在文檔中找到任何明確的答案。
This link告訴我使用「AWS令牌售貨服務」。但是,我找不到如何做到這一點的任何示例。

回答

1

可以使用Android AWS SDK。檢查出的文檔鏈接:可用http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/

此外,越來越多的信息:http://aws.amazon.com/sdkforandroid/

+3

我已經通過這兩個鏈接。我覺得我正在圍繞着圈子走。說明書記錄很差。 – Anirudh

+0

您可以分享一下您迄今嘗試過的代碼。你得到的錯誤是什麼? – doNotCheckMyBlog

+0

再次您好!代碼工作正常,我沒有得到任何錯誤。我只需要直接在亞馬遜服務器上註冊設備ID,我無法做到(而不是手動添加它)。我編輯了我的問題來包含這一點。請看看。謝謝! – Anirudh

10

使用AmazonSNSClient這裏記載:

http://docs.aws.amazon.com/AWSAndroidSDK/latest/javadoc/

應該可以使用類似的代碼來此登記:

AWSCredentials awsCredentials = new BasicAWSCredentials("XXXXXX", XXXXXXXXXXXXXXX"); 
String platformApplicationArn = "arn:aws:sns:us-east-1:123456789:app/GCM/myappname"; 
AmazonSNSClient pushClient = new AmazonSNSClient(awsCredentials); 

String customPushData = "my custom data"; 
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); 

它不喜歡我的ARN,但那是一個愚蠢的裏德指出的錯字現在已在上面修正。

+0

我來自PH,雖然我的互聯網可能在澳大利亞路由(辦公室設置,不要問),但我不明白爲什麼這隻適用於我在美國東部創建我的SNS應用程序。可能會爲此提出一個不同的問題,但由於這個問題有效,它應該是被接受的答案。 – josephus

+0

我用我自己的'platformApplicationArn'替換了我的。 – josephus

+0

對於其他使用這種方法運行到區域錯誤的人,我的解決方案是手動設置SNSClient上的區域。 'pushClient.setRegion(Region.getRegion(Regions.US_WEST_2));' –

0

這適用於Firebase和Cognito。一個AsyncTask是必要的,以避免在主線程上運行。

private class RegisterIdForAWS extends AsyncTask<String, Void, Void> { 

     private Exception exception; 

     protected Void doInBackground(String... urls) { 
      try { 
       String pushNotificationRegId = FirebaseInstanceId.getInstance().getToken(); 

       if (pushNotificationRegId != null) { 

        CognitoCachingCredentialsProvider provider = new CognitoCachingCredentialsProvider(
          getApplicationContext(), 
          "us-west-2:aaaaaaaaa-1234-1234-1234-0bbbbbbbbbbbb", 
          Regions.US_WEST_2); 
        String platformApplicationArn = "arn:aws:sns:us-west-2:123456789:app/GCM/appname"; 
        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"); 
     } 
    }