Send device to device push notification using parse.com - android

I am new to parse. I have created an android application in which I have to send the push notification by click on the send button. I have successfully stored data to its parse database like Object Id, device type, device token, push type (which is created by default under the installation class).
I have also add some custom column under installation class like age, friend id, gender and retrieve the correspondence Object id in the edit text.
But when I try to send push notification by click on send button, I usually get this error:
com.parse.Parse Exception: Clients aren't allowed to perform the find operation on the installation collection
The query which I am using to send push notification with object-id corresponding to the given Friend-id is:
sndpush.setOnClickListener(new OnClickListener() {
#SuppressWarnings("unchecked")
#Override
public void onClick(View v) {
JSONObject obj;
try {
obj =new JSONObject();
obj.put("alert","erwerwe");
obj.put("action","com.parse.pushnotifications.UPDATE_STATUS");
obj.put("customdata","My string");
ParsePush push = new ParsePush();
ParseQuery query = ParseInstallation.getQuery();
query.whereEqualTo("objectId",objectidEditText.getText().toString() );
push.setQuery(query);
push.setData(obj);
push.sendInBackground();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
});
I do google and R&D and I get the idea that we have no permission to send push by get the object id from default class that is installation class.
I have created a new class in parse.com where I have insert some data like age, friend-id and also able to retrieve object-id corresponding to friend id. But when ever I tried to send push notification by supplying Object-id in it, I get the error:
java.lang.IllegalArgumentException: Can only push to a query for Installations
Here is the code:
// Get the user's Id from parse.com.
#SuppressWarnings({ "rawtypes", "unchecked" })
public void getObjectId() {
#SuppressWarnings("rawtypes")
ParseQuery query = new ParseQuery("_installation");
query.whereEqualTo("FriendId", FrndTextString);
query.getFirstInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
Log.d("objectId", "The getFirst request failed.");
} else {
String objectid = object.getObjectId();
objectidEditText.setText(objectid);
sndpush.setEnabled(true);
Log.d("objectId", "Retrieved the object.");
}
}
});
}
I have followed the link from stack overflow to send push notification to a specific user. But there is no proper result.
send push notification with parse to single device
Please tell me where I am wrong. My main question is how can I send a push notification from one device to another.
Here is my complete code:
http://piratepad.net/ep/pad/view/ro.WwZ9v6FX1a6/latest

Related

Send push notification to other user in publish chat group quickblox

i'm using quickblox SDK in my project .
And i have a question about that.
Plz advise
I want to send push notification to other user in PUBLIC chat group.
I can do it with private chat by creating an event and sent to friend like this
public void createEvent(String message, List<Integer> userIds) {
StringifyArrayList<Integer> userIds_ = new StringifyArrayList<Integer>(userIds);
QBEvent event = new QBEvent();
event.setUserIds(userIds_);
event.setEnvironment(QBEnvironment.PRODUCTION);
event.setNotificationType(QBNotificationType.PUSH);
HashMap<String, Object> messageData = new HashMap<>();
messageData.put("message", message);
messageData.put("firID", KApp.getInstance().getCurrentUser().getId());
messageData.put("name", KApp.getInstance().getCurrentUser().getName());
event.setMessage(messageData);
QBPushNotifications.createEvent(event).performAsync(new QBEntityCallback<QBEvent>() {
#Override
public void onSuccess(QBEvent qbEvent, Bundle bundle) {
System.out.print("Create event success");
System.out.print(qbEvent.toString());
}
#Override
public void onError(QBResponseException e) {
System.out.print("Create event error : " + e.getLocalizedMessage());
}
});
}
But with PUBLIC chat dialog , i can't get list user id, it alway return empty.
How can i send push notification to other member in PUBLIC group ?
There is no sense in sending pushes to PUBLIC_GROUP because all users can chatting in public group chat. If you need notify occupants of dialog, use GROUP dialog. Note: in PUBLIC_GROUP you will also not receive pushes about messages when user is offline because PUBLIC_GROUP dialog not contain occupants_ids.
PUBLIC_GROUPS are open group. The difference between GROUP and PUBLIC_GROUP is that it does not keep associated participant's records like user_ids or unread_count . You have to store your participants data in your own backend server or else locally.

Parse.com Receiving push notifications even after Logout -Android

I've made an app that uses Parse to send push notifications to other devices when the user is logged into his account. The problem is that the users still receive pushes even after they've logged out.
A method to fix this would be awesome.
Here's the code i use to register the device.
public void registerPushNotification() {
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
installation.put("user", mCurrentUser);
installation.saveInBackground();
}
Thanks in advance!
Try this (code is in Swift, but I think it's the same)
PFUser.logOut()
PFInstallation.currentInstallation().removeObjectForKey("user")
PFInstallation.currentInstallation().saveInBackground()
Try by calling the ParseInstallation.getCurrentInstallation().deleteInBackground() at the time of logout.
I suggest using channels, with channels you can get some sort of control of pushes and so on.
The method signature is like this
ParsePush.subscribeInBackground("Name of my channel", SaveCallback cb);
Try using this when the user logs in:
ParsePush.subscribeInBackground("my_channel", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// Great
} else {
// Something wrong happened...
}
}
});
Use this when user logs out:
ParsePush.unsubscribeInBackground("my_channel");
When sending pushes query for all users who (are still) subscribed to the "my_channel" channel:
ParsePush push = new ParsePush();
push.setChannel("my_channel");
push.setMessage("Message to the subscribers of the channel!");
push.sendInBackground();
Make sure to check out Parse guide and docs.
Cheers!

Sending push notification using Parse SDK on Android

I am trying to do ChatApp. I can do talking people each other. But there is an issue. I want to use push notification when a message sended to any user.
So i am adding these information. I have these classses on Parse.
Intallation , Session , User, Chat.
I am using User class for users , Chat class for each conversation for example someone senf a message another one , i am adding an object my chat class.
So I dont know how can i send push notification. I guess i should use User objectId to finding buddy. And here i am adding my sendMessage method:
private void sendMessage() {
if (txt.length()==0)
return;
InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txt.getWindowToken(), 0);
String s=txt.getText().toString();
final Conversation c=new Conversation(s,new Date(),UserList.user.getUsername());
c.setStatus(Conversation.STATUS_SENDING);
convList.add(c);
adp.notifyDataSetChanged();
txt.setText(null);
ParseObject po=new ParseObject("Chat");
po.put("sender",UserList.user.getUsername());
po.put("receiver",buddy);
po.put("message",s);
po.saveEventually(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null){
c.setStatus(Conversation.STATUS_SENT);
}
else {
c.setStatus(Conversation.STATUS_FAILED);
}
adp.notifyDataSetChanged();
}
});
}
Use the below code to send the push notification:
public void sendpushnotification(String buddyObjectId,String message,String type,String chatObjectID){
ParseQuery query = ParseInstallation.getQuery();
query.whereEqualTo("owner",owner);
ParsePush push = new ParsePush();
push.setQuery(query);
try {
JSONObject data = new JSONObject("{\"action\": \"{Action name}\",\"alert\":\""+message+"\",\"mid\":\""+chatObjectID+"\",\"pid\":\""+chatObjectID+"\",\"t\": \""+type+"\"}");
push.setData(data);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
push.sendInBackground();
}
Where buddyObjectId is buddy object id
chatObjectID is chat message object id after saving
type is type of message if any.
Also make sure to save user object id in owner column of the installation table when a user is created.

How to avoid a user receiving his own chat gcm notification on Pubnub

We have created a chat application using pubnub api on phonegap. We are using a common unique channel for two users (say A & B) to send and receive messages. Also, in addition to this, we have enabled pubnub gcm notification so that users can receive notifications when their app is either in the background or closed. Now when a user A sends a pubnub message to B and instantly changes the application or hides it, then the user A himself also receives a gcm notification oh his own message. This is the only issue that is troubling us. Rest all is working fine.
In your subscribe callback, can you detect if the application is not in the foreground, and if not in the foreground, return immediately?
This way, the logic which would deliver received messages to your application logic simply doesnt fire unless you are in foreground... ?
I ran into the same similar problem you did and so I rigged something together that works for me.
private boolean isFromSelf(final Object msgSent){
boolean isEcho = false;
String msgText = msgSent.toString();
JSONObject j;
String receiver = currentUserId; //a unique string ID for each user
try
{
j = new JSONObject(msgText);
String senderID = j.get("senderID").toString();
if (senderID.equals(receiver)){
isEcho = true;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return isEcho;
}
It makes the assumption that every user that publishes a message to a channel will send their unique Id as "senderID" included in the payload. Then when a message is received, extract it from the payload and check it against the user's Id. So I add isFromSelf method as the first thing that is called when I receive a message, like this:
pN = new Pubnub(pub_key, sub_key);
try {
pN.subscribe(CHANNEL_NAME, new Callback() {
public void successCallback(String channel, Object msg) {
if (!isFromSelf(msg)) {
//Now add here what you want to do with the message
}
}
public void errorCallback(String channel, PubnubError error) {
Toast.makeText(MainActivity.this, "there was an error connecting", Toast.LENGTH_SHORT).show();
}
});
} catch (PubnubException e) {
e.printStackTrace();
}
So in your case, perhaps rig together a senderID check so that if you do receive a GCM from yourself, do not display notification at all, even if app is in background.

can't send push notification from a device-parse

I'm trying to add the feature of push notifications in my app and i did everything correctly according to many guides but the push is never being sent (i don't see it in the push list in parse). On the contrary, when i send a push from parse to a specific channel( which is exactly what i want to do from the device), i do get a message on devices which subscribed to that channel.
doneBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParsePush push = new ParsePush();
push.setQuery(query);
push.setChannel(listId);
push.setMessage("Hey pal"+", it looks like "+cur_user.getUsername()+" added you to a new list."+" Log in to check it.");
push.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if(e == null){
Log.d("DONE BUTTON","im here");
onBackPressed();
}else{
Log.d("PUSH ERROR",e.toString());
}
}
});
}
});
//update the list on the screen
updateData();
}
I literally have no idea why it doesn't work.
listId which sent to setChannel is a valid channel (I checked that) and it is included in the channels column in several installations in parse among other channels (i mean, i have installations with more than one channel and i want to send this message onlt to listId channel).

Categories

Resources