public static void e(String tag, String message) { e(tag, message, null); } }
/* package */ Date parse(String dateString) { synchronized (lock) { try { return dateFormat.parse(dateString); } catch (java.text.ParseException e) { // Should never happen PLog.e(TAG, "could not parse date: " + dateString, e); return null; } } }
/* package */ Date parse(String dateString) { synchronized (lock) { try { return dateFormat.parse(dateString); } catch (java.text.ParseException e) { // Should never happen PLog.e(TAG, "could not parse date: " + dateString, e); return null; } } }
/** * Get the push data as a parsed JSONObject * * @param intent the intent of the notification * @return the parsed JSONObject, or null */ protected JSONObject getPushData(Intent intent) { try { return new JSONObject(intent.getStringExtra(KEY_PUSH_DATA)); } catch (JSONException e) { PLog.e(TAG, "Unexpected JSONException when receiving push data: ", e); return null; } }
/** * Sets the message that will be shown in the notification. This will overwrite any data specified * in {@link #setData(JSONObject)}. */ public void setMessage(String message) { JSONObject data = new JSONObject(); try { data.put(KEY_DATA_MESSAGE, message); } catch (JSONException e) { PLog.e(TAG, "JSONException in setMessage", e); } setData(data); }
private void setInternal(String newInstallationId) { synchronized (lock) { try { ParseFileUtils.writeStringToFile(file, newInstallationId, "UTF-8"); } catch (IOException e) { PLog.e(TAG, "Unexpected exception writing installation id to disk", e); } installationId = newInstallationId; } }
static String getPushHashFromIntent(Intent intent) { String pushData = null; if (intent != null && intent.getExtras() != null) { pushData = intent.getExtras().getString(ParsePushBroadcastReceiver.KEY_PUSH_DATA); } if (pushData == null) { return null; } String pushHash = null; try { JSONObject payload = new JSONObject(pushData); pushHash = payload.optString("push_hash"); } catch (JSONException e) { PLog.e(TAG, "Failed to parse push data: " + e.getMessage()); } return pushHash; } }
/** * Attempts to insert a push into history. The push is ignored if we have already seen it * recently. Otherwise, the push is inserted into history. If the length of the history exceeds * the maximum length, then the history is trimmed by removing the oldest pushes until it no * longer exceeds the maximum length. * * @return Returns whether or not the push was inserted into history. */ public boolean tryInsertPush(String pushId, String timestamp) { if (timestamp == null) { throw new IllegalArgumentException("Can't insert null pushId or timestamp into history"); } if (lastTime == null || timestamp.compareTo(lastTime) > 0) { lastTime = timestamp; } if (pushIds.contains(pushId)) { PLog.e(TAG, "Ignored duplicate push " + pushId); return false; } entries.add(new Entry(pushId, timestamp)); pushIds.add(pushId); while (entries.size() > maxHistoryLength) { Entry head = entries.remove(); pushIds.remove(head.pushId); } return true; }
/** * Returns the version code for this app, as specified by the android:versionCode attribute in the * <manifest> element of the manifest. */ public static int getVersionCode() { synchronized (lock) { if (versionCode == -1) { try { versionCode = getPackageManager().getPackageInfo(getContext().getPackageName(), 0).versionCode; } catch (NameNotFoundException e) { PLog.e(TAG, "Couldn't find info about own package", e); } } } return versionCode; }
private synchronized void saveStateToDisk() { try { ParseFileUtils.writeJSONObjectToFile(diskState, toJSON()); } catch (IOException | JSONException e) { PLog.e(TAG, "Unexpected error when serializing push state to " + diskState, e); } }
static JSONObject jsonFromKeyValueCache(String key, long maxAgeMilliseconds) { String raw = loadFromKeyValueCache(key, maxAgeMilliseconds); if (raw == null) { return null; } try { return new JSONObject(raw); } catch (JSONException e) { PLog.e(TAG, "corrupted cache for " + key, e); clearFromKeyValueCache(key); return null; } } }
/** * Returns the version name for this app, as specified by the android:versionName attribute in the * <manifest> element of the manifest. */ public static String getVersionName() { synchronized (lock) { if (versionName == null) { try { versionName = getPackageManager().getPackageInfo(getContext().getPackageName(), 0).versionName; } catch (NameNotFoundException e) { PLog.e(TAG, "Couldn't find info about own package", e); versionName = "unknown"; } } } return versionName; }
static String loadFromKeyValueCache(final String key, final long maxAgeMilliseconds) { synchronized (MUTEX_IO) { File file = getKeyValueCacheFile(key); if (file == null) { return null; } Date now = new Date(); long oldestAcceptableAge = Math.max(0, now.getTime() - maxAgeMilliseconds); if (getKeyValueCacheAge(file) < oldestAcceptableAge) { return null; } // Update mtime to make the LRU work file.setLastModified(now.getTime()); try { RandomAccessFile f = new RandomAccessFile(file, "r"); byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes, "UTF-8"); } catch (IOException e) { PLog.e(TAG, "error reading from cache", e); return null; } } }
PLog.e(TAG, "Found " + SENDER_ID_EXTRA + " <meta-data> element with value \"" + senderIDExtra.toString() + "\", but the value is missing the expected \"id:\" " + "prefix."); PLog.e(TAG, "You must provide " + SENDER_ID_EXTRA + " in your AndroidManifest.xml\n" + "Make sure to prefix with the value with id:\n\n" + "<meta-data\n" +
@Override public void onMessageReceived(String s, Bundle bundle) { super.onMessageReceived(s, bundle); String pushId = bundle.getString("push_id"); String timestamp = bundle.getString("time"); String dataString = bundle.getString("data"); String channel = bundle.getString("channel"); JSONObject data = null; if (dataString != null) { try { data = new JSONObject(dataString); } catch (JSONException e) { PLog.e(ParseGCM.TAG, "Ignoring push because of JSON exception while processing: " + dataString, e); return; } } PushRouter.getInstance().handlePush(pushId, timestamp, channel, data); } }
uriString = pushData.optString("uri", null); } catch (JSONException e) { PLog.e(TAG, "Unexpected JSONException when receiving push data: ", e);
/** * Loads the installationId from memory, then tries to loads the legacy installationId from disk * if it is present, or creates a new random UUID. */ public String get() { synchronized (lock) { if (installationId == null) { try { installationId = ParseFileUtils.readFileToString(file, "UTF-8"); } catch (FileNotFoundException e) { PLog.i(TAG, "Couldn't find existing installationId file. Creating one instead."); } catch (IOException e) { PLog.e(TAG, "Unexpected exception reading installation id from disk", e); } } if (installationId == null) { setInternal(UUID.randomUUID().toString()); } } return installationId; }
@Override public void done(ParseException e) { if (e == null) { PLog.d(ParseFCM.TAG, "FCM token saved to installation"); jobFinished(job, false); } else { PLog.e(ParseFCM.TAG, "FCM token upload failed", e); jobFinished(job, true); } } });
@Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); PLog.d(ParseFCM.TAG, "onMessageReceived"); String pushId = remoteMessage.getData().get("push_id"); String timestamp = remoteMessage.getData().get("time"); String dataString = remoteMessage.getData().get("data"); String channel = remoteMessage.getData().get("channel"); JSONObject data = null; if (dataString != null) { try { data = new JSONObject(dataString); } catch (JSONException e) { PLog.e(ParseFCM.TAG, "Ignoring push because of JSON exception while processing: " + dataString, e); return; } } PushRouter.getInstance().handlePush(pushId, timestamp, channel, data); } }
@Override public Void call() { try { InstanceID instanceID = InstanceID.getInstance(getApplicationContext()); String senderId = job.getExtras().getString(KEY_GCM_SENDER_ID); String token = instanceID.getToken(senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); ParseInstallation installation = ParseInstallation.getCurrentInstallation(); installation.setDeviceToken(token); //even though this is FCM, calling it gcm will work on the backend installation.setPushType(PUSH_TYPE); installation.save(); PLog.d(ParseGCM.TAG, "GCM registration success"); } catch (Exception e) { PLog.e(ParseGCM.TAG, "GCM registration failed", e); jobFinished(job, true); } return null; } });