Reputation: 139
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
Upvotes: 2
Views: 79
Reputation: 1055
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.
Upvotes: 1
Reputation: 4007
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/
Upvotes: 0