简体   繁体   中英

Android XMPP Smack receive message when application is close

I know how to receive message using XMPP smack library, But is it possible to receiving message even after application is closed. Like whats app we are receiving message even after application close.

Currently I am using below code to receive message, What should i change to receive messages when application close.

 PacketFilter filter = new MessageTypeFilter(Message.Type.chat);

            // Listener for incoming message from any user

            connection.addPacketListener(new PacketListener() {
                public void processPacket(Packet packet) {
                    final Message message = (Message) packet;
                    if (message.getBody() != null) {
                        fromName = StringUtils.parseBareAddress(message
                                .getFrom());
                        Log.i("XMPPClient", "Got text [" + message.getBody()
                                + "] from [" + fromName + "]");     


                                }
                            }
                        });

                    }
                }
            }, filter);
  1. You need to maintain persistent tcp connection to your server, which will receive all messages in the background. Android Service will help you. Writing background services is a bit tricky, because it is too easy to make you service preventing cpu from sleep and draining user battery.
  2. You may use existing well-designed Google Cloud Messaging service. In that case it will not cost you additional cpu/battery time, but you need modifications on your server-side: when recipient is not connected - XMPP server should forward messages to GCM servers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM