我在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);
}
}
}
}
答案 0 :(得分:0)
我知道这个答案迟到了,但希望它可以帮助别人。 这个问题可以通过两种方法解决:
方法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();

希望它可以帮助任何人。