I'm developing an app for Android,which have a push notification system that uses Parse (https://www.parse.com).
I've already setup the Parse on my app exactly as they instructs on their tutorials, and it is working - if I send the notifications "manually" from the Parse panel, it is received with no problems.
But when I try to send the push notifications from inside the app, the pushes never gets to it's destination channel, and it does not show up in the Parse logs either.
Take a look on my code:
public class Application extends android.app.Application {
public Application() {
}
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "MY_CODE", "MY_OTHER_CODE");
ParsePush.subscribeInBackground("");
ParsePush push = new ParsePush();
push.setChannel( "my_example_channel" );
push.setMessage( "Test message" );
push.sendInBackground( new SendCallback() {
#Override
public void done(com.parse.ParseException e) {
if (e == null) Log.d("push", "success!");
else Log.d("push", "failure");
}
});
}
The callback returns null - no errors are shown and it logs "success", and the other mobile that I'm using to test the app is subscribed to the "my_example_channel" - it receives notifications if I send them through the Parse Dashboard.
I've also tried to put the "Push send code" (the previous code, without the "Parse.initialize..." and "parsePush.subscribeInBackground..." parts) on a Button click, and on an "onCreate" method of an Activity, but the result is the same.
I believe I solved it! It seems that you have to enable Client Side Push in your Parse Settings. Settings > Push > Client push enabled? >Yes. I was struggling with it all last night!!
Related
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!
I am developing an Android application and using Parse for push notification. Currently I have a problem when registering new device. I've used the code below to subscribe my Android phone on a specific channel on Parse server.
final String channel = "myapp_" + userId;
ParsePush.subscribeInBackground(channel, new SaveCallback() {
#Override
public void done(ParseException e) {
if (e != null) {
e.printStackTrace();
ParsePush.subscribeInBackground(channel, new SaveCallback() {
#Override
public void done(ParseException e) {
Log.e("Subscribe", e == null ? "Success" : "Failure");
}
});
} else {
Log.e("Subscribe", "Success");
}
}
});
I have a strange problem on this:
The device can be registered to Parse for the 1st time after installing apps to phone (my device has already subscribed a channel successfully) but the GCM does not return the device token. Even after I wait for a long time, but nothing good happen. Unless I quit and re-open the apps, then GCM returns device token. I don't know where is the difference between them: the 1st time using apps vs reopen apps.
Have anyone got this problem? And can you give me any suggestion to solve this?
I found the reason: the Parse registration must be proceed at onCreate action of base Application class. Then the deviceToken will be retrieved normally on Parse server.
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.
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).
I'm looking for some help with push notifications on Android.
Situation:
I'm trying to do a push send from an Android device, at this time for itself ( will understand later) but when I hit the send button nothing happens
Config:
Firstly, yes, the client side push is enabled.
Secondly my onCreate method in the "extends Application" class
public void onCreate() {
super.onCreate();
//Parse
//Parse.enableLocalDatastore(this);
Parse.initialize(this, "", "");
ParseObject.registerSubclass(Groups.class);
ParseObject.registerSubclass(Users.class);
//Subscribe to push
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
});
// Save the current Installation to Parse.
ParseInstallation.getCurrentInstallation().saveInBackground();
}
My button to send the push notification ( its the basic code from the docs )
sendReqBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Debug push onclick");
ParsePush push = new ParsePush();
push.setChannel("");
push.setMessage("The Giants just scored! It's now 2-2 against the Mets.");
push.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
System.out.println("Push send done");
}
});
}
});
All other settings set by the basic android tutorial (Like manifest and basic sdk adding, I already use other parse functions)
So after this code snippets added to my app I hoped that I will get a notification on the device itself as described earlier, but nothing happens. From the parse push dashboard I tried it, and it works fine (thats why I think the manifest and other things are configured well) and in the core panel on the dashboard i can see the installation in the list and the "" mark in the channels array.
Can anyone help me out?
Thanks in advance!!
Steve