[英]Android client for GCM not receiving echoing push notification
因此,在过去15个小时左右的时间里,我一直遇到问题,我关注GCM的Android使用入门(包括客户端演示源),据我所知,我应该在注册设备后立即收到推送通知并在发送ECHO_NOW消息后。 这是我所做的和一些代码片段:
我一直在HTC One(4.3)中运行代码
Activity
,一个IntentService
和一个WakefulBroadcastReceiver
接收器。 AndroidManifest.xml中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="my.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="my.package.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="my.package.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="my.package" />
</intent-filter>
</receiver>
<service android:name="my.package.GcmIntentService" />
</application>
</manifest>
我假设阅读此内容后会收到推送通知(包含在gcm客户端演示实现的源代码中):
// For this demo: we don't need to send it because the device
// will send upstream messages to a server that echo back the
// message using the 'from' address in the message.
自从第一次尝试以来,设备成功地注册并获得注册ID。
题
我是否做出了错误的假设,不应收到任何推送通知? 如果没有,那可能是错的?
我已经尽可能地记录了,我应该发布更多代码吗? 我检查了很多问题,没有一个解决这个确切的问题,或者只是解决不了。
在此先感谢社区。
此致
阿曼多
编辑:结论:GCM演示客户端工作正常,只需要记住,需要服务器端来请求GCM发送实际的推送通知。 ECHO_NOW并不意味着GCM会代您发送一些东西。 实现服务器端!
带标记的答案(伊朗)是第一个,但是Rajat的片段为需要抬头的人提供摘要。 谢谢你俩。
实施服务器端代码以将通知发送到您的设备。 有关更多信息,请学习Android GCM教程 。
<?php
$api_key = "AIzBpx4XbXwKd8P-v2d1Jk";
$registrationIDs = array("APA91b3Rjlo8T_URx15NqvxY2mRyQ");
$message = "congratulations";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message ),
);
$headers = array(
'Authorization: key=' . $api_key,
'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
您遵循的GCM演示应用程序使用新设备进行云消息传递(上游消息)以向服务器发送消息,并假定服务器回显该消息。 为此,您应该实现服务器端(应该建立与GCM Cloud Connection服务器的连接)。 这也将需要您填写一个表格,以便为新的GCM功能列出白色的项目ID。
如果您不需要新的GCM功能(设备到云和用户通知),您只需将注册ID发送到服务器并从服务器向您的设备发送消息。
您可以在此处查看所需服务器端代码的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.