1

As you may know access to Google servers such as www.android.com and all of its sub-domains like developer.android.com are denied in some countries by Google. Therefore, it is not possible to use GCM.

I have a NEWS & Magazine application and I want to broadcast message to particular devices. For example, Some users want to have new updates of Sports while some other may have new updates of other categories.

I'm looking for a way to simulate GCM system. Is it possible?

Thanks

Hesam
  • 52,260
  • 74
  • 224
  • 365

2 Answers2

2

Yes, It is possible.

  • Message API (you can use any format JSON or XML)
  • Android Service which will connect to that Message API and check for new message
  • And you might also need BroadcastReceiver

Here is some code that I used.

Firstly I create BroadcastReceiver and Service in AndroidManifest.xml

<receiver android:name="com.example.Push_Notification" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        <action android:name="android.net.wifi.NETWORK_IDS_CHANGED" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service
    android:name="com.example.Push_Notification_Checker"
    android:enabled="true" />

This is Push_Notification :

public class Push_Notification extends BroadcastReceiver{

    private Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        this.context = context;
        startService();
    }

    public void startService(){
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE );
        NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE|ConnectivityManager.TYPE_WIFI);
        boolean isConnected = activeNetInfo != null && activeNetInfo.isConnectedOrConnecting();

        Calendar cur_cal = Calendar.getInstance();
        cur_cal.setTimeInMillis(System.currentTimeMillis());
        Intent i = new Intent(context, Push_Notification_Checker.class);
        PendingIntent pintent = PendingIntent.getService(context, 0, i, 0);
        AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (isConnected){
            catchLog("connecte " +isConnected);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(), 1 * 60*1000, pintent);
        }
        else {
            catchLog("not connecte " +isConnected);
            alarm.cancel(pintent);
        }
    }

    private void catchLog(String log) {
        Log.i("PushNotification", log);
    }
}

This is Push_Notification_Checker service. :

public class Push_Notification_Checker extends Service {

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        catchLog("Service got created");
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        pref = getSharedPreferences(Constants.SHARED_PRED, 0);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        //Here i call AsyncTask to check Message. Android didn't allow Network Connection directly tied into Main Thread
        new GetMessage(getApplicationContext()).execute();
    }

}

By sending android UDID to server, you can check whether there is new message for the particular device. This way you can send message to specific device. How to getUDID ?

Here is why I use AsyncTask to connect and these two examples can help you with how to communicate with server.

how to send data to server using android by Http post method?

Send data from android to server via JSON

I know this is a long shot but hope this can help you in some way.

P.S: I'm also from the one of the country denied by Google

Community
  • 1
  • 1
Hein
  • 2,675
  • 22
  • 32
1

There are many different options apart from GCM.

Options that require some hacking:

MQTT - blog 1, blog 2

aSmack using XMPP

The Deacon Project

Third party out-of-the-box providers:

Parse

Urban Airship

Push.io among others

Also see:

Does Android support near real time push notification?

Push Notifications in Android Platform

Community
  • 1
  • 1
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84