public static void registerDeviceForPushNotifications(final Context ctx, String token) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx); String uuid = settings.getString(WPCOM_PUSH_DEVICE_UUID, null); if (uuid == null) return; String deviceName = DeviceUtils.getInstance().getDeviceName(ctx); Map<String, String> contentStruct = new HashMap<String, String>(); contentStruct.put("device_token", token); contentStruct.put("device_family", "android"); contentStruct.put("app_secret_key", NotificationUtils.getAppPushNotificationsName()); contentStruct.put("device_name", deviceName); contentStruct.put("device_model", Build.MANUFACTURER + " " + Build.MODEL); contentStruct.put("app_version", WordPress.versionName); contentStruct.put("os_version", android.os.Build.VERSION.RELEASE); contentStruct.put("device_uuid", uuid); com.wordpress.rest.RestRequest.Listener listener = new RestRequest.Listener() { @Override public void onResponse(JSONObject jsonObject) { AppLog.d(T.NOTIFS, "Register token action succeeded"); try { String deviceID = jsonObject.getString("ID"); if (deviceID == null) { AppLog.e( T.NOTIFS, "Server response is missing of the device_id. Registration skipped!!"); return; } SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx); SharedPreferences.Editor editor = settings.edit(); editor.putString(WPCOM_PUSH_DEVICE_SERVER_ID, deviceID); JSONObject settingsJSON = jsonObject.getJSONObject("settings"); editor.putString(WPCOM_PUSH_DEVICE_NOTIFICATION_SETTINGS, settingsJSON.toString()); editor.commit(); AppLog.d(T.NOTIFS, "Server response OK. The device_id : " + deviceID); } catch (JSONException e1) { AppLog.e(T.NOTIFS, "Server response is NOT ok. Registration skipped!!", e1); } } }; RestRequest.ErrorListener errorListener = new RestRequest.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { AppLog.e(T.NOTIFS, "Register token action failed", volleyError); } }; WordPress.getRestClientUtils() .post("/devices/new", contentStruct, null, listener, errorListener); }
public static void setPushNotificationSettings(Context context) { if (!WordPress.hasValidWPComCredentials(context)) { return; } String gcmToken = GCMRegistrar.getRegistrationId(context); if (TextUtils.isEmpty(gcmToken)) { return; } SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); String deviceID = settings.getString(WPCOM_PUSH_DEVICE_SERVER_ID, null); if (TextUtils.isEmpty(deviceID)) { AppLog.e(T.NOTIFS, "device_ID is null in preferences. Set device settings skipped."); return; } String settingsJson = settings.getString(WPCOM_PUSH_DEVICE_NOTIFICATION_SETTINGS, null); if (settingsJson == null) return; Gson gson = new Gson(); Map<String, StringMap<String>> notificationSettings = gson.fromJson(settingsJson, HashMap.class); Map<String, Object> updatedSettings = new HashMap<String, Object>(); if (notificationSettings == null) return; // Build the settings object to send back to WP.com StringMap<?> mutedBlogsMap = notificationSettings.get("muted_blogs"); StringMap<?> muteUntilMap = notificationSettings.get("mute_until"); ArrayList<StringMap<Double>> blogsList = (ArrayList<StringMap<Double>>) mutedBlogsMap.get("value"); notificationSettings.remove("muted_blogs"); notificationSettings.remove("mute_until"); for (Map.Entry<String, StringMap<String>> entry : notificationSettings.entrySet()) { StringMap<String> setting = entry.getValue(); updatedSettings.put(entry.getKey(), setting.get("value")); } if (muteUntilMap != null && muteUntilMap.get("value") != null) { updatedSettings.put("mute_until", muteUntilMap.get("value")); } ArrayList<StringMap<Double>> mutedBlogsList = new ArrayList<StringMap<Double>>(); for (StringMap<Double> userBlog : blogsList) { if (MapUtils.getMapBool(userBlog, "value")) { mutedBlogsList.add(userBlog); } } if (updatedSettings.size() == 0 && mutedBlogsList.size() == 0) return; updatedSettings.put( "muted_blogs", mutedBlogsList); // If muted blogs list is unchanged we can even skip this assignment. Map<String, String> contentStruct = new HashMap<String, String>(); contentStruct.put("device_token", gcmToken); contentStruct.put("device_family", "android"); contentStruct.put("app_secret_key", NotificationUtils.getAppPushNotificationsName()); contentStruct.put("settings", gson.toJson(updatedSettings)); WordPress.getRestClientUtils().post("/device/" + deviceID, contentStruct, null, null, null); }