【问题标题】:Android : Client server programmingAndroid:客户端服务器编程
【发布时间】:2014-11-01 12:12:56
【问题描述】:

我是 android 中的 GCM 新手。从各种视频资源中,现在我对 GCM 中的客户端连接有了一些了解。但我缺乏服务器端编程。

我家里有台式机,我只是想把它作为服务器,通过我的手机和台式机发送一些数据。

我不知道如何将我的桌面作为服务器,如何从 android 连接到我的桌面 IP,如何从桌面服务器向 GCM 服务器发送数据,哪种语言在服务器端最好。

非常感谢您的指导。

问候, 阿斯温。

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

    下面的代码是如何使用 GCM 的一个很好的基本工作示例

    以下是我添加到清单中的相关部分:

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="20" />
    
    <permission
        android:name=".permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name=".permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    

    ...

    <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.badbob.app.gmctestapp" />
        </intent-filter>
    </receiver>
    
    <service android:name=".GCMIntentService" />
    

    我已将以下代码添加到我的主要活动的 onCreate 中:

        GCMRegistrar.checkDevice( this );
        GCMRegistrar.checkManifest( this );
        final String regId = GCMRegistrar.getRegistrationId( this );
        if( regId.equals( "" ) ) {
            GCMRegistrar.register( this, GCM_SENDER_ID );
        }
        else {
            Log.v( LOG_TAG, "Already registered" );
        }
    

    我还创建了 GCMIntenetService 类,如下所示:

    public class GCMIntentService extends GCMBaseIntentService {
    
        private static final String LOG_TAG = "GetAClue::GCMIntentService";
    
        public GCMIntentService() {
            super( GCM_SENDER_ID );
            // TODO Auto-generated constructor stub
            Log.i( LOG_TAG, "GCMIntentService constructor called" );
        }
    
        @Override
        protected void onError( Context arg0, String errorId ) {
            // TODO Auto-generated method stub
            Log.i( LOG_TAG, "GCMIntentService onError called: " + errorId );
        }
    
        @Override
        protected void onMessage( Context arg0, Intent intent ) {
            // TODO Auto-generated method stub
            Log.i( LOG_TAG, "GCMIntentService onMessage called" );
            Log.i( LOG_TAG, "Message is: " + intent.getStringExtra( "message" ) );
        }
    
        @Override
        protected void onRegistered( Context arg0, String registrationId ) {
            // TODO Auto-generated method stub
            Log.i( LOG_TAG, "GCMIntentService onRegistered called" );
            Log.i( LOG_TAG, "Registration id is: " + registrationId );
        }
    
        @Override
        protected void onUnregistered( Context arg0, String registrationId ) {
            // TODO Auto-generated method stub
            Log.i( LOG_TAG, "GCMIntentService onUnregistered called" );
            Log.i( LOG_TAG, "Registration id is: " + registrationId );
        }
    }
    

    【讨论】:

      【解决方案2】:

      GCM 服务器不过是 XMPP 服务器。

      您是否看过这篇文章:Impelementing GCM Server: https://developer.android.com/google/gcm/server.html

      看看这个

      Good XMPP Java Libraries for server side?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多