@Override
  public void onCreate() {
    super.onCreate();
    // Register your parse models here
    ParseObject.registerSubclass(Address.class);
    ParseObject.registerSubclass(Gym.class);
    ParseObject.registerSubclass(Message.class);
    ParseObject.registerSubclass(SimpleUser.class);
    ParseObject.registerSubclass(Trainer.class);
    ParseObject.registerSubclass(Review.class);
    ParseObject.registerSubclass(TrainerSlots.class);
    ParseObject.registerSubclass(BlockedSlots.class);
    Parse.enableLocalDatastore(this);
    Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);

    ParsePush.subscribeInBackground(
        "",
        new SaveCallback() {
          @Override
          public void done(ParseException e) {
            if (e == null) {
              Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
              PushService.setDefaultPushCallback(
                  getBaseContext(),
                  ChatActivity.class); // change the class where u want to go after clicking on noti
              ParseInstallation.getCurrentInstallation().saveInBackground();
            } else {
              Log.e("com.parse.push", "failed to subscribe for push", e);
            }
          }
        });

    FacebookSdk.sdkInitialize(getApplicationContext());
  }
Exemplo n.º 2
0
  public static void addCommunityToUser(final ParseObject community) {
    ParseUser user = ParseUser.getCurrentUser();
    ParseRelation<ParseObject> relation = user.getRelation(Common.OBJECT_USER_COMMUNITY_RELATION);
    relation.add(community);
    user.saveInBackground(
        new SaveCallback() {
          @Override
          public void done(ParseException e) {
            getUserCommunity(ParseUser.getCurrentUser());
          }
        });

    ParseRelation<ParseObject> communityUsers =
        community.getRelation(Common.OBJECT_COMMUNITY_USERS);
    communityUsers.add(user);
    community.saveInBackground();

    String channel = "community_" + community.getObjectId();
    ParsePush.subscribeInBackground(channel);
  }
  public void pushNotification(View view) {

    Button pushButton = (Button) findViewById(R.id.sendPushButton);

    final ProgressBar loading = (ProgressBar) findViewById(R.id.progressBarOfLoadingPush);
    loading.setVisibility(View.VISIBLE);
    if (isNetworkAvailable()) {
      try {
        pushButton.setEnabled(false);
        Random rand = new Random();
        int ramdom;
        Intent intentFromLogin = getIntent();
        String userID = intentFromLogin.getStringExtra(LoginActivity.userID);
        PushDataInfo pushDataInfo = new PushDataInfo();
        pushDataInfo.setTitle(
            userID
                + ":"
                + ((EditText) findViewById(R.id.notificationTitleEditText)).getText().toString());
        pushDataInfo.setUrl(
            ((EditText) findViewById(R.id.notificationLinkUrlEditText)).getText().toString());
        pushDataInfo.setAlert(
            ((EditText) findViewById(R.id.notificationMessageEditText)).getText().toString());
        ramdom = rand.nextInt((10000 - 1000) + 1) + 1000;
        pushDataInfo.setPushId(ramdom);
        getSharedPreferences("PUSHID", MODE_PRIVATE).edit().putInt("pushid", ramdom).apply();
        SimpleDateFormat currentDateAndTime = new SimpleDateFormat("dd/MM/yyyy \t kk:mm:ss");
        pushDataInfo.setDateAndTime(currentDateAndTime.format(new Date()));
        Gson gson = new Gson();
        JSONObject jsonObject = new JSONObject(gson.toJson(pushDataInfo));
        ParsePush push = new ParsePush();
        ParsePush.subscribeInBackground(userID);
        push.setChannel(userID);
        push.setData(jsonObject);
        push.sendInBackground();
        final Intent intentToMainActivity = new Intent(this, MainActivity.class);
        loading.setVisibility(View.INVISIBLE);
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Success");
        alertDialog.setMessage("Your notification will be sent shortly !");
        alertDialog.setButton(
            AlertDialog.BUTTON_NEUTRAL,
            "OK",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                finish();
                startActivity(intentToMainActivity);
              }
            });
        alertDialog.show();
      } catch (JSONException e) {

      }
    } else {
      pushButton.setEnabled(true);
      loading.setVisibility(View.INVISIBLE);
      AlertDialog alertDialog = new AlertDialog.Builder(this).create();
      alertDialog.setTitle("No Internet");
      alertDialog.setMessage("Check Network Connection and Try Again");
      alertDialog.setButton(
          AlertDialog.BUTTON_NEUTRAL,
          "OK",
          new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
            }
          });
      alertDialog.show();
    }
  }