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.
Related
I am using Firebas mesaging service class. and Its working properly when app is running in foreground but when i clear this application from recent i am not able to access the push notification data for setting it on textview.
I want to store data in database when app is running in background.
I had Used Database to store the value directly but its not working.
public void onMessageReceived(RemoteMessage message) {
database=new SQLiteHelper(getApplicationContext());
database.open();
String image = message.getNotification().getIcon();
String title = message.getNotification().getTitle();
String text = message.getNotification().getBody();
String sound = message.getNotification().getSound();
int id = 0;
database.addQuotes(text,"Xyz");
}
In MainActivity.java in Oncreate method
Bundle extras = getIntent().getExtras();
if (extras != null) {
final String message = extras.getString("message");
quote_text.setText(message);
Toast.makeText(this, "A", Toast.LENGTH_SHORT).show();
}
But message shows null data.
based on this:
I want to store data in database when app is running in background
The service calls which extends FirebaseMessagingService has a method called onMessageReceived which is called when a push notification is received on the front-end (app) add your message to DB there!
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
.....
// parse and add your message to Db here!
Normally when your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.
This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
But you can refer this answer on stackoverflow if you want to get notification data-messages in your app while in background.
You may also refer this
when my app in foreground then the notification url link open automatically without user's any contribution.
Now,I want to keep notification in notification bar and when user press the notification that will open the url link.
One more thing I have to mention that,when my app in background it works fine.Notification arrive in notification bar.
I want same thing to do for foreground.
please help,
Thanks in advance.
my FirebaseMessagingServices
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if(remoteMessage.getData().size()>0){
String url = remoteMessage.getData().get("url");
Intent intent = new Intent("com.bellecarib_FCM-MESSAGE");
intent.putExtra("url",url);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(intent);
}
}
here is my MainActivity code
LocalBroadcastManager.getInstance(this).registerReceiver(mHandler, new IntentFilter("com.bellecarib_FCM-MESSAGE"));
if(getIntent().getExtras() != null){
for(String key: getIntent().getExtras().keySet()){
if(key.equals(("url"))){
mwebView.loadUrl(getIntent().getExtras().getString(key));
Toast.makeText(getApplicationContext(),"Opening The Notification Page",Toast.LENGTH_LONG).show();
}
}
}
The behaviour you are seeing , is when you send notification payload from FCM. If you want a customised behaviour, then send only a data payload and when you receive the message, throw up a notification manually. This means more work for your app (at the cost of customisation). Also data payloads are delivered with a normal priority which means if your device is in idle mode, then FCM Data payload messages are not delivered immediately and deferred till the next maintenance window (Read upon Android Doze mode and App Standby).
Hope this is clear
I'm using intercom.io to send messages to my customers. I can receive gcm (with notification) from intercom just fine, ONLY if the message that I sent is the first message in a conversation. For subsequent messages in the conversation, I don't receive anything. I put a log in my onMessageReceived() but it didn't receive anything, except if the message is the first message in a conversation.
public class MyGcmListenerService extends GcmListenerService {
#Override
public void onMessageReceived(String from, Bundle data) {
MessageUtils.log("onMessageReceived data is " + data);
}
}
Any idea what am I missing?
In case you don't get what I'm trying to say, here's what I meant:
I select a customer from my intercom.io web dashboard (or
whatever it's called)
Then I click on the 'Message' button to
send a message to the customer.
The customer received my message, together with the notification.
Now I send another message to the customer within the same conversation as before.. but now the customer won't receive any more gcm message from intercom.
Yes it does support now. The github issue is closed now and they added it in 3.0.3
They have a git hub project for FCM, but it is missing few code.
The code is available on this github page and is as follows
if you are extending FirebaseMessagingService in a class in your own app? then you will need to manually pass on the push to intercom now.
private final IntercomPushClient intercomPushClient = new IntercomPushClient();
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> message = remoteMessage.getData();
if (intercomPushClient.isIntercomPush(message)) {
intercomPushClient.handlePush(getApplication(), message);
} else {
//DO HOST LOGIC HERE
}
}
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.
Is it possible to not display a push notification after the phone has actually received it?
I'm building a chatting application. Push notifications are sent every time a message is sent to user X. If user X is inside the Activity that represents the chat with user Y, and receives a message from user Y, I would like the push notification to not display.
Is this possible with regular GCM push notifications?
You can do it only with IntentService.
When a new Notification is received in your BroadcastReceiver, you will send it to the IntentService so, there before displaying Notification with NotificationBuilder have a Listener set to the Activity. If Listener exists, the user is inside the Activity, then just ignore the notifications.
For Example, define a Listener like this,
public interface PushNotificationListenerService {
public void showNewMessage();
}
And in your IntentService before displaying Notification,
public void setListener(PushNotificationListenerService listener) {
onPushReceivedCallback = listener;
}
Handler mHandler = new Handler(getMainLooper());
mHandler.post(new Runnable() {
#Override
public void run() {
if (onPushReceivedCallback != null) {
onPushReceivedCallback.showNewMessage();
// then ignore the notification.
}
else{
// show notification
}
});
In you Activity,
onCreate Method,
NotificationIntentService.getInstance(this).setListener(this);
The messages received through GCM are processed by your IntentService which will be triggered each time a new message is received, but it's completely up to you what do you do with that message.
If you plan to act in base of an active/inactive Activity, I'd suggest declaring a global boolean that will be true when onCreate() or onResume() is called, and set to false when onDestroy() or onPause() are called. So if you receive a message, you see what value it has and act accordingly to its current state.