パケットリスナーを使用して、アプリケーション(xmpp chat)に通知を実装しています。
最初の通知が来るまでコードは正常に機能しますが、ユーザーが通知をクリックするとすぐに、最初のクリック後に(ifとelse)の両方のコードが不足します。私は解決策を得ることができず、その奇妙なことに、IF/ELSEコードの両方がどのように実行されているか-最初の通知クリックの後。
コード
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
final PacketCollector collector = connection.createPacketCollector(filter);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packetChat) {
Message message = (Message) packetChat;
packetChat = collector.nextResult();
if (message.getBody() != null) {
fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody()
+ "] from [" + fromName + "]");
// messages.add(fromName + ":");
mMessageItem = new MessageItemDataClass();
mMessageItem.isSendMessage = false;
mMessageItem.messageText = message.getBody();
messages.add(mMessageItem);
//to add the packet message in the layout only when the user is on the same friend's screen
//for anonymous chat
if(packetChat.getFrom().equalsIgnoreCase(refineFromjId(frienduserID)+"/Smack")){
messages.add(mMessageItem);
}
else{
if(checkPresence=true){
notificationforChat(fromName.substring(0,fromName.indexOf("@"))+":1212 "+message.getBody(),packetChat.getFrom(),0);
}
}
//messages.add(mMessageItem);
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
//simply turn around to the last of the screen
mList.setSelection(mList.getCount());
lastIndex = mList.getLastVisiblePosition()+1;
if (mList.getFirstVisiblePosition() > lastIndex || mList.getLastVisiblePosition() <= lastIndex) {
//mList.smoothScrollToPosition(lastIndex);
customAdapter.notifyDataSetChanged();
mList.setSelection(mList.getCount());
dbhHelper.close();
}else{
mList.setSelection(mList.getCount());
customAdapter.notifyDataSetChanged();
dbhHelper.close();
}
}
});
}
}
}, filter);
通知
public void notificationforChat(CharSequence message,String toJid, int notificationID) {
int notificationCount = 1;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//notification.number = notificationCount++;
Context context = getApplicationContext();
CharSequence contentTitle = "Chat";
CharSequence contentText = message;
Intent notificationIntentforChat = new Intent(this, UserChatActivity.class);
notificationIntentforChat.putExtra("userNameVal", toJid);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntentforChat, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
mNotificationManager.notify(notificationID, notification);
}
誰かができるなら私を助けてください!ありがとう