我想收到从我的服务器到我的Android应用程序的推送通知。我使用Google Cloud Messaging for Android Library extras / google / samples /的示例。我已经谷歌搜索3天寻求帮助但没有希望:/。
这就是我的进展:
您将在最后找到源代码
我 - 服务器部分:
1-I使用块注释打开gcm-demo-server \ WebContent \ WEB-INF \ classes \ api。我用api键改变了它
2 - 在gcm-demo-server目录的shell窗口中运行“ant war”
3-I将dist \ gcm-demo.war文件复制到tomcat \ webapps
4 - 我在浏览器中使用此url localhost打开服务器:8080 / gcm-demo
II-App部分:
在CommonUtilities.java中,我更改了以下字段:
1- static final String SERVER_URL =“localhost:8080 / gcm-demo / home”;(我的服务器网址)
2- static final String SENDER_ID =“876836784656”; (我的项目ID)
之后我打开服务器。它显示“没有设备注册”,然后我运行它显示“设备已经在服务器上注册”的应用程序,但当我刷新我的服务器时显示“没有设备注册”,但是我的服务器必须显示注册的设备和一个按钮发送“推送通知”:哭: PLEAAAZ帮忙!!
应用代码: - 源代码和类是HERE
The Manifest:
<?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>
服务器代码:
- 所有课程均为HERE
我要感谢大家如何看待这个主题:oops:。
答案 0 :(得分:0)
我尝试该演示时出现问题,因为我的tomcat是在本地运行的,但仿真器就像一个“虚拟路由器”,所以我只能使用 http://localhost:8080
<来访问我的tomcat服务器/ strong>来自我的Android应用程序。我必须使用运行Tomcat服务器的机器的IP,所以它就像 http://[my-machine's ip]:8080/gcm-demo
,其中[my-machine的ip]就像这样 192.168。 1.10 强>
注意:您可以通过在Linux或MacOS上运行ifconfig或在Windows上运行ipconfig来获取IP。 ip地址是IPv4字段的值。