GCM Intent并不总是收到通知

时间:2013-06-11 09:59:20

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

我有以下GCM Intent服务:

 public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";

private static int new_video_count;

WifiLock wifiLock = null;

@Override
protected void onError(Context context, String errorId) {
    LogService.log(TAG, "Received error: " + errorId);
    Utils.displayMessage(context, getString(R.string.gcm_error, errorId));
}

@Override
protected void onMessage(Context context, Intent intent) {
    String message = getString(R.string.gcm_message);
    LogService.log(TAG, "onMessage() : " + message);
    try {
        if (intent.hasExtra("message_text")) {
            Log.d(TAG, "has data extra = " + intent.getStringExtra("message_text"));
            message = intent.getStringExtra("message_text");
            // TODO gcm notification should contain date element and parse
            // it here
        } else {
            Log.d(TAG, "NO data extra message_text");
        }
    } catch (NullPointerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    new_video_count = UserCredentialsPersistence.restoreNewVideoCount(getBaseContext()) + 1;
    UserCredentialsPersistence.saveNewVideoCount(getBaseContext(), new_video_count);
    Log.d(TAG, "new_video_count = " + new_video_count);
    // send to activity
    Utils.displayMessage(context, message);
    // send to notification bar
    generateNotification(context, message);
}

@Override
protected void onRegistered(Context context, String registrationId) {
    LogService.log(TAG, "Device registered: regId = " + registrationId);
    Utils.displayMessage(context, getString(R.string.gcm_registered));
    Settings.System.putInt(getContentResolver(), Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_NEVER);
    WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, "MyWifiLock");
    if (!wifiLock.isHeld()) {
        wifiLock.acquire();
        LogService.log(TAG, "Device registered: wifilock aquired ");
    }
}

@Override
protected void onUnregistered(Context context, String registrationId) {
    LogService.log(TAG, "Device unregistered");
    Utils.displayMessage(context, getString(R.string.gcm_unregistered));
    if (wifiLock != null) {
        if (wifiLock.isHeld()) {
            wifiLock.release();
            LogService.log(TAG, "Device registered: wifilock released ");
        }
    }
}

private static void generateNotification(Context context, String message) {

    LogService.log(TAG, "generateNotification()");

    ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);

    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    ComponentName componentInfo = taskInfo.get(0).topActivity;

    if (!componentInfo.getPackageName().equalsIgnoreCase(Constants.PACKAGE_NAME) || Constants.getNotif) {

        Log.d(TAG, "generateNotification() show notification top bar");

        int icon = R.drawable.icon;
        long when = System.currentTimeMillis();
        String title = "We";

        Intent notificationIntent = new Intent(context, VideoHolderActivity.class);
        notificationIntent.putExtra("video_id", "last_video");
        notificationIntent.putExtra("fragment", "last_video");
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        Notification notification;
        if (Build.VERSION.SDK_INT >= 16) {
            notification = new Notification.Builder(context).setContentTitle(title).setContentText(message).setSmallIcon(icon).setContentIntent(intent).build();
        } else {
            notification = new Notification.Builder(context).setContentTitle(title).setContentText(message).setSmallIcon(icon).setContentIntent(intent).getNotification();
        }

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.number = new_video_count;

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify((int) when, notification);

    } else {
        Log.w(TAG, "generateNotification() don't show notification top bar");
    }

    LogService.log(TAG, "generateNotification() new_video_count: " + new_video_count);
    if (VibrationManager.getInstance(context).getVibrationEnabled()) {
        playTone(context);
    }

}

@Override
protected String[] getSenderIds(Context context) {
    String[] ids = new String[1];
    ids[0] = Constants.SENDER_ID;
    return ids;// return super
}

private static void playTone(Context context) {

    LogService.log(TAG, "playTone()");

    try {

        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Ringtone ringtone = RingtoneManager.getRingtone(context, notification);

        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

        if (!ringtone.isPlaying()) {
            ringtone.play();

            if (vibrator.hasVibrator()) {
                vibrator.vibrate(300);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

现在,当收到消息时,如果您不在应用程序中,它将生成通知,如果您在应用程序中,它将使用以下内容显示消息:Utils.displayMessage(context, message);

以下是:

public static void displayMessage(Context context, String message) {
    Intent intent = new Intent(Constants.DISPLAY_MESSAGE_ACTION);
    intent.putExtra(Constants.EXTRA_MESSAGE, message);
    context.sendBroadcast(intent);
}

现在有时它会起作用,有时则不然。如果我没有更改我所在的片段,然后返回,它将检查通知,它将执行所需的操作。但是,我需要实时。可能是因为我没有收到通知?

编辑:另外一件事,几分钟后,我开始收到通知,在此之前,logcat向我展示了这个:

06-17 15:54:38.581: I/GTalkService/c(760): [AndroidEndpoint@1099335768] connect: acct=1000000, state=CONNECTING 06-17 15:54:38.581: I/GTalkService/c(760): [GTalkConnection@1099531696] connect: acct=1, state=CONNECTING

这是什么意思?我注册GCM后不应该出现这个吗?

0 个答案:

没有答案