[英]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.