0

次の GCM インテント サービスがあります。

 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);
}

今ではうまくいくこともあれば、うまくいかないこともあります。そうでない場合は、現在のフラグメントを変更してから戻ると、通知がチェックされ、必要なことが行われます。しかし、私はそれがリアルタイムである必要があります。通知が届かない原因は何ですか?

編集:もう1つのことは、数分後、通知を受け取り始め、その前に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 を登録するとすぐに表示されますか?

4

0 に答える 0