// Prepare notification data and display it. private void doNotification(Context context, Long messageId, Bitmap srcPhoto) { ChatMsgBaseInfo message = WalkArroundMsgManager.getInstance(context).getMessageById(messageId); if (message == null) { return; } ContactInfo contact = ContactsManager.getInstance(context).getContactByUsrObjId(message.getContact()); Intent intent = new Intent(); intent.setClass(context.getApplicationContext(), BuildMessageActivity.class); intent.putExtra(BuildMessageActivity.INTENT_RECEIVER_EDITABLE, false); intent.putExtra(BuildMessageActivity.INTENT_CONVERSATION_RECEIVER, message.getContact()); intent.putExtra(BuildMessageActivity.INTENT_CONVERSATION_THREAD_ID, message.getMsgThreadId()); intent.putExtra(BuildMessageActivity.INTENT_CONVERSATION_TYPE, message.getChatType()); // TODO: contact should write to DB & we should crate a content provider for those contacts. if (contact != null) { intent.putExtra(BuildMessageActivity.INTENT_CONVERSATION_DISPLAY_NAME, contact.getUsername()); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity( context, (int) message.getMsgThreadId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); // If the contact is null, set the user name as empty. String displayName = (contact == null ? "" : contact.getUsername()); message.setData(MessageUtil.getDisplayStr(context, displayName, message)); Notification notification = new Notification(); notification.flags = Notification.FLAG_AUTO_CANCEL; notification.icon = R.drawable.msg_notify_icon; // TODO: Add Emoji parser later. notification.tickerText = EmojiParser.getInstance(context).getSmileyText(message.getData()); notification.tickerText = message.getData(); if (Build.VERSION.SDK_INT == 19) { contentIntent.cancel(); contentIntent = PendingIntent.getActivity( context, (int) message.getMsgThreadId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); } notification.contentIntent = contentIntent; notification.contentView = new RemoteViews(context.getPackageName(), R.layout.msg_notify_panel); // notification.defaults=Notification.DEFAULT_SOUND; initNotifyView(context, notification.contentView, message, contact, srcPhoto); try { String number = (contact == null ? DEFAULT_NOTIFICATION_ID : contact.getMobilePhoneNumber()); int startPos = number.length() > 5 ? number.length() - 5 : 0; int id = Integer.parseInt(number.substring(startPos)); NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(number, id, notification); } catch (NumberFormatException e) { sLogger.e("notification NumberFormatException:" + e.getMessage()); } }
/** * 收到新的IM消息,是否显示通知bar * * @param context * @param messageId */ private void onReceivedNewImMsg(Context context, long messageId, String userId) { if (BuildMessageActivity.sCurrentReceiverNum != null && BuildMessageActivity.sCurrentReceiverNum.equals(userId)) { return; } // If there is no build message activity on foreground. if (MessageUtil.isNewMsgNotifyReceive()) { mContext = context; notification(context, userId, messageId); } }