Friday, December 4, 2015

Android Check If App is in Foreground on Receiving GCM message

Recently in one of my project we got a requirement to do some tasks when there is a GCM message in the app. Basically the requirement was to forcefully show some screens when there is a PUSH message. So if app is in Foreground we shall directly go to screen and if app is not running and GCM is received, app should go to particular screen on start. In this blog I am going to explain how to do this.

Basically I have used ordered broadcast to achieve this. Here is step by step process.

First let's capture this when application is in foreground. First add a broadcast receiver in your activity.


BroadcastReceiver mBroadcastReceiver

Now initialize it in onCreate method of your activity.

mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //Right here do what you want in your activity
        Log.v("message","got push on foreground");
            abortBroadcast();
        }
    };

Now register it in onResume method of your activity.

@Override
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter("com.your_package.GOT_PUSH");
        filter.setPriority(2);
        registerReceiver(mBroadcastReceiver, filter);
}

As you can see above we register intent with action. Now go to onMessage method of your GCMIntentService and add following code.

Intent responseIntent = new Intent("com.your_package.GOT_PUSH");
sendOrderedBroadcast(responseIntent, null);

As you can see above we are sending ordered broadcast in onMessage method. So whenever there is a PUSH message app onReceive method on receiver will be called in your activity and then you can do rest of the stuff.

Now let's see what to do when app is not running or is in background. We will define a receiver for that in our manifest file.

<receiver android:name=".BackgroundGCMBroadCastReceiver">
    <ntent-filter
        android:priority="1">
        <action
            android:name="com.your_package.GOT_PUSH" />
    </intent-filter>
</receiver>

Now add a class in your Project with name BackgroundGCMBroadCastReceiver which extends BroadcastReceiver.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;

public class BackgroundGCMBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //In this receiver just send your notification
    Log.v("message","got push on background");
    }
}

That's it now you can do your logic in onReceive method. Do not forget to unRegister receiver in your onPause method of activity.

@Override
protected void onPause() {

super.onPause();
try {
unregisterReceiver(mBroadcastReceiver);
} catch (IllegalArgumentException ex) {

}
}

That's it. Hope this helps you.

Thursday, December 3, 2015

Magento - Multiple Actions for Single Coupon Code

Hello,

Recently in one of my project we had a requirements to supports multiple actions for single coupon code. For example if my my cart total is between 500 to 1000, the coupon should give flat 50 off discount and if total is more than 1000, coupon should give flat 100 off discount. But coupon code should be same.

We all know that magento only allows unique coupon code and each coupon will have only once action. So how to do this? I have explained it in this blog.  Please note that this solution needs changes in magento database. So only use it if you database very well.

So while looking to solve this issue. I found out that, in magento database there is a unique key defined for coupon code. So to solve this problem we have to remove that unique key.

So open your Magento database and run following SQL command.

ALTER TABLE salesrule_coupon DROP INDEX UNQ_SALESRULE_COUPON_CODE;

This will drop unique key index.

Now go back to magento admin panel and clear the cache and create two different shopping cart rules with different discounts and keep the coupon code same and it will work.

Hope this helps you.