在你指出它是重复之前,我知道这已被多次询问,但似乎在新GCM发布后(需要将播放服务项目作为库包含)things have changed on how to correctly implement gcm in our code。
我的GCMIntentService.java
和GcmBroadcastReceiver.java
中的断点在注册中被点击,因为我在清单中设置了<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
但是当我从服务器发送推送时断点没有被点击。以下是您需要的所有内容:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aceast.waveindia"
android:versionCode="1"
android:versionName="waveindia1" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="11" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
android:name="com.aceast.waveindia.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- 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" />
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.aceast.waveindia" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
GCMBroadcastReceiver.java
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
//breakpoint gets hit on register here...
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
register
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
.NET code
触发推送public string SendNotification()
{
var value = Message;
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
// tRequest.ContentType = " application/json;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", GOOGLE_APPID));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + DeviceID + "";
Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
现在可以指出错误吗?
答案 0 :(得分:1)
我在你的清单中看到了一个问题:
<uses-permission
android:name="com.aceast.waveindia.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
应该是:
<permission android:name="com.aceast.waveindia.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.aceast.waveindia.permission.C2D_MESSAGE" />