0

我想从我的服务器接收到我的 android 应用程序的推送通知。我正在使用来自 Google Cloud Messaging for Android Library extras/google/samples/ 的示例。我已经在谷歌上搜索了 3 天以寻求帮助,但没有希望:/。

我就是这样进行的:

你会在最后找到源代码

I-服务器部分:

1-我用块注释打开 gcm-demo-server\WebContent\WEB-INF\classes\api。我用我的 api 密钥改变了它

2-在 gcm-demo-server 目录的 shell 窗口中,我运行“ant war”

3-我将 dist\gcm-demo.war 文件复制到 tomcat\webapps

4-我在浏览器中使用这个 url localhost:8080/gcm-demo 打开服务器

II-App部分:

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

1- 静态最终字符串 SERVER_URL = "localhost:8080/gcm-demo/home";(我的服务器 url)

2- 静态最终字符串 SENDER_ID = "876836784656"; (我的项目编号)

之后我打开服务器。它显示“未注册设备”,然后我运行显示“设备已在服务器上注册”的应用程序,但是当我刷新服务器时,它显示“未注册设备”,但是我的服务器必须显示已注册的设备和一个发送按钮“推送通知” :cry: PLEAAAZ 帮助!

应用程序代码: - 源代码和类在这里

  • 清单:

    <?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>
    

服务器代码:

-所有课程都在这里

我要感谢大家如何看待这个话题 :oops: 。

4

1 回答 1

0

我在尝试该演示时遇到了问题,因为我的 tomcat 在本地运行,但是模拟器就像一个“虚拟路由器”,所以我无法http://localhost:8080仅从我的 android 应用程序访问我的 tomcat 服务器。我必须使用运行 Tomcat 服务器的机器的 IP,所以它就像http://[my-machine's ip]:8080/gcm-demo[my-machine's ip] 是这样的192.168.1.10

注意:您可以通过在 Linux 或 MacOS 上运行 ifconfig 或在 Windows 上运行 ipconfig 来获取 IP。ip 地址是 IPv4 字段的值。

于 2013-07-04T11:11:55.953 回答