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
Related
I've been following the Set Up a GCM Client App tutorial and tried to understand the demo app they provided, but I couldn't understand how to send push notification with this service.
The above guide leads me to a "Generating InstanceID token" screen with never-ending ProgressBar. This program source can be obtained here: https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main
After using the above source code I've tried next to Simple Downstream messaging, this: https://developers.google.com/cloud-messaging/downstream in order to send push to the app users.
I couldn't understand how I'm supposed to send my app users notifications with it.
I have a server and I'm great with PHP, What is left to do in order to send push notification to my application users using the GCM?
Finally managed to get the message to a TextView!
How do make it into push?
public class GcmService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
JSONObject jsonObject = new JSONObject();
Set<String> keys = data.keySet();
for (String key : keys) {
try {
jsonObject.put(key, data.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
sendNotification("Received: " + jsonObject.toString(5));
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onDeletedMessages() {
sendNotification("Deleted messages on server");
}
#Override
public void onMessageSent(String msgId) {
sendNotification("Upstream message sent. Id=" + msgId);
}
#Override
public void onSendError(String msgId, String error) {
sendNotification("Upstream message send error. Id=" + msgId + ", error" + error);
}
private void sendNotification(final String msg) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
#Override
public void run() {
if (MainActivity.mTextView != null) {
MainActivity.mTextView.setText(msg);
}
}
});
}
}
Let's assume you have created a new project at Google Developers Console and taken note of 2 values: Project Number, which will be used as SENDER_ID in a client project; and API server key (created at Credentials), which will be used as API_KEY in a server project.
You can find more about basic server project with my answers at the following questions. In the second link, you will find sample code for a server project in Java, then you can refer to its logic to implement in your PHP project:
Adding Google Cloud Messagin (GCM) for Android - Registration process
How to implement a GCM Hello World for Android using Android Studio
Hope this helps!
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.
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've started using parse.com to receive push notifications in my application.
It works perfectly, but I have a couple of questions.
We perform registration, but unused channels, as follows:
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("dmode", "Oks!");
} else {
Log.e("dmode", "Fail :(", e);
}
}
});
My first question is:
I find no way to disable receiving push notifications
I searched the official documentation and stackoverflow, but I find solutions that do not work for me.
I tried:
ParsePush.unsubscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("dmode", "unsuscribe oks");
} else {
Log.e("dmode", "unsuscribe fail", e);
}
}
});
Unsuccessfully, I have also tried:
ParsePush.unsubscribeInBackground("");
...but not work.
I'm not using channels, that is why the quotes are empty:
ParsePush.subscribeInBackground("", new SaveCallback() { //...
How I can enable and disable push notifications?
The second query is that sometimes receive duplicate notifications until two three times at once on the same device.
Someone has been the same?
Thank you very much for the help.
Greetings!
I think you should use a name for a default channel even if you are not using channels and not the empty string "".
Also have a look here Parse Question
I quote from the answer on the above link
If you called PushService.setDefaultCallback, call it again passing null as the class.
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!!