2012-07-13 96 views
11

我需要Google雲消息示例應用程序。用示例服務器來測試我的應用程序。誰可以幫我這個事?Google Cloud messaging - 示例服務器

我需要一個樣本服務器來測試我的代碼,我已經編寫了代碼,但我不知道它是否會工作。我不知道服務器端編碼,所以任何人都可以幫助我。這裏是我的代碼

意圖服務

package com.example.pushnotificationsample; 

import android.content.Context; 

public class GCMIntentService extends GCMBaseIntentService { 

protected GCMIntentService(String senderId) { 
    super(senderId); 
    // TODO Auto-generated constructor stub 
} 

@Override 
protected void onError(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 
} 

@Override 
protected void onMessage(Context arg0, Intent msgIntent) { 
    // TODO Auto-generated method stub 
    Log.d("GCM", "RECIEVED A MESSAGE"); 
    //  String msg=msgIntent.getStringExtra("Message"); 
    Log.d("GCM", msgIntent.toString()); 
    // Get the data from intent and send to notificaion bar 

} 

@Override 
protected void onRegistered(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 
} 

@Override 
protected void onUnregistered(Context arg0, String arg1) { 
    // TODO Auto-generated method stub 
} 
} 

我的主要活動

package com.example.pushnotificationsample; 

import android.app.Activity; 
import com.google.android.gcm.GCMRegistrar; 
import android.os.Bundle; 
import android.util.Log; 

public class MainActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    GCMRegistrar.checkDevice(this); 
    // GCMRegistrar.checkManifest(this); 
    final String regId = GCMRegistrar.getRegistrationId(this); 
    if (regId.equals("")) { 
     GCMRegistrar.register(this, "555817657362"); 
     Log.v("Msg", "registered"); 
    } else { 
     Log.v("Msg", "Already registered"); 
    } 
} 


} 
+2

親愛的,如果你已經下載了「從SDK管理器GCM包」,那麼有客戶端,服務器的樣本。只需檢查** android-sdk/extras/google/GCM **目錄 – 2012-07-13 04:46:27

+0

Tnx很多Paresh – Dinu 2012-07-13 05:24:36

+0

@PareshMayani http://chat.stackoverflow.com/transcript/message/4508859#4508859但無法在sdk manager中找到extras文件夾 – Khan 2012-07-13 05:36:45

回答

23

你需要通過Android SDK中下載。轉到窗口 - > Android SDK管理器。向下滾動至額外並檢查「Google Cloud Messaging」並安裝。

完成後,你可以檢查在:android-sdk/extras/google/gcm/samples

,或者你可以試試這個(我已經上傳自己):gcm

用於服務器端,檢查這個答案:https://stackoverflow.com/a/11253231/554740

4

我發現一個開源發送Windows客戶端在這裏:你執行的GCM註冊代碼,並通過您的客戶端應用程序檢索您的註冊ID(設置一個斷點或打印後https://gcm.codeplex.com/

  • 設備令牌可以發現聲明讓你能夠複製/粘貼此值,這是很長)
  • 驗證密鑰後您安裝在谷歌的開發者控制檯項目發現

screenshot

6

「curl」命令行工具可用於向GCM中註冊的設備發送消息。

curl -X POST \ 
    -H "Authorization: key= <YOUR_AUTHORIZATION_KEY>" \ 
    -H "Content-Type: application/json" \ 
    -d '{ 
    "registration_ids": [ 
    "<YOUR_DEVICE_TOKEN>" 
    ], 
    "data": { 
    "message": "<YOUR_MESSAGE>" 
    } 
}' \ 
    https://android.googleapis.com/gcm/send 

請參閱此博客文章的進一步細節。 http://www.zinniakhan.com/2014/07/check-google-cloud-messaging-gcm-client.html

+0

堆棧溢出強烈建議僅鏈接回答。相反,[最好](http://meta.stackoverflow.com/q/8259)在這裏包含答案的基本部分,並提供供參考的鏈接。 – drs 2014-07-22 01:41:42

+1

感謝您的指示,回覆更新。 – user3002993 2014-07-22 01:54:35

+1

對我來說就像一個魅力,請注意!不要把任何像(它需要被隔離的)「這是我,一些虛假消息」的字符放入消息中),因爲它會產生很多痛苦。 – cV2 2014-09-25 16:11:34