我在GCM上收到此错误,有时我收到消息,但未生成任何通知。谁能帮我找出问题所在?

这是我的代码(为简单起见,删除了一些业务逻辑代码):

GcmBroadcastReceiver:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    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);
  }
}


GcmIntentService:

public class GcmIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

public GcmIntentService() {
    super(ConstantManager.SENDER_ID);
}

/**
 * Method called on device registered
 **/
@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
}

/**
 * Method called on device un registred
 * */
@Override
protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device unregistered");
}

/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("message");
    String type = intent.getExtras().getString("type");
    int eventId = 0;
    generateNotification(context, message,2,eventId);

}

/**
 * Method called on receiving a deleted message
 * */
@Override
protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
    // notifies user
    generateNotification(context, "Deleted Message",0,0);
}

/**
 * Method called on Error
 * */
@Override
public void onError(Context context, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    // log message
    Log.i(TAG, "Received recoverable error: " + errorId);
    return super.onRecoverableError(context, errorId);
}

/**
 * Issues a notification to inform the user that server has sent a message.
 */
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message,int type,int referId) {
    int icon = R.drawable.logo_v2;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);
    Log.i(TAG, "Generating Notification");
    String title = context.getString(R.string.app_name);
    if(GeneralManager.sessionManager.isLoggedIn())
    {
        if(type==2)
        {
            Log.i(TAG, "Type 2");
            Intent notificationIntent = new Intent(context, PendingEventDetailsActivity.class);
            SharedPreferences pref = context.getSharedPreferences(SessionManager.PREF_NAME, SessionManager.PRIVATE_MODE);
            String userId = pref.getString(SessionManager.KEY_USER_ID,null);
            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            notificationIntent.putExtra("eventId", String.valueOf(referId));
            notificationIntent.putExtra("userId", userId);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            // Play default notification sound
            notification.defaults |= Notification.DEFAULT_SOUND;

            //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

            // Vibrate if vibrate is enabled
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notificationManager.notify(0, notification);
        }
        else
        {
            Log.i(TAG, "Other type");
            Intent notificationIntent = new Intent(context, WelcomeActivity.class);
            // set intent so it does not start a new activity
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
            notificationIntent.putExtra("notification", "true");

            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, 0);
            notification.setLatestEventInfo(context, title, message, intent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;

            // Play default notification sound
            notification.defaults |= Notification.DEFAULT_SOUND;

            //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

            // Vibrate if vibrate is enabled
            notification.defaults |= Notification.DEFAULT_VIBRATE;
            notificationManager.notify(0, notification);
        }
    }

   }
}

最佳答案

我知道这个答案很晚,但是希望它能对其他人有所帮助。
此问题可以通过两种方法解决:

方法1:
WakeLock释放不正确时会遇到此问题,即当库尝试释放不包含任何内容的WakeLock(内部锁计数器变为负数)时。要避免这种情况,可以在WakeLocker上添加以下代码行(如果WakeLock未激活,则捕获异常) 。发布(); :



synchronized (LOCK) {
        // sanity check for null as this is a public method
        if (WakeLock != null) {
            Log.v(TAG, "Releasing wakelocker");
            try {
                 WakeLocker.release();
            } catch (Throwable th) {
                // ignoring this exception, probably wakeLock was already released
            }
        } else {
            // should never happen during normal workflow
            Log.e(TAG, "Reference of WakeLock is null");
        }
    }





方法2:
这是解决问题的更简便方法,您可以使用WakeLocker.isHeld();。在WakeLocker.release();上即



if (WakeLocker.isHeld())
    WakeLocker.release();





希望它能帮助任何人。

10-08 16:17