我m developing an app with phonegap and using GCM for Push Notification, but i cann
没有收到任何通知,但是 Google 服务器返回消息 ID,我猜它是成功的消息。
那是我的 MainFest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="MyPackageName.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="MyPackageName.permission.C2D_MESSAGE" />
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="MyPackageName.permission.MAPS_RECIEVE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/title_activity_Test"
android:theme="@style/AppTheme" android:allowBackup="true">
<activity
android:name="Test"
android:label ="@string/title_activity_Test" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GCMIntentService" />
<receiver
android:name="com.google.android.gcm.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="MyPackageName" />
</intent-filter>
</receiver>
</application>
我的 GCMIntentService.java
package MyPackageName;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
}
@Override
protected void onError(final Context arg0, final String arg1) {
}
@Override
protected void onMessage(final Context arg0, final Intent arg1) {
String message = "";
if (arg1.getExtras() != null) {
final Bundle extras = arg1.getExtras();
if (extras.containsKey("message")) {
message = extras.getString("message");
}
}
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setContentTitle("Test").setContentText(message)
.setSmallIcon(R.drawable.ic_launcher);
final Intent resultIntent = new Intent(this, MainActivity.class);
final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(resultIntent);
final PendingIntent resultPendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
try {
final Uri notification = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
final Ringtone r = RingtoneManager.getRingtone(
getApplicationContext(), notification);
r.play();
} catch (final Exception e) {
}
notificationManager.notify(1, mBuilder.build());
}
@Override
protected void onRegistered(final Context arg0, final String arg1) {
}
@Override
protected void onUnregistered(final Context arg0, final String arg1) {
}
}
并调用该函数在 MainActivity 注册。
private String RegisterWithGCM()
{
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "MySenderID");
regId = GCMRegistrar.getRegistrationId(this);
} else {
Log.v("Registration", "Already registered, regId: " + regId);
}
try{
new AlertDialog.Builder(this).setTitle("Reg").setMessage(regId).setNeutralButton("CLose", null).show();
}
catch (Exception e){
}
return regId;
}
在服务器端,我尝试了两种方法
using (var wc = new WebClient())
{
wc.Headers.Add("Authorization", "key=" + serviceKey);
var nameValues = new NameValueCollection
{
{"registration_id", registrationId},
{"collapse_key", Guid.NewGuid().ToString()},
{"data.payload", HttpUtility.UrlEncode(message)}
};
var resp = wc.UploadValues("https://android.googleapis.com/gcm/send", nameValues);
var respMessage = Encoding.Default.GetString(resp);
}
和
var value = "test";
System.Net.WebRequest tRequest;
tRequest = System.Net.WebRequest.Create("https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
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=" + regId + "";
Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
tRequest.ContentType = "application/x-www-form-urlencoded";
System.IO.Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
System.Net.WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
System.IO.StreamReader tReader = new System.IO.StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tResponse.Close();
这是谷歌服务器响应
id=0:1378735596819604%5511fa701fcc42e9 所以请帮忙???