我正在实现一个向 PHP 服务器发送消息的 Android 应用程序,然后生成 GCM 推送通知的 PHP 服务器将其定向到相关的注册 ID。问题是尽管 GCMIntentService 类收到了消息,但未显示通知,如附加的日志 cat 中所示
这是Android代码
public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "===GCMIntentService===";
private static final String senderId = "*****";
String message;
public GCMIntentService() {
super(senderId);
}
static void displayMessage(Context context, String message) {
Intent intent = new Intent("guc.edu.iremote.DISPLAY_MESSAGE");
intent.putExtra("message", message);
context.sendBroadcast(intent);
}
@Override
protected void onRegistered(Context arg0, String registrationId) {
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
// sets the app name in the intent
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", senderId);
startService(registrationIntent);
Log.i(TAG, "Device registered: regId = " + registrationId);
displayMessage(getApplicationContext(), "Your device registred with GCM");
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
startService(unregIntent);
Log.i(TAG, "unregistered = " + arg1);
displayMessage(getApplicationContext(), "Unregistered");
}
@Override
protected void onMessage(Context context, Intent intent) {
message = intent.getStringExtra( "message" );
// message = intent.getExtras().getString("message");
Intent notificationIntent=new Intent(context,this.getApplicationContext().getClass());
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire();
displayMessage(context, message);
generateNotification(context, message, notificationIntent);
final AlertDialog.Builder d = new AlertDialog.Builder(context);
//Use an activity object here
d.setMessage(message) //Provide a message here... A string or a string ID will do
.setCancelable(false) //If you want them to be able to dismiss with a Back button
.setPositiveButton(R.drawable.navigationaccept, new DialogInterface.OnClickListener() {
AlertDialog dialog = d.create();
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
})
.create()
.show();
}
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message, Intent notificationIntent) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
notificationManager.notify(0, notification);
String title = context.getString(R.string.app_name);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
}
@Override
protected void onError(Context arg0, String errorId) {
Log.i(TAG, "Received error: " + errorId);
displayMessage(arg0, getString(R.string.gcm_error, errorId));
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
displayMessage(context, getString(R.string.gcm_recoverable_error,
errorId));
return super.onRecoverableError(context, errorId);
}
}
这是日志猫
11-22 22:49:10.747: V/GCMBroadcastReceiver(6305): onReceive: com.google.android.c2dm.intent.REGISTRATION
11-22 22:49:10.747: V/GCMBroadcastReceiver(6305): GCM IntentService class: guc.edu.iremote.GCMIntentService
11-22 22:49:10.747: V/GCMBaseIntentService(6305): Acquiring wakelock
11-22 22:49:10.838: V/GCMBaseIntentService(6305): Intent service name: GCMIntentService-55975***5670-181
11-22 22:49:10.877: D/GCMBaseIntentService(6305): handleRegistration: registrationId = ****, error = null, unregistered = null
11-22 22:49:10.877: D/GCMRegistrar(6305): resetting backoff for guc.edu.iremote
11-22 22:49:10.907: V/GCMRegistrar(6305): Saving regId on app version 1
11-22 22:49:10.927: I/===GCMIntentService===(6305): Device registered: regId = ****
11-22 22:49:10.957: V/GCMBaseIntentService(6305): Releasing wakelock
11-22 22:49:13.087: V/GCMBroadcastReceiver(6305): onReceive: com.google.android.c2dm.intent.REGISTRATION
11-22 22:49:13.087: V/GCMBroadcastReceiver(6305): GCM IntentService class: guc.edu.iremote.GCMIntentService
11-22 22:49:13.087: V/GCMBaseIntentService(6305): Acquiring wakelock
11-22 22:49:13.388: V/GCMBaseIntentService(6305): Intent service name: GCMIntentService-559753615670-182
11-22 22:49:13.388: I/Choreographer(6305): Skipped 67 frames! The application may be doing too much work on its main thread.
11-22 22:49:13.417: D/GCMBaseIntentService(6305): handleRegistration: registrationId = ****, error = null, unregistered = null
11-22 22:49:13.430: D/GCMRegistrar(6305): resetting backoff for guc.edu.iremote
11-22 22:49:13.430: V/GCMRegistrar(6305): Saving regId on app version 1
11-22 22:49:13.477: I/===GCMIntentService===(6305): Device registered: regId = ****
11-22 22:49:13.477: V/GCMBaseIntentService(6305): Releasing wakelock