2

here i am really not getting why my code is not sending push from a user to another user ,i can send and receive pushes by sending through channels but problem is i get push notification to all user, not the specific user ,how can i send from a user to another user here below is my code please explain me what is my mistake ,or how to me it work

MY onClick of button where things must happen

ok.setOnClickListener(
            new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {

                    final String currentUserId = ParseUser.getCurrentUser().getObjectId();
                    ParseQuery<ParseUser> query = ParseUser.getQuery();
                    getPhone = phone.getText().toString();

                    //for not including myself
                    query.whereNotEqualTo("objectId", currentUserId);
                    query.whereEqualTo("username", getPhone);
                    query.getFirstInBackground(new GetCallback<ParseUser>()
                    {
                        public void done(final ParseUser user, ParseException e)
                        {
                            if (user == null)
                            {
                                Toast.makeText(Welcome.this, "couldnot connect to " + getPhone, Toast.LENGTH_SHORT).show();
                                Log.d("username", "problem retriving username");
                            }
                            else
                            {
                                ParseQuery pushQuery = ParseInstallation.getQuery();
                                pushQuery.whereEqualTo("email", "three");
                                 final String name = user.getUsername();

                                String data = "{\n" +
                                        "\"data\":{\n "+
                                                "\"message\":\"Connection request\",\n" +
                                                "\"title\":\"Connection\",\n" +
                                                "\"from\":"+"\""+ParseUser.getCurrentUser().getUsername()+"\""+"\n "+
                                                "}\n" +
                                          "}";
                                JSONObject jsonObject = null ;
                                try
                                {
                                    jsonObject = new JSONObject(data);
                                } catch (JSONException e1)
                                {
                                    e1.printStackTrace();
                                }


                                ParsePush push = new ParsePush();
                                push.setQuery(pushQuery);
                                push.setData(jsonObject);
                                //push.setChannel("Giants");
                                push.sendInBackground(new SendCallback()
                                                      {
                                                          @Override
                                                          public void done(ParseException e)
                                                          {

                                                              if (e == null)
                                                              {
                                                                  Toast.makeText(getApplicationContext(), "request send to "+name, Toast.LENGTH_SHORT).show();
                                                              }
                                                              else
                                                              {
                                                                  Toast.makeText(getApplicationContext(), "problem sending request to "+name+" due to "+e.getMessage(), Toast.LENGTH_SHORT).show();
                                                              }

                                                          }
                                                      }
                                );
                                Intent i = new Intent(Welcome.this, TestActivity.class);
                                Log.e("about user", "connected to " + user.getUsername());
                                retrivedUser = user.getUsername();
                                i.putExtra("number", retrivedUser);
                                startActivity(i);
                            }
                        }
                    });

My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!--
  IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
  to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
    android:name="com.parse.starter.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.chattapp.permission.C2D_MESSAGE" />


<application
    android:name=".Chattapp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" />
    <activity android:name=".DispatchActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".LoginActivity" />
    <activity android:name=".SignUpActivity" />
    <activity android:name=".Welcome" />
    <activity android:name=".TestActivity"></activity>

    <service android:name="com.parse.PushService" />
    <!--<receiver android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>-->
   <receiver android:name="com.example.chattapp.CustomReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>
   <receiver android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />


              <!--IMPORTANT: Change "com.parse.starter" to match your app's package name.-->

            <category android:name="com.example.chattapp" />
        </intent-filter>
    </receiver>

</application>

here is the push image

push details

0

2 Answers 2

1

This is what I did for a chat app I made, every time a user sings up on your app, suscribe it to a channel with their user id. That way you will have an unique channel for every user and you can send individual pushes to them.

Here is a quick code I put up:

        ParseUser user = new ParseUser();
        user.setUsername("my name");
        user.setPassword("my pass");
        user.setEmail("[email protected]");

        // other fields can be set just like with ParseObject
        user.put("phone", "650-253-0000");

        user.signUpInBackground(new SignUpCallback() {
          public void done(ParseException e) {
            if (e == null) {
                String userID = user.getObjectId();
                ParsePush.subscribeInBackground(userID});
            } else {
              // Sign up didn't succeed. Look at the ParseException
              // to figure out what went wrong
            }
          }
        });

After this you just need the userid of the receiver and that's it.

Sign up to request clarification or add additional context in comments.

12 Comments

I'm adding parse to an app I've made also to try and send data from user to user. Would it be the same way you explained? I'd like one user to send a push notification to another user of their choosing and that user click on the push notification and save the data that the sender sent?
@Nick G and what must be the query for getting the user example pushQuery.whereEqualTo(?,?), and what channel to set in pust.setChannel(?); ,please help me for this one
@SmiffyKmc You can use the above code to subscribe the user to their unique channel, so you can send push notifications specifically to that person. You cannot send data in push notification per se, what you can do is send a Json push with a flag that identifies the data you want to send, then in the receiver end create a view or a class that handles the downloading of the data.
@user2724733 Just send the push like this ParsePush push = new ParsePush(); push.setChannel("TheUserIDOfTheUserYouWantToSendThePushTo"); push.setMessage("Your message"); push.sendInBackground();
@Nick G thanks nick solved all my problems,and push is working like charm
|
0

Is client push enabled in the parse application settings ? It's disabled by default. More infomation can be found at the parse blog:

http://blog.parse.com/learn/engineering/the-dangerous-world-of-client-push/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.