Send a Message with a push notification when the user is not in the chat window

Hi,

I’ll try to explain my problem, I’m sending a message to a channel with a subtopic successfully however i would like to send it with a push notification, i was also able to do that with no problems, on the other hand i have a small problem with the fact that, even when the receiver is currently on the chat window and he will actually be able to see that message in a second, the message is still being sent with the push notification. Is there any method to control this? i’ve tried with the message status wich is always “scheduled” in the handleResponse method so i can’t really control it over there. My main purpose is for the message to be sent through a push notification if the receiver is not currently on the chat window. Any suggestions? i already thought about modifying some server code and use the afterPublish method, but i’m wondering if there is another easier way? i’m attaching the method that i use for sending the message.
Thanks in advance guys.
publishOptions = new PublishOptions();
publishOptions.setPublisherId( FstUserName);
publishOptions.setSubtopic( subtopic );
publishOptions.putHeader( PublishOptions.ANDROID_TICKER_TEXT_TAG, String.format( ConstantsClass.MESSAGE_SEND, MainActivity.user.getName() ) );
publishOptions.putHeader( PublishOptions.ANDROID_CONTENT_TITLE_TAG, getResources().getString( R.string.app_name ) );
publishOptions.putHeader( PublishOptions.ANDROID_CONTENT_TEXT_TAG, String.format( ConstantsClass.MESSAGE_SEND, MainActivity.user.getName() ) );private boolean onSendMessage( int keyCode, KeyEvent keyEvent )
{
DeliveryOptions deliveryOptions = new DeliveryOptions();
deliveryOptions.setPushPolicy( PushPolicyEnum.ALSO );
deliveryOptions.addPushSinglecast( UsrsDeviceIDs);
if( keyCode == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_UP )
{
String message = messageField.getText().toString();

    if( message == null || message.equals( "" ) )
        return true;

    Backendless.Messaging.publish( (Object) message, publishOptions,deliveryOptions, new DefaultCallback<MessageStatus>( ChatActivity.this, "Sending..." )
    {
        @Override
        public void handleResponse( MessageStatus response )
        {
            super.handleResponse( response );
            PublishStatusEnum messageStatus = response.getStatus();

            if( messageStatus == PublishStatusEnum.SCHEDULED )
            {
                messageField.setText( "" );
            }
            else
            {
                Toast.makeText( ChatActivity.this, "Message status: " + messageStatus.toString(), Toast.LENGTH_SHORT );
            }
        }
    } );

    return true;
}
return false;

}

Any help with this topic? any suggestion that will help? i’m kind of stuck with this :confused: anyway to run a background service in order to get messages even when the application is off?

Hi Michael,
Please upgrade your Android SDK to latest and follow documentation on implementing Push with Service https://github.com/Backendless/Android-SDK/blob/master/docs/push.md
You also can use sample project https://github.com/Backendless/Android-SDK/tree/master/samples/MessagingService/pushdemo as example.

Backendless Push Service now leverage Anrdoid`s wake lock and pushes are received even if phone is in sleep.
Hope that helps.

Artur

Hi, Michael!
There is no way to send notification only as pubsub message. So, you should handle this case on the device inside “onMessage” method. Just check if app is active inside it, and if it’s not - show notification, otherwise do nothing.

regards,
Alex

Thanks Alexandr, i’m very grateful for your answer. It was really helpful. i already solved this problem and moved on. Thanks once again.
Kind Regards
Michael