未显示GCM通知

时间:2012-11-22 23:55:35

标签: android google-cloud-messaging android-notifications

我正在实现一个向PHP服务器发送消息的Android应用程序,然后生成GCM推送通知的PHP服务器将其指向相关的注册ID。问题是尽管GCMIntentService类收到了消息,但未显示Notification,因为它显示在附加的log cat中

HERE是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);

}
}

这里是LOG CAT

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

1 个答案:

答案 0 :(得分:2)

试试这段代码......

 package com.gcm.demo;

    import static com.gcm.demo.CommonUtilities.SENDER_ID;
    import static com.gcm.demo.CommonUtilities.displayMessage;

    import android.app.IntentService;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;

    import com.google.android.gcm.GCMBaseIntentService;
    import com.google.android.gcm.GCMRegistrar;


    /**
     * {@link IntentService} responsible for handling GCM messages.
     */
    public class GCMIntentService extends GCMBaseIntentService {

        @SuppressWarnings("hiding")
        private static final String TAG = "GCMIntentService";

        public GCMIntentService() {
            super(SENDER_ID);
            Log.i(TAG, "================Inside GCMIntentService Constructor==============================");
        }

        @Override
        protected void onRegistered(Context context, String registrationId) {
            Log.i(TAG, "================Inside onRegistered Method==============================");
            Log.i(TAG, "Device registered: regId = " + registrationId);
            displayMessage(context, getString(R.string.gcm_registered));
            ServerUtilities.register(context, registrationId);
        }

        @Override
        protected void onUnregistered(Context context, String registrationId) {
            Log.i(TAG, "================Inside onUnRegistered Method==============================");
            displayMessage(context, getString(R.string.gcm_unregistered));
            if (GCMRegistrar.isRegisteredOnServer(context)) {
                ServerUtilities.unregister(context, registrationId);
            } else {
                // This callback results from the call to unregister made on
                // ServerUtilities when the registration to the server failed.
                Log.i(TAG, "Ignoring unregister callback");
            }
        }

        @Override
        protected void onMessage(Context context, Intent intent) {
            Log.i(TAG, "================Inside OnMessage Method==============================");
            Log.i(TAG, "Received message");
            String message = getString(R.string.gcm_message);
            displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
        }

        @Override
        protected void onDeletedMessages(Context context, int total) {
            Log.i(TAG, "================Inside onDeletedMessages Method==============================");
            Log.i(TAG, "Received deleted messages notification");
            String message = getString(R.string.gcm_deleted, total);
            displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
        }

        @Override
        public void onError(Context context, String errorId) {
            Log.i(TAG, "================Inside onError Method==============================");
            Log.i(TAG, "Received error: " + errorId);
            displayMessage(context, getString(R.string.gcm_error, errorId));
        }

        @Override
        protected boolean onRecoverableError(Context context, String errorId) {
            // log message
            Log.i(TAG, "================Inside onRecoverableError Method==============================");
            Log.i(TAG, "Received recoverable error: " + errorId);
            displayMessage(context, getString(R.string.gcm_recoverable_error,
                    errorId));
            return super.onRecoverableError(context, errorId);
        }

        /**
         * Issues a notification to inform the user that server has sent a message.
         */
        private static void generateNotification(Context context, String message) {
            Log.i(TAG, "================Inside generateNotification Method==============================");
            int icon = R.drawable.ic_stat_gcm;
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager)
                    context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new Notification(icon, message, when);
            String title = context.getString(R.string.app_name);
            Intent notificationIntent = new Intent(context, DemoActivity.class);
            // 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, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, notification);
        }

    }