2013-07-03 82 views
0

我想從我的服務器到我的android應用程序的推送通知。我使用來自Google雲消息傳遞的Android庫示例額外/ google/samples /。我一直在使用谷歌搜索3天的幫助,但沒有希望:/。Android推送通知 - 沒有設備在服務器註冊

這是我是如何進行的:

1-I打開GCM-演示服務器\:

您將在年底

I-服務器部分找到源代碼WebContent \ WEB-INF \ classes \ api和塊註釋。我用我的API密鑰改變它

2在GCM-演示服務器目錄中的shell窗口我運行 「蟻戰」

3-I DIST \ GCM-demo.war文件複製到Tomcat \的webapps

4我在這個網址本地主機瀏覽器中打開服務器:8080/GCM-演示

II-應用部分:

在CommonUtilities.java我改變了以下字段:

1靜態最後絃樂SERVER_URL = 「本地主機:8080/GCM-DEMO /家」(我的服務器URL)

2-靜態最後絃樂SENDER_ID = 「876836784656」; (我的項目ID)

之後,我打開服務器。它顯示「沒有設備註冊」,然後我運行該應用程序顯示「設備已在服務器上註冊」,但當我刷新我的服務器時顯示「沒有設備註冊」,但是我的服務器必須顯示註冊設備和一個按鈕發送「推送通知」:cry: PLEAAAZ help !!

應用代碼: - 該源代碼和類HERE

  • 所述清單:

    <?xml version="1.0" encoding="utf-8"?> 
    

    <!-- GCM requires Android SDK version 2.2 (API level 8) or above. --> 
    <!-- The targetSdkVersion is optional, but it's always a good practice 
        to target higher versions. --> 
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/> 
    
    <!-- GCM connects to Google Services. --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    
    <!-- GCM requires a Google account. --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    
    <!-- Keeps the processor from sleeping when a message is received. --> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    
    <!-- 
    Creates a custom permission so only this app can receive its messages. 
    
    NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE, 
         where PACKAGE is the application's package name. 
    --> 
    <permission 
        android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" 
        android:protectionLevel="signature" /> 
    <uses-permission 
        android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" /> 
    
    <!-- This app has permission to register and receive data message. --> 
    <uses-permission 
        android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    
    <!-- Main activity. --> 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
         android:name=".DemoActivity" 
         android:label="@string/app_name" 
         android:screenOrientation="portrait" > 
         <intent-filter> 
          <action android:name="android.intent.action.MAIN" /> 
    
          <category android:name="android.intent.category.LAUNCHER" /> 
         </intent-filter> 
        </activity> 
    
        <!-- 
         BroadcastReceiver that will receive intents from GCM 
         services and handle them to the custom IntentService. 
    
         The com.google.android.c2dm.permission.SEND permission is necessary 
         so only GCM services can send data messages for the app. 
        --> 
        <receiver 
         android:name="com.google.android.gcm.GCMBroadcastReceiver" 
         android:permission="com.google.android.c2dm.permission.SEND" > 
         <intent-filter> 
          <!-- Receives the actual messages. --> 
          <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
          <!-- Receives the registration id. --> 
          <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
          <category android:name="com.google.android.gcm.demo.app" /> 
         </intent-filter> 
        </receiver> 
    
        <!-- 
         Application-specific subclass of GCMBaseIntentService that will 
         handle received messages. 
    
         By default, it must be named .GCMIntentService, unless the 
         application uses a custom BroadcastReceiver that redefines its name. 
        --> 
        <service android:name=".GCMIntentService" /> 
    </application> 
    

Server代碼:

- 所有類HERE

我要感謝大家如何需要看這個話題:哎呀。

+0

[如何從Eclipse的Android模擬器連接到我的http:// localhost web服務器](http://stackoverflow.com/questions/5806220/how-to-connect-to-my-http- localhost-web-server-from-android-emulator-in-eclips) – Selvin

+0

由於您沒有在您的Web服務器上保存已註冊設備的列表,因此預期這種行爲。每次重新啓動Web服務器時,都必須重新註冊,以便將其存儲在某個內存中的對象(如數組)中。將數據庫支持添加到服務器應用程序應該可以解決問題 – faizal

回答

0

我有問題時,我嘗試了演示,因爲我的tomcat是在本地運行,但仿真器具有像「虛擬路由器」按這樣說只是用http://localhost:8080從我的Android應用我無法訪問我的Tomcat服務器。我不得不使用運行Tomcat服務器我的機器的IP,所以它會像http://[my-machine's ip]:8080/gcm-demo其中,[我的機器的IP]是這樣的192.168.1.10

注:你可以通過在Linux或MacOS上運行ifconfig或在Windows上運行ipconfig來獲取IP。 該ip地址是IPv4字段的值。

相關問題