Search code examples
androidparse-platforminstant-messagingsinch

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


Solution

  • 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.