2013-01-11 50 views
1

正如你可能知道的访问谷歌服务器,如www.android.com及其所有子域的样developer.android.com在一些国家通过谷歌否认。因此,不可能使用GCM。Android,是否可以在不使用Google云消息系统的情况下向设备广播消息?

我有一个新闻&杂志应用程序,我想广播消息到特定设备。例如,某些用户想要对体育进行新的更新,而另一些用户可能会对其他类别进行新的更新。

我正在寻找一种方法来模拟GCM系统。可能吗?

谢谢

+0

这可能吗?为什么不呢:Google做到了。请注意,添加其他服务会使用更多电池。 –

回答

2

是的,这是可能的。

  • Message API(你可以使用任何格式JSON或XML)
  • Android Service将连接到该消息API和检查 新的消息
  • ,你可能还需要BroadcastReceiver

这里是我使用的一些代码。

首先我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" /> 

这是Push_Notification创建BroadcastReceiverService

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); 
    } 
} 

这是Push_Notification_Checker服务。 :

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(); 
    } 

} 

通过发送android UDID到服务器,您可以检查是否有特定设备的新消息。这样你可以发送消息到特定的设备。 How to getUDID ?

Here是我使用AsyncTask进行连接的原因,这两个示例可以帮助您了解如何与服务器进行通信。

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

Send data from android to server via JSON

我知道这是一个长镜头,但希望这可以帮助你以某种方式。

P.S:我也是从国家的由谷歌

否认一个