Skip to content

Commit

Permalink
Fix phonegap#689 use the senderID from installation as Android default.
Browse files Browse the repository at this point in the history
- Prefer explicitly defined SENDER_ID from the plugin's JS.
- I believe this resolves  without breaking existing compatibility.
- Add Android helper function to get 'google_app_id' parameter. This is pretty ugly code. I welcome refactoring from someone more familiar with Android best practices. The closest StackOverflow I found was http://stackoverflow.com/questions/24453018/plugin-preferences-in-config-xml
  • Loading branch information
Christopher Prescott committed Mar 22, 2016
1 parent f43f1af commit c6d5193
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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";
}
32 changes: 31 additions & 1 deletion src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;

Expand Down Expand Up @@ -40,6 +43,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 All @@ -61,7 +87,8 @@ public void run() {

Log.v(LOG_TAG, "execute: jo=" + jo.toString());

senderID = jo.getString(SENDER_ID);
// Prefer explicitly defined SENDER_ID from the plugin's JS. Default to installation information.
senderID = !jo.isNull(SENDER_ID) ? jo.getString(SENDER_ID) : getSenderIdFromApplicationManifest();

Log.v(LOG_TAG, "execute: senderID=" + senderID);

Expand Down Expand Up @@ -94,6 +121,9 @@ else if (!savedSenderID.equals(senderID)) {
callbackContext.error("Empty registration ID received from GCM");
return;
}
} catch (NameNotFoundException e) {
Log.e(LOG_TAG, "execute: Got PackageManager's NameNotFound Exception " + e.getMessage());
callbackContext.error(e.getMessage());
} catch (JSONException e) {
Log.e(LOG_TAG, "execute: Got JSON Exception " + e.getMessage());
callbackContext.error(e.getMessage());
Expand Down

0 comments on commit c6d5193

Please sign in to comment.