Commit e23ef7eb authored by Archana Naik's avatar Archana Naik
Browse files

Fixed couple of issues - 1. permission denied when launching the app from...

Fixed couple of issues - 1. permission denied when launching the app from notification. 2. Message is not downloaded when resumed after hitting hoem button.
parent f9f45d6a
Loading
Loading
Loading
Loading
+1 −1
Changes for plugin.xml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -88,7 +88,7 @@
            is unavailable. -->
            <amazon:enable-feature android:name="com.amazon.device.messaging" android:required="true"/>
			<service android:exported="false" android:name="com.amazon.cordova.plugin.ADMMessageHandler" />
            <activity android:name="com.amazon.cordova.plugin.ADMHandlerActivity" />
            <activity android:exported="true" android:name="com.amazon.cordova.plugin.ADMHandlerActivity" />
            <receiver android:name="com.amazon.cordova.plugin.ADMMessageHandler$Receiver" android:permission="com.amazon.device.messaging.permission.SEND">
                <intent-filter>
                    <action android:name="com.amazon.device.messaging.intent.REGISTRATION" />
+10 −10
Changes for src/amazon/ADMMessageHandler.java: 10 added lines, 10 removed lines.
Original line number Diff line number Diff line
@@ -145,7 +145,7 @@ public class ADMMessageHandler extends ADMMessageHandlerBase {
     * @param extras
     */
    public void createNotification(Context context, Bundle extras) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String appName = getAppName(this);

        // reuse the intent so that we can combine multiple messages into extra
@@ -159,23 +159,23 @@ public class ADMMessageHandler extends ADMMessageHandlerBase {
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        final Builder mBuilder = new Notification.Builder(context);
        mBuilder.setSmallIcon(context.getApplicationInfo().icon)
        final Builder notificationBuilder = new Notification.Builder(context);
        notificationBuilder.setSmallIcon(context.getApplicationInfo().icon)
            .setWhen(System.currentTimeMillis())
            .setContentIntent(contentIntent);

        if (PushPlugin.showMessageInNotificationCenter()) {
            mBuilder.setContentText(extras.getString("message"));
            notificationBuilder.setContentText(extras.getString("message"));
        } else {
            mBuilder.setContentText(PushPlugin.defaultNotificationMessage());
            notificationBuilder.setContentText(PushPlugin.defaultNotificationMessage());
        }

        String title = appName;
        mBuilder.setContentTitle(title).setTicker(title);
        mBuilder.setAutoCancel(true);
        notificationBuilder.setContentTitle(title).setTicker(title);
        notificationBuilder.setAutoCancel(true);
        // Because the ID remains unchanged, the existing notification is updated.
        mNotificationManager.notify((String) appName, NOTIFICATION_ID,
            mBuilder.build());
        notificationManager.notify((String) appName, NOTIFICATION_ID,
            notificationBuilder.getNotification());
    }

    public static void cancelNotification(Context context) {
@@ -203,7 +203,7 @@ public class ADMMessageHandler extends ADMMessageHandlerBase {
        }
    }

    static Bundle getOfflineMessage() {
    public static Bundle getOfflineMessage() {
        Bundle pushBundle = null;
        if (notificationIntent != null) {
            pushBundle = notificationIntent.getExtras().getBundle(PUSH_BUNDLE);
+46 −27
Changes for src/amazon/PushPlugin.java: 46 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaActivity;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;
import com.amazon.device.messaging.ADM;
@@ -99,7 +100,7 @@ public class PushPlugin extends CordovaPlugin {
                    .getStringProperty("defaultnotificationmessage", null);
            }
        } else {
            Log.e(TAG, NON_AMAZON_DEVICE_ERROR);
            LOG.e(TAG, NON_AMAZON_DEVICE_ERROR);
        }
    }

@@ -136,7 +137,6 @@ public class PushPlugin extends CordovaPlugin {
    public boolean execute(final String request, final JSONArray args,
        CallbackContext callbackContext) throws JSONException {
        try {

            // check ADM readiness
            ADMReadiness ready = isPushPluginReady();
            if (ready == ADMReadiness.NON_AMAZON_DEVICE) {
@@ -146,7 +146,7 @@ public class PushPlugin extends CordovaPlugin {
                callbackContext.error(ADM_NOT_SUPPORTED_ERROR);
                return false;
            } else if (callbackContext == null) {
                Log.e(TAG,
                LOG.e(TAG,
                    "CallbackConext is null. Notification to WebView is not possible. Can not proceed.");
                return false;
            }
@@ -155,28 +155,28 @@ public class PushPlugin extends CordovaPlugin {
            if (REGISTER.equals(request)) {

                if (args == null) {
                    Log.e(TAG, REGISTER_OPTIONS_NULL);
                    LOG.e(TAG, REGISTER_OPTIONS_NULL);
                    callbackContext.error(REGISTER_OPTIONS_NULL);
                    return false;
                }

                // parse args to get eventcallback name
                if (args.isNull(0)) {
                    Log.e(TAG, ECB_NOT_SPECIFIED);
                    LOG.e(TAG, ECB_NOT_SPECIFIED);
                    callbackContext.error(ECB_NOT_SPECIFIED);
                    return false;
                }

                JSONObject jo = args.getJSONObject(0);
                if (jo.getString("ecb").isEmpty()) {
                    Log.e(TAG, ECB_NAME_NOT_SPECIFIED);
                    LOG.e(TAG, ECB_NAME_NOT_SPECIFIED);
                    callbackContext.error(ECB_NAME_NOT_SPECIFIED);
                    return false;
                }
                callbackContext.success(REGISTRATION_SUCCESS_RESPONSE);
                notificationHandlerCallBack = jo.getString(ECB);
                String regId = adm.getRegistrationId();
                Log.d(TAG, "regId = " + regId);
                LOG.d(TAG, "regId = " + regId);
                if (regId == null) {
                    adm.startRegister();
                } else {
@@ -185,15 +185,8 @@ public class PushPlugin extends CordovaPlugin {

                // see if there are any messages while app was in background and
                // launched via app icon
                if (cachedExtrasAvailable()) {
                    Log.v(TAG, "sending cached extras");
                    sendExtras(gCachedExtras);
                    gCachedExtras = null;
                } else {
                    deliverOfflineMessages();
                }
                // Clear the notification if any exists
                ADMMessageHandler.cancelNotification(activity);
                LOG.d(TAG,"checking for offline message..");
                deliverPendingMessageAndCancelNotifiation();
                return true;

            } else if (UNREGISTER.equals(request)) {
@@ -201,7 +194,7 @@ public class PushPlugin extends CordovaPlugin {
                callbackContext.success(UNREGISTRATION_SUCCESS_RESPONSE);
                return true;
            } else {                
                Log.e(TAG, "Invalid action : " + request);
                LOG.e(TAG, "Invalid action : " + request);
                callbackContext.error("Invalid action : " + request);
                return false;
            }
@@ -243,33 +236,38 @@ public class PushPlugin extends CordovaPlugin {
     * Checks if offline message was pending to be delivered from notificationIntent. Sends it to webView(JS) if it is
     * and also clears notification from the NotificaitonCenter.
     */
    private void deliverOfflineMessages() {
    private boolean deliverOfflineMessages() {
        LOG.d(TAG,"deliverOfflineMessages()");
        Bundle pushBundle = ADMMessageHandler.getOfflineMessage();
        if (pushBundle != null) {
            Log.d(TAG,"Sending offline message...");
            LOG.d(TAG,"Sending offline message...");
            sendExtras(pushBundle);
            ADMMessageHandler.cleanupNotificationIntent();
            return true;
        }
        return false;
    }

    // lifecyle callback to set the isForeground
    @Override
    public void onPause(boolean multitasking) {
        Log.d(TAG, "onPause");
        LOG.d(TAG, "onPause");
        super.onPause(multitasking);
        isForeground = false;
    }

    @Override
    public void onResume(boolean multitasking) {
        Log.d(TAG, "onResume");
        LOG.d(TAG, "onResume");
        super.onResume(multitasking);
        isForeground = true;
        //Check if there are any offline messages?
        deliverPendingMessageAndCancelNotifiation();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        LOG.d(TAG, "onDestroy");
        super.onDestroy();
        isForeground = false;
        webview = null;
@@ -294,6 +292,27 @@ public class PushPlugin extends CordovaPlugin {
        return webview != null;
    }

    /**
     * Delivers pending/offline messages if any
     * 
     * @return returns true if there were any pending messages otherwise false.
     */
    public boolean deliverPendingMessageAndCancelNotifiation() {
        boolean delivered = false;
        LOG.d(TAG,"deliverPendingMessages()");
        if (cachedExtrasAvailable()) {
            LOG.v(TAG, "sending cached extras");
            sendExtras(gCachedExtras);
            gCachedExtras = null;
            delivered = true;
        } else {
            delivered = deliverOfflineMessages();
        }
        // Clear the notification if any exists
        ADMMessageHandler.cancelNotification(activity);
        
        return delivered;
    }
    /**
     * Sends register/unregiste events to JS
     * 
@@ -325,14 +344,14 @@ public class PushPlugin extends CordovaPlugin {
     */
    public static boolean sendJavascript(JSONObject json) {
        if (json == null) {
            Log.i(TAG, "JSON object is empty. Nothing to send to JS.");
            LOG.i(TAG, "JSON object is empty. Nothing to send to JS.");
            return true;
        }

        if (notificationHandlerCallBack != null && webview != null) {
            String jsToSend = "javascript:" + notificationHandlerCallBack + "("
                + json.toString() + ")";
            Log.v(TAG, "sendJavascript: " + jsToSend);
            LOG.v(TAG, "sendJavascript: " + jsToSend);
            webview.sendJavascript(jsToSend);
            return true;
        }
@@ -346,7 +365,7 @@ public class PushPlugin extends CordovaPlugin {
    public static void sendExtras(Bundle extras) {
        if (extras != null) {
            if (!sendJavascript(convertBundleToJson(extras))) {
                Log.v(TAG,
                LOG.v(TAG,
                    "sendExtras: could not send to JS. Caching extras to send at a later time.");
                gCachedExtras = extras;
            }
@@ -408,11 +427,11 @@ public class PushPlugin extends CordovaPlugin {
                } // while
            }
            json.put(PAYLOAD, jsondata);
            Log.v(TAG, "extrasToJSON: " + json.toString());
            LOG.v(TAG, "extrasToJSON: " + json.toString());

            return json;
        } catch (JSONException e) {
            Log.e(TAG, "extrasToJSON: JSON exception");
            LOG.e(TAG, "extrasToJSON: JSON exception");
        }
        return null;
    }