Parse - Notification not being received - android

I am trying to send a Parse Push Notification from one Android application to all others.
The following is the set-up code in my Application object:
Parse.enableLocalDatastore(this);
ParseObject.registerSubclass(Game.class);
Parse.initialize(this, "code", "code");
ParsePush.subscribeInBackground(ParseHelper.SUBSCRIPTION_CHANNEL_GAME);
The following is the Push Notification code:
ParsePush push = new ParsePush();
String message = "Hello";
push.setChannel(ParseHelper.SUBSCRIPTION_CHANNEL_GAME);
push.setMessage(message);
push.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Toast.makeText(CreateGameActivity.this, "Success", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(CreateGameActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
}
});
break;
}
Even though the Success Toast is called, I still don't see the Notification appearing on any of the two Android devices I have installed the app on.
I have tested the Push Notifications via www.parse.com's Dashboard and that does work. Why won't it work in my app though?

To send notifications from a device, you have to do one extra step. Go into the settings of your app on parse and enable Client Push. That should resolve your issue.

Related

android firebase reg id recived but notification not show in phone display

I will try to show personal single notification on my phone tray, but I can't rich, so help.
I am having an issue with FireBase Cloud Messaging in which I get the Token from the device and send the notification test through the Google Firebase notification console, however, the notification is never logged nor pushed to the android virtual device. The documentation for FCM is almost exactly the code that I have below and little else in the way of what else you would have to do to get push notifications working with firebase. I have gone through all of the setup information (build.gradle additions, Installing google play services, etc...) as specified in the documentation, but still do not have messages generating. What is wrong with the code that I am not receiving my push notifications to the logcat or the device? Please let me know any further information that would be helpful. Thanks.
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION))
{
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
displayFirebaseRegId();
}
private void displayFirebaseRegId() {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
String regId = pref.getString("regId", null);
Log.e(TAG, "Firebase reg id: " + regId);
if (!TextUtils.isEmpty(regId))
txtRegId.setText("Firebase Reg Id: " + regId);
else
txtRegId.setText("Firebase Reg Id is not received yet!");
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.REGISTRATION_COMPLETE));
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.PUSH_NOTIFICATION));
NotificationUtils.clearNotifications(getApplicationContext());
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
And I will add lib of fire base messaging is:
compile 'com.google.firebase:firebase-messaging:11.0.4'
You don't need to subscribe inside the BroadcastReceiver you can just do it inside the onTokenRefresh method in the FirebaseInstanceIdService
You don't need to get the push notification in the BroadcastReceiver, you have to do it inside the onMessageReceive in the FirebaseMessagingService
FCM is extremely unreliable with emulators, simply use a real device, I have struggled with this and in some cases I even get the notification days later when opening the emulator for other projects, test this with real phones

Unable to receive push notification from fcm in android

I am trying to send push notification from FCM .But unable to receive it in my android device.
At first you can see in the image that it's not getting initialized but later the initialization is successful.
I am able to get the FCM registration id but unable to get the push message .
here is the screenshot:
OnReceive Method :
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();
txtMessage.setText(message);
}
}
};
I am using this example of AndroidHive and I have also tried to test it on different devices with different versions.
if anyone can give some idea then it will be very helpful to me.

Android Parse push notifications not received even when subscribed

I have seen this question, and although my problem seems similar, I have everything set up properly, here's the relevant java:
Parse.initialize(this, "MY_KEY", "MY_KEY");
ParseInstallation.getCurrentInstallation().saveInBackground();
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
// callback to confirm subscription
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);
}
}
});
Anyways, looking at my dashboard on parse.com the subscription is being recognized and when I attempt to send the push it says "sending to 2 devices" (my phone and a virtual machine). It then shows up with a green check mark on the list of sent pushes BUT under "pushes sent" it says 0.
It also seems that sometimes, deviceToken and pushType are not being set properly, but there hasn't been any difference in effect whether these are present are not, although I'm assuming they are necessary.
How is not sending any pushes to subscribed devices a success?
My manifest can be found here
Considering that all the XML manifest is correct, try this:
> Parse.initialize(this, "MY_KEY", "MY_KEY");
> ParseAnalytics.trackAppOpenedInBackground(getIntent());
> ParsePush.subscribeInBackground("");//Optionaly put your callback
> PushService.startServiceIfRequired(getApplicationContext());
>
> ParseInstallation.getCurrentInstallation().saveInBackground();

Sinch onShouldSendPushData takes few seconds to get fired

I've developed an instant messaging app following these tutorials:
https://www.sinch.com/tutorials/android-messaging-tutorial-using-sinch-and-parse/
and
https://www.sinch.com/tutorials/send-push-notifications-android-messaging-app-using-gcm/
Everything is working fine. From a user phone I've tried to send an instant message to another user in which the MessageService is stopped (on his device). The user gets the notification.
This is the implementation of my method:
#Override
public void onShouldSendPushData(MessageClient client, Message message, List<PushPair> pushPairs) {
final WritableMessage writableMessage = new WritableMessage(message.getRecipientIds().get(0), message.getTextBody());
ParseQuery userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("objectId", writableMessage.getRecipientIds().get(0));
ParseQuery pushQuery = ParseInstallation.getQuery();
pushQuery.whereMatchesQuery("idutente", userQuery);
// Send push notification to query
ParsePush push = new ParsePush();
push.setQuery(pushQuery); // Set our Installation query
push.setMessage("sent you a message");
push.sendInBackground(new SendCallback() {
#Override
public void done(ParseException e) {
if(e==null){
}else{
e.printStackTrace();
}
}
});
}
The problem is the following :
if I'm in the MessagingActivity and I'll write a message and I quit the activity before onShouldSendPushData gets called (for instance a write a quick message and then I click to the back button to exit the application) the push notification is never fired (obviously because onShouldSendPushData is not called in time).
Is there a way to wait for onShouldSendPushData to get fired before closing the app? Is there a listener or some property that I can check in order to prevent the closing of the activity before onShouldSendPushData gets called?
Thank you,
Andrea
Not really, you should probably just make a progress indicator, but if you kill the app there is not way for us to now this. One way of implementing this is to have a "Service" pattern in you app that is always running, so instead of handling everything in the MessagingActivity. Then you could continue to run in the background.

Parse Push Notifications Android to appear once the app is opened

I have done a lot of reading on Parse push notifications. I have push notifications working perfectly if the app is running or sitting in the background (user gets out of the app by pushing the home button or back button). I know I cannot get notifications to appear if the app has been force quit but if I close the app/force quit the app, is there a way to get the notifications to pop up the next time the app opens?
I have implemented MainApplication and made sure my manifest is correct but if the app is completely closed, then I do a push notification from Parse and then go to open the app it is as if it has not received it and never will display. Is that normal behavior?
Thanks,
Adam
Main Application
public class MainApplication extends Application {
private static MainApplication instance = new MainApplication();
public MainApplication()
{
instance=this;
}
public static Context getContext()
{
return instance;
}
#Override
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
//initialise whatson from parse.com
Parse.initialize(this, "xxxxx", "xxxxx");
PushService.setDefaultPushCallback(this, MainActivity.class);
//to register device to get push notifications and register the install
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);
}
}
});
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}

Categories

Resources