Skip to content

Commit

Permalink
Add Android helper function to get 'google_app_id' parameter.
Browse files Browse the repository at this point in the history
- This is pretty ugly code. I welcome refactoring from someone more familiar with Android best practices.
- Closest StackOverflow I found was http://stackoverflow.com/questions/24453018/plugin-preferences-in-config-xml
  • Loading branch information
Christopher Prescott committed Mar 16, 2016
1 parent 9ab285b commit b1b5e9d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ public interface PushConstants {
public static final String GCM = "GCM";
public static final String CONTENT_AVAILABLE = "content-available";
public static final String TOPICS = "topics";
public static final String GOOGLE_APP_ID = "google_app_id";
}
23 changes: 23 additions & 0 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ private Context getApplicationContext() {
return this.cordova.getActivity().getApplicationContext();
}

/**
* Retrieve the senderId we added to AndroidManifest.xml when we installed.
*
* @return senderId String
* @throws NameNotFoundException
*/
private String getSenderIdFromApplicationManifest() throws NameNotFoundException {
// Ugly boilerplate to get the metaData from the AndroidManifest. Refactoring welcome!
Context context = getApplicationContext();
ApplicationInfo app = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = app.metaData;

// We cannot getString() because ID is a number. We cannot getInt() because the senderId
// has 13 digits & the max length of Android int is 10 digits. We must therefore get a float.
// See similar StackOverflow if you don't believe me
// http://stackoverflow.com/questions/22210404/android-metadata-from-manifest-returning-null
Float senderIDAsFloatObj = bundle.getFloat(GOOGLE_APP_ID);

// There is no getLong(). We must do the convoluted & somewhat unsafe conversion.
// http://www.tutorialspoint.com/java/lang/float_longvalue.htm
return Long.toString(senderIDAsFloatObj.longValue());
}

@Override
public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) {
Log.v(LOG_TAG, "execute: action=" + action);
Expand Down

0 comments on commit b1b5e9d

Please sign in to comment.