2017-06-08 3 views
0

J'ai suivi un didacticiel sur la configuration de la notification SNS Push, mais l'objet CreatePlatformEndpointResult renvoie null. J'ai besoin de cela pour que je puisse récupérer l'endpointArn et l'envoyer au backend. Voici ma configuration complète. Et voici le lien vers le tutoriel: http://www.allcode.com/amazon-sns-push-notification-tutorial-android-using-gcm/Comment faire une notification push SNS? CreatePlatformEndpointResult renvoie la valeur null

D'abord, je récupérer un jeton de GCM en appelant

AWSManager.registerAppToGCM (getApplicationContext())

Ceci est de ma classe AWSManager

public class AWSManager { 
    private static final String TAG = AWSManager.class.getSimpleName(); 
    private static final String SNS_ACCESS_KEY_ID = "1234567890"; // I have swapped out the real key 
    private static final String SNS_SECRET_KEY = "1234567890"; // I have swapped out the real key 

    private static AmazonSNSClient snsClient; 

    /** 
    * Method is used to retrieve SNSClient object 
    * 
    * @return snsClient object 
    */ 
    public static AmazonSNSClient getSNSClient() { 
     if (FrameworkUtils.checkIfNull(snsClient)) { 
      snsClient = new AmazonSNSClient(new BasicAWSCredentials(SNS_ACCESS_KEY_ID, SNS_SECRET_KEY)); 
      snsClient.setRegion(Region.getRegion(Regions.US_WEST_1)); 
     } 
     return snsClient; 
    } 

    /** 
    * Method is used to register app to GCM 
    * 
    * @param context 
    */ 
    public static void registerAppToGCM(Context context) { 
     SharedPref sharedPref = new SharedPref(context, Constants.PREF_FILE_NAME); 
     String gcmToken = sharedPref.getStringPref(Constants.NOTIFICATION_GCM_TOKEN, ""); 
     if (FrameworkUtils.isStringEmpty(gcmToken)) { 
      new GCMRegisterTask(context).execute(); 
     } 
    } 
} 

Voici la classe exécutant la tâche d'arrière-plan

public class GCMRegisterTask extends AsyncTask<String, Void, Boolean> { 
    private static final String TAG = GCMRegisterTask.class.getSimpleName(); 

    private Context mContext; 

    /** 
    * Constructor 
    * 
    * @param context 
    */ 
    public GCMRegisterTask(Context context) { 
     super(); 
     mContext = context; 
    } 

    @Override 
    protected Boolean doInBackground(String... params) { 
     String token; 
     try { 
      token = InstanceID.getInstance(mContext).getToken(mContext.getString(R.string.gcm_project_id), GoogleCloudMessaging.INSTANCE_ID_SCOPE); 
      SharedPref sharedPref = new SharedPref(mContext, Constants.PREF_FILE_NAME); 
      sharedPref.setPref(Constants.NOTIFICATION_GCM_TOKEN, token); 
      Logger.i(TAG, "GCM token successfully stored to prefs: " + token); 
      return true; 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return false; 
    } 
} 

Une fois que j'ai réussi à récupérer le jeton GCM. J'utilise nouvelle

AWSCreateEndpointTask (mContext) .Execute (test, jeton, "[email protected]")

pour commencer le processus de création du point de terminaison ARN. test = "arn: aws: sns: région: nous-est-1: app/GCM/APPNAME"

public class AWSCreateEndpointTask extends AsyncTask<String, Void, CreatePlatformEndpointResult> { 

    Context mContext; 

    /** 
    * Constructor 
    * 
    * @param context 
    */ 
    public AWSCreateEndpointTask(Context context) { 
     super(); 
     mContext = context; 
    } 

    @Override 
    protected CreatePlatformEndpointResult doInBackground(String[] params) { 
     if (params.length < 3) { 
      return null; 
     } 

     String arn = params[0]; 
     String gcmToken = params[1]; 
     String userData = params[2]; 

     try { 
      CreatePlatformEndpointRequest request = new CreatePlatformEndpointRequest(); 
      request.setCustomUserData(userData); 
      request.setToken(gcmToken); 
      request.setPlatformApplicationArn(arn); 
      return AWSManager.getSNSClient().createPlatformEndpoint(request); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    @Override 
    protected void onPostExecute(CreatePlatformEndpointResult result) { 
     if (!FrameworkUtils.checkIfNull(result)) { 
      String endpointArn = result.getEndpointArn(); 
      SharedPref sharedPref = new SharedPref(mContext, Constants.PREF_FILE_NAME); 
      sharedPref.setPref(Constants.NOTIFICATION_ENDPOINT_ARN, endpointArn); 
     } 
    } 

A l'intérieur de onPostExecute, l'objet CreatePlatformEndpointResult retournée est nulle. Qu'est-ce que je manque ou fais incorrectement pour causer ceci?

Répondre

0

Il s'avère que l'implémentation est correcte, la valeur arn était erronée. Pour les autres utilisateurs de ce conflit, assurez-vous que l'information est correcte lorsque vous essayez d'obtenir l'endpointARN. Je mis à jour à la mine

arn: aws: sns: nous-est-1 :: app/GCM/

Cela vient de la console développeur.