Example usage for android.content Intent FLAG_INCLUDE_STOPPED_PACKAGES

List of usage examples for android.content Intent FLAG_INCLUDE_STOPPED_PACKAGES

Introduction

In this page you can find the example usage for android.content Intent FLAG_INCLUDE_STOPPED_PACKAGES.

Prototype

int FLAG_INCLUDE_STOPPED_PACKAGES

To view the source code for android.content Intent FLAG_INCLUDE_STOPPED_PACKAGES.

Click Source Link

Document

If set, this intent will always match any components in packages that are currently stopped.

Usage

From source file:org.giffftalk.messenger.NotificationsController.java

@SuppressLint("InlinedApi")
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast) {
    if (Build.VERSION.SDK_INT < 18) {
        return;/*ww w .  j a  v  a 2 s.c  o  m*/
    }

    ArrayList<Long> sortedDialogs = new ArrayList<>();
    HashMap<Long, ArrayList<MessageObject>> messagesByDialogs = new HashMap<>();
    for (int a = 0; a < pushMessages.size(); a++) {
        MessageObject messageObject = pushMessages.get(a);
        long dialog_id = messageObject.getDialogId();
        if ((int) dialog_id == 0) {
            continue;
        }

        ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            messagesByDialogs.put(dialog_id, arrayList);
            sortedDialogs.add(0, dialog_id);
        }
        arrayList.add(messageObject);
    }

    HashMap<Long, Integer> oldIdsWear = new HashMap<>();
    oldIdsWear.putAll(wearNotificationsIds);
    wearNotificationsIds.clear();

    HashMap<Long, Integer> oldIdsAuto = new HashMap<>();
    oldIdsAuto.putAll(autoNotificationsIds);
    autoNotificationsIds.clear();

    for (int b = 0; b < sortedDialogs.size(); b++) {
        long dialog_id = sortedDialogs.get(b);
        ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
        int max_id = messageObjects.get(0).getId();
        int max_date = messageObjects.get(0).messageOwner.date;
        TLRPC.Chat chat = null;
        TLRPC.User user = null;
        String name;
        if (dialog_id > 0) {
            user = MessagesController.getInstance().getUser((int) dialog_id);
            if (user == null) {
                continue;
            }
        } else {
            chat = MessagesController.getInstance().getChat(-(int) dialog_id);
            if (chat == null) {
                continue;
            }
        }
        TLRPC.FileLocation photoPath = null;
        if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
            if (chat != null) {
                if (chat.photo != null && chat.photo.photo_small != null
                        && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                    photoPath = chat.photo.photo_small;
                }
            } else {
                if (user.photo != null && user.photo.photo_small != null
                        && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                    photoPath = user.photo.photo_small;
                }
            }
        }

        Integer notificationIdWear = oldIdsWear.get(dialog_id);
        if (notificationIdWear == null) {
            notificationIdWear = wearNotificationId++;
        } else {
            oldIdsWear.remove(dialog_id);
        }

        Integer notificationIdAuto = oldIdsAuto.get(dialog_id);
        if (notificationIdAuto == null) {
            notificationIdAuto = autoNotificationId++;
        } else {
            oldIdsAuto.remove(dialog_id);
        }

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                name).setLatestTimestamp((long) max_date * 1000);

        Intent msgHeardIntent = new Intent();
        msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        msgHeardIntent.setAction("org.giffftalk.messenger.ACTION_MESSAGE_HEARD");
        msgHeardIntent.putExtra("dialog_id", dialog_id);
        msgHeardIntent.putExtra("max_id", max_id);
        PendingIntent msgHeardPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                notificationIdAuto, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        unreadConvBuilder.setReadPendingIntent(msgHeardPendingIntent);

        NotificationCompat.Action wearReplyAction = null;

        if (!ChatObject.isChannel(chat) && !AndroidUtilities.needShowPasscode(false)
                && !UserConfig.isWaitingForPasscodeEnter) {
            Intent msgReplyIntent = new Intent();
            msgReplyIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            msgReplyIntent.setAction("org.giffftalk.messenger.ACTION_MESSAGE_REPLY");
            msgReplyIntent.putExtra("dialog_id", dialog_id);
            msgReplyIntent.putExtra("max_id", max_id);
            PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
                    ApplicationLoader.applicationContext, notificationIdAuto, msgReplyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputAuto = new RemoteInput.Builder(NotificationsController.EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            unreadConvBuilder.setReplyAction(msgReplyPendingIntent, remoteInputAuto);

            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
            replyIntent.putExtra("dialog_id", dialog_id);
            replyIntent.putExtra("max_id", max_id);
            PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                    notificationIdWear, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            String replyToString;
            if (chat != null) {
                replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
            } else {
                replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
            }
            wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString,
                    replyPendingIntent).addRemoteInput(remoteInputWear).build();
        }

        String text = "";
        for (int a = messageObjects.size() - 1; a >= 0; a--) {
            MessageObject messageObject = messageObjects.get(a);
            String message = getStringForMessage(messageObject, false);
            if (message == null) {
                continue;
            }
            if (chat != null) {
                message = message.replace(" @ " + name, "");
            } else {
                message = message.replace(name + ": ", "").replace(name + " ", "");
            }
            if (text.length() > 0) {
                text += "\n\n";
            }
            text += message;

            unreadConvBuilder.addMessage(message);
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if (chat != null) {
            intent.putExtra("chatId", chat.id);
        } else if (user != null) {
            intent.putExtra("userId", user.id);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
        if (wearReplyAction != null) {
            wearableExtender.addAction(wearReplyAction);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setGroup("messages").setContentText(text)
                        .setAutoCancel(true)
                        // Customized by Digant
                        // .setColor(0xff2ca5e0)
                        .setColor(Theme.PRIMARY_COLOR).setGroupSummary(false).setContentIntent(contentIntent)
                        .extend(wearableExtender)
                        .extend(new NotificationCompat.CarExtender()
                                .setUnreadConversation(unreadConvBuilder.build()))
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                builder.setLargeIcon(img.getBitmap());
            }
        }

        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            builder.addPerson("tel:+" + user.phone);
        }

        notificationManager.notify(notificationIdWear, builder.build());
        wearNotificationsIds.put(dialog_id, notificationIdWear);
    }

    for (HashMap.Entry<Long, Integer> entry : oldIdsWear.entrySet()) {
        notificationManager.cancel(entry.getValue());
    }
}

From source file:org.pouyadr.messenger.NotificationsController.java

@SuppressLint("InlinedApi")
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast) {
    if (Build.VERSION.SDK_INT < 18) {
        return;/*  w  ww.  j av  a2 s  .  c  o  m*/
    }

    ArrayList<Long> sortedDialogs = new ArrayList<>();
    HashMap<Long, ArrayList<MessageObject>> messagesByDialogs = new HashMap<>();
    for (int a = 0; a < pushMessages.size(); a++) {
        MessageObject messageObject = pushMessages.get(a);
        long dialog_id = messageObject.getDialogId();
        if ((int) dialog_id == 0) {
            continue;
        }

        ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            messagesByDialogs.put(dialog_id, arrayList);
            sortedDialogs.add(0, dialog_id);
        }
        arrayList.add(messageObject);
    }

    HashMap<Long, Integer> oldIdsWear = new HashMap<>();
    oldIdsWear.putAll(wearNotificationsIds);
    wearNotificationsIds.clear();

    HashMap<Long, Integer> oldIdsAuto = new HashMap<>();
    oldIdsAuto.putAll(autoNotificationsIds);
    autoNotificationsIds.clear();

    for (int b = 0; b < sortedDialogs.size(); b++) {
        long dialog_id = sortedDialogs.get(b);
        ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
        int max_id = messageObjects.get(0).getId();
        int max_date = messageObjects.get(0).messageOwner.date;
        TLRPC.Chat chat = null;
        TLRPC.User user = null;
        String name;
        if (dialog_id > 0) {
            user = MessagesController.getInstance().getUser((int) dialog_id);
            if (user == null) {
                continue;
            }
        } else {
            chat = MessagesController.getInstance().getChat(-(int) dialog_id);
            if (chat == null) {
                continue;
            }
        }
        TLRPC.FileLocation photoPath = null;
        if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter
                || HiddenController.IsHidden(dialog_id)) {
            name = LocaleController.getString("AppName", R.string.AppName);
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
            if (chat != null) {
                if (chat.photo != null && chat.photo.photo_small != null
                        && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                    photoPath = chat.photo.photo_small;
                }
            } else {
                if (user.photo != null && user.photo.photo_small != null
                        && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                    photoPath = user.photo.photo_small;
                }
            }
        }

        Integer notificationIdWear = oldIdsWear.get(dialog_id);
        if (notificationIdWear == null) {
            notificationIdWear = wearNotificationId++;
        } else {
            oldIdsWear.remove(dialog_id);
        }

        Integer notificationIdAuto = oldIdsAuto.get(dialog_id);
        if (notificationIdAuto == null) {
            notificationIdAuto = autoNotificationId++;
        } else {
            oldIdsAuto.remove(dialog_id);
        }

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                name).setLatestTimestamp((long) max_date * 1000);

        Intent msgHeardIntent = new Intent();
        msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        msgHeardIntent.setAction("org.telegram.messenger.ACTION_MESSAGE_HEARD");
        msgHeardIntent.putExtra("dialog_id", dialog_id);
        msgHeardIntent.putExtra("max_id", max_id);
        PendingIntent msgHeardPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                notificationIdAuto, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        unreadConvBuilder.setReadPendingIntent(msgHeardPendingIntent);

        NotificationCompat.Action wearReplyAction = null;

        if (!ChatObject.isChannel(chat) && !AndroidUtilities.needShowPasscode(false)
                && !UserConfig.isWaitingForPasscodeEnter && !HiddenController.IsHidden(dialog_id)) {
            Intent msgReplyIntent = new Intent();
            msgReplyIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            msgReplyIntent.setAction("org.pouyadr.messenger.ACTION_MESSAGE_REPLY");
            msgReplyIntent.putExtra("dialog_id", dialog_id);
            msgReplyIntent.putExtra("max_id", max_id);
            PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
                    ApplicationLoader.applicationContext, notificationIdAuto, msgReplyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputAuto = new RemoteInput.Builder(NotificationsController.EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            unreadConvBuilder.setReplyAction(msgReplyPendingIntent, remoteInputAuto);

            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
            replyIntent.putExtra("dialog_id", dialog_id);
            replyIntent.putExtra("max_id", max_id);
            PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                    notificationIdWear, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            String replyToString;
            if (chat != null) {
                replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
            } else {
                replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
            }
            wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString,
                    replyPendingIntent).addRemoteInput(remoteInputWear).build();
        }

        String text = "";
        for (int a = messageObjects.size() - 1; a >= 0; a--) {
            MessageObject messageObject = messageObjects.get(a);
            String message = getStringForMessage(messageObject, false);
            if (message == null) {
                continue;
            }
            if (chat != null) {
                message = message.replace(" @ " + name, "");
            } else {
                message = message.replace(name + ": ", "").replace(name + " ", "");
            }
            if (text.length() > 0) {
                text += "\n\n";
            }
            text += message;

            unreadConvBuilder.addMessage(message);
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if (chat != null) {
            intent.putExtra("chatId", chat.id);
        } else if (user != null) {
            intent.putExtra("userId", user.id);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
        if (wearReplyAction != null) {
            wearableExtender.addAction(wearReplyAction);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setGroup("messages").setContentText(text)
                        .setAutoCancel(true).setColor(0xff2ca5e0).setGroupSummary(false)
                        .setContentIntent(contentIntent).extend(wearableExtender)
                        .extend(new NotificationCompat.CarExtender()
                                .setUnreadConversation(unreadConvBuilder.build()))
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                builder.setLargeIcon(img.getBitmap());
            }
        }

        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            builder.addPerson("tel:+" + user.phone);
        }

        notificationManager.notify(notificationIdWear, builder.build());
        wearNotificationsIds.put(dialog_id, notificationIdWear);
    }

    for (HashMap.Entry<Long, Integer> entry : oldIdsWear.entrySet()) {
        notificationManager.cancel(entry.getValue());
    }
}

From source file:ir.irani.telecam.messenger.NotificationsController.java

@SuppressLint("InlinedApi")
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast) {
    if (Build.VERSION.SDK_INT < 18) {
        return;// w  ww.  ja v  a  2s  . c om
    }

    ArrayList<Long> sortedDialogs = new ArrayList<>();
    HashMap<Long, ArrayList<MessageObject>> messagesByDialogs = new HashMap<>();
    for (int a = 0; a < pushMessages.size(); a++) {
        MessageObject messageObject = pushMessages.get(a);
        long dialog_id = messageObject.getDialogId();
        if ((int) dialog_id == 0) {
            continue;
        }

        ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            messagesByDialogs.put(dialog_id, arrayList);
            sortedDialogs.add(0, dialog_id);
        }
        arrayList.add(messageObject);
    }

    HashMap<Long, Integer> oldIdsWear = new HashMap<>();
    oldIdsWear.putAll(wearNotificationsIds);
    wearNotificationsIds.clear();

    HashMap<Long, Integer> oldIdsAuto = new HashMap<>();
    oldIdsAuto.putAll(autoNotificationsIds);
    autoNotificationsIds.clear();

    for (int b = 0; b < sortedDialogs.size(); b++) {
        long dialog_id = sortedDialogs.get(b);
        ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
        int max_id = messageObjects.get(0).getId();
        int max_date = messageObjects.get(0).messageOwner.date;
        TLRPC.Chat chat = null;
        TLRPC.User user = null;
        String name;
        if (dialog_id > 0) {
            user = MessagesController.getInstance().getUser((int) dialog_id);
            if (user == null) {
                continue;
            }
        } else {
            chat = MessagesController.getInstance().getChat(-(int) dialog_id);
            if (chat == null) {
                continue;
            }
        }
        TLRPC.FileLocation photoPath = null;
        if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter
                || HiddenController.IsHidden(dialog_id)) {
            name = LocaleController.getString("AppName", R.string.AppName);
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
            if (chat != null) {
                if (chat.photo != null && chat.photo.photo_small != null
                        && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                    photoPath = chat.photo.photo_small;
                }
            } else {
                if (user.photo != null && user.photo.photo_small != null
                        && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                    photoPath = user.photo.photo_small;
                }
            }
        }

        Integer notificationIdWear = oldIdsWear.get(dialog_id);
        if (notificationIdWear == null) {
            notificationIdWear = wearNotificationId++;
        } else {
            oldIdsWear.remove(dialog_id);
        }

        Integer notificationIdAuto = oldIdsAuto.get(dialog_id);
        if (notificationIdAuto == null) {
            notificationIdAuto = autoNotificationId++;
        } else {
            oldIdsAuto.remove(dialog_id);
        }

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                name).setLatestTimestamp((long) max_date * 1000);

        Intent msgHeardIntent = new Intent();
        msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        msgHeardIntent.setAction("org.telegram.messenger.ACTION_MESSAGE_HEARD");
        msgHeardIntent.putExtra("dialog_id", dialog_id);
        msgHeardIntent.putExtra("max_id", max_id);
        PendingIntent msgHeardPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                notificationIdAuto, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        unreadConvBuilder.setReadPendingIntent(msgHeardPendingIntent);

        NotificationCompat.Action wearReplyAction = null;

        if (!ChatObject.isChannel(chat) && !AndroidUtilities.needShowPasscode(false)
                && !UserConfig.isWaitingForPasscodeEnter && !HiddenController.IsHidden(dialog_id)) {
            Intent msgReplyIntent = new Intent();
            msgReplyIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            msgReplyIntent.setAction("ir.irani.telecam.messenger.ACTION_MESSAGE_REPLY");
            msgReplyIntent.putExtra("dialog_id", dialog_id);
            msgReplyIntent.putExtra("max_id", max_id);
            PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
                    ApplicationLoader.applicationContext, notificationIdAuto, msgReplyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputAuto = new RemoteInput.Builder(NotificationsController.EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            unreadConvBuilder.setReplyAction(msgReplyPendingIntent, remoteInputAuto);

            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
            replyIntent.putExtra("dialog_id", dialog_id);
            replyIntent.putExtra("max_id", max_id);
            PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                    notificationIdWear, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            String replyToString;
            if (chat != null) {
                replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
            } else {
                replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
            }
            wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString,
                    replyPendingIntent).addRemoteInput(remoteInputWear).build();
        }

        String text = "";
        for (int a = messageObjects.size() - 1; a >= 0; a--) {
            MessageObject messageObject = messageObjects.get(a);
            String message = getStringForMessage(messageObject, false);
            if (message == null) {
                continue;
            }
            if (chat != null) {
                message = message.replace(" @ " + name, "");
            } else {
                message = message.replace(name + ": ", "").replace(name + " ", "");
            }
            if (text.length() > 0) {
                text += "\n\n";
            }
            text += message;

            unreadConvBuilder.addMessage(message);
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if (chat != null) {
            intent.putExtra("chatId", chat.id);
        } else if (user != null) {
            intent.putExtra("userId", user.id);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
        if (wearReplyAction != null) {
            wearableExtender.addAction(wearReplyAction);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setGroup("messages").setContentText(text)
                        .setAutoCancel(true).setColor(0xff2ca5e0).setGroupSummary(false)
                        .setContentIntent(contentIntent).extend(wearableExtender)
                        .extend(new NotificationCompat.CarExtender()
                                .setUnreadConversation(unreadConvBuilder.build()))
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                builder.setLargeIcon(img.getBitmap());
            }
        }

        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            builder.addPerson("tel:+" + user.phone);
        }

        notificationManager.notify(notificationIdWear, builder.build());
        wearNotificationsIds.put(dialog_id, notificationIdWear);
    }

    for (HashMap.Entry<Long, Integer> entry : oldIdsWear.entrySet()) {
        notificationManager.cancel(entry.getValue());
    }
}

From source file:ir.besteveryeverapp.telegram.NotificationsController.java

@SuppressLint("InlinedApi")
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast) {
    if (Build.VERSION.SDK_INT < 18) {
        return;/*from   w w  w  . j av a 2  s .  co  m*/
    }

    ArrayList<Long> sortedDialogs = new ArrayList<>();
    HashMap<Long, ArrayList<MessageObject>> messagesByDialogs = new HashMap<>();
    for (int a = 0; a < pushMessages.size(); a++) {
        MessageObject messageObject = pushMessages.get(a);
        long dialog_id = messageObject.getDialogId();
        if ((int) dialog_id == 0) {
            continue;
        }

        ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            messagesByDialogs.put(dialog_id, arrayList);
            sortedDialogs.add(0, dialog_id);
        }
        arrayList.add(messageObject);
    }

    HashMap<Long, Integer> oldIdsWear = new HashMap<>();
    oldIdsWear.putAll(wearNotificationsIds);
    wearNotificationsIds.clear();

    HashMap<Long, Integer> oldIdsAuto = new HashMap<>();
    oldIdsAuto.putAll(autoNotificationsIds);
    autoNotificationsIds.clear();

    for (int b = 0; b < sortedDialogs.size(); b++) {
        long dialog_id = sortedDialogs.get(b);
        ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
        int max_id = messageObjects.get(0).getId();
        int max_date = messageObjects.get(0).messageOwner.date;
        TLRPC.Chat chat = null;
        TLRPC.User user = null;
        String name;
        if (dialog_id > 0) {
            user = MessagesController.getInstance().getUser((int) dialog_id);
            if (user == null) {
                continue;
            }
        } else {
            chat = MessagesController.getInstance().getChat(-(int) dialog_id);
            if (chat == null) {
                continue;
            }
        }
        TLRPC.FileLocation photoPath = null;
        if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
            if (chat != null) {
                if (chat.photo != null && chat.photo.photo_small != null
                        && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                    photoPath = chat.photo.photo_small;
                }
            } else {
                if (user.photo != null && user.photo.photo_small != null
                        && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                    photoPath = user.photo.photo_small;
                }
            }
        }

        Integer notificationIdWear = oldIdsWear.get(dialog_id);
        if (notificationIdWear == null) {
            notificationIdWear = wearNotificationId++;
        } else {
            oldIdsWear.remove(dialog_id);
        }

        Integer notificationIdAuto = oldIdsAuto.get(dialog_id);
        if (notificationIdAuto == null) {
            notificationIdAuto = autoNotificationId++;
        } else {
            oldIdsAuto.remove(dialog_id);
        }

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                name).setLatestTimestamp((long) max_date * 1000);

        Intent msgHeardIntent = new Intent();
        msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        msgHeardIntent.setAction("ir.besteveryeverapp.telegram.ACTION_MESSAGE_HEARD");
        msgHeardIntent.putExtra("dialog_id", dialog_id);
        msgHeardIntent.putExtra("max_id", max_id);
        PendingIntent msgHeardPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                notificationIdAuto, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        unreadConvBuilder.setReadPendingIntent(msgHeardPendingIntent);

        NotificationCompat.Action wearReplyAction = null;

        if (!ChatObject.isChannel(chat) && !AndroidUtilities.needShowPasscode(false)
                && !UserConfig.isWaitingForPasscodeEnter) {
            Intent msgReplyIntent = new Intent();
            msgReplyIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            msgReplyIntent.setAction("ir.besteveryeverapp.telegram.ACTION_MESSAGE_REPLY");
            msgReplyIntent.putExtra("dialog_id", dialog_id);
            msgReplyIntent.putExtra("max_id", max_id);
            PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
                    ApplicationLoader.applicationContext, notificationIdAuto, msgReplyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputAuto = new RemoteInput.Builder(NotificationsController.EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            unreadConvBuilder.setReplyAction(msgReplyPendingIntent, remoteInputAuto);

            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
            replyIntent.putExtra("dialog_id", dialog_id);
            replyIntent.putExtra("max_id", max_id);
            PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                    notificationIdWear, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            String replyToString;
            if (chat != null) {
                replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
            } else {
                replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
            }
            wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString,
                    replyPendingIntent).addRemoteInput(remoteInputWear).build();
        }

        String text = "";
        for (int a = messageObjects.size() - 1; a >= 0; a--) {
            MessageObject messageObject = messageObjects.get(a);
            String message = getStringForMessage(messageObject, false);
            if (message == null) {
                continue;
            }
            if (chat != null) {
                message = message.replace(" @ " + name, "");
            } else {
                message = message.replace(name + ": ", "").replace(name + " ", "");
            }
            if (text.length() > 0) {
                text += "\n\n";
            }
            text += message;

            unreadConvBuilder.addMessage(message);
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if (chat != null) {
            intent.putExtra("chatId", chat.id);
        } else if (user != null) {
            intent.putExtra("userId", user.id);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
        if (wearReplyAction != null) {
            wearableExtender.addAction(wearReplyAction);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setGroup("messages").setContentText(text)
                        .setAutoCancel(true).setColor(0xff2ca5e0).setGroupSummary(false)
                        .setContentIntent(contentIntent).extend(wearableExtender)
                        .extend(new NotificationCompat.CarExtender()
                                .setUnreadConversation(unreadConvBuilder.build()))
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                builder.setLargeIcon(img.getBitmap());
            }
        }

        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            builder.addPerson("tel:+" + user.phone);
        }

        notificationManager.notify(notificationIdWear, builder.build());
        wearNotificationsIds.put(dialog_id, notificationIdWear);
    }

    for (HashMap.Entry<Long, Integer> entry : oldIdsWear.entrySet()) {
        notificationManager.cancel(entry.getValue());
    }
}

From source file:kr.wdream.storyshop.NotificationsController.java

@SuppressLint("InlinedApi")
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast) {
    if (Build.VERSION.SDK_INT < 18) {
        return;//from w  w  w.j a  v  a  2  s.co m
    }

    ArrayList<Long> sortedDialogs = new ArrayList<>();
    HashMap<Long, ArrayList<MessageObject>> messagesByDialogs = new HashMap<>();
    for (int a = 0; a < pushMessages.size(); a++) {
        MessageObject messageObject = pushMessages.get(a);
        long dialog_id = messageObject.getDialogId();
        if ((int) dialog_id == 0) {
            continue;
        }

        ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            messagesByDialogs.put(dialog_id, arrayList);
            sortedDialogs.add(0, dialog_id);
        }
        arrayList.add(messageObject);
    }

    HashMap<Long, Integer> oldIdsWear = new HashMap<>();
    oldIdsWear.putAll(wearNotificationsIds);
    wearNotificationsIds.clear();

    for (int b = 0; b < sortedDialogs.size(); b++) {
        long dialog_id = sortedDialogs.get(b);
        ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
        int max_id = messageObjects.get(0).getId();
        int max_date = messageObjects.get(0).messageOwner.date;
        TLRPC.Chat chat = null;
        TLRPC.User user = null;
        String name;
        if (dialog_id > 0) {
            user = MessagesController.getInstance().getUser((int) dialog_id);
            if (user == null) {
                continue;
            }
        } else {
            chat = MessagesController.getInstance().getChat(-(int) dialog_id);
            if (chat == null) {
                continue;
            }
        }
        TLRPC.FileLocation photoPath = null;
        if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
            if (chat != null) {
                if (chat.photo != null && chat.photo.photo_small != null
                        && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                    photoPath = chat.photo.photo_small;
                }
            } else {
                if (user.photo != null && user.photo.photo_small != null
                        && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                    photoPath = user.photo.photo_small;
                }
            }
        }

        Integer notificationId = oldIdsWear.get(dialog_id);
        if (notificationId == null) {
            notificationId = (int) dialog_id;
        } else {
            oldIdsWear.remove(dialog_id);
        }

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                name).setLatestTimestamp((long) max_date * 1000);

        Intent msgHeardIntent = new Intent();
        msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        msgHeardIntent.setAction("kr.toptalk.messenger.ACTION_MESSAGE_HEARD");
        msgHeardIntent.putExtra("dialog_id", dialog_id);
        msgHeardIntent.putExtra("max_id", max_id);
        PendingIntent msgHeardPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                notificationId, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        unreadConvBuilder.setReadPendingIntent(msgHeardPendingIntent);

        NotificationCompat.Action wearReplyAction = null;

        if (!ChatObject.isChannel(chat) && !AndroidUtilities.needShowPasscode(false)
                && !UserConfig.isWaitingForPasscodeEnter) {
            Intent msgReplyIntent = new Intent();
            msgReplyIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            msgReplyIntent.setAction("kr.toptalk.messenger.ACTION_MESSAGE_REPLY");
            msgReplyIntent.putExtra("dialog_id", dialog_id);
            msgReplyIntent.putExtra("max_id", max_id);
            PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
                    ApplicationLoader.applicationContext, notificationId, msgReplyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputAuto = new RemoteInput.Builder(NotificationsController.EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            unreadConvBuilder.setReplyAction(msgReplyPendingIntent, remoteInputAuto);

            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
            replyIntent.putExtra("dialog_id", dialog_id);
            replyIntent.putExtra("max_id", max_id);
            PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                    notificationId, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            String replyToString;
            if (chat != null) {
                replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
            } else {
                replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
            }
            wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString,
                    replyPendingIntent).addRemoteInput(remoteInputWear).build();
        }

        Integer count = pushDialogs.get(dialog_id);
        if (count == null) {
            count = 0;
        }
        NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(null)
                .setConversationTitle(String.format("%1$s (%2$s)", name, LocaleController
                        .formatPluralString("NewMessages", Math.max(count, messageObjects.size()))));

        String text = "";
        for (int a = messageObjects.size() - 1; a >= 0; a--) {
            MessageObject messageObject = messageObjects.get(a);
            String message = getStringForMessage(messageObject, false);
            if (message == null) {
                continue;
            }
            if (chat != null) {
                message = message.replace(" @ " + name, "");
            } else {
                message = message.replace(name + ": ", "").replace(name + " ", "");
            }
            if (text.length() > 0) {
                text += "\n\n";
            }
            text += message;

            unreadConvBuilder.addMessage(message);
            messagingStyle.addMessage(message, ((long) messageObject.messageOwner.date) * 1000, null);
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if (chat != null) {
            intent.putExtra("chatId", chat.id);
        } else if (user != null) {
            intent.putExtra("userId", user.id);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
        if (wearReplyAction != null) {
            wearableExtender.addAction(wearReplyAction);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setGroup("messages").setContentText(text)
                        .setAutoCancel(true).setNumber(messageObjects.size()).setColor(0xff2ca5e0)
                        .setGroupSummary(false).setWhen(((long) messageObjects.get(0).messageOwner.date) * 1000)
                        .setStyle(messagingStyle).setContentIntent(contentIntent).extend(wearableExtender)
                        .extend(new NotificationCompat.CarExtender()
                                .setUnreadConversation(unreadConvBuilder.build()))
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                builder.setLargeIcon(img.getBitmap());
            } else {
                try {
                    float scaleFactor = 160.0f / AndroidUtilities.dp(50);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = scaleFactor < 1 ? 1 : (int) scaleFactor;
                    Bitmap bitmap = BitmapFactory
                            .decodeFile(FileLoader.getPathToAttach(photoPath, true).toString(), options);
                    if (bitmap != null) {
                        builder.setLargeIcon(bitmap);
                    }
                } catch (Throwable e) {
                    //ignore
                }
            }
        }

        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            builder.addPerson("tel:+" + user.phone);
        }

        notificationManager.notify(notificationId, builder.build());
        wearNotificationsIds.put(dialog_id, notificationId);
    }

    for (HashMap.Entry<Long, Integer> entry : oldIdsWear.entrySet()) {
        notificationManager.cancel(entry.getValue());
    }
}

From source file:net.bluehack.messenger.NotificationsController.java

@SuppressLint("InlinedApi")
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast) {
    if (Build.VERSION.SDK_INT < 18) {
        return;//w  w w  .j  a v  a 2  s.  co m
    }

    ArrayList<Long> sortedDialogs = new ArrayList<>();
    HashMap<Long, ArrayList<MessageObject>> messagesByDialogs = new HashMap<>();
    for (int a = 0; a < pushMessages.size(); a++) {
        MessageObject messageObject = pushMessages.get(a);
        long dialog_id = messageObject.getDialogId();
        if ((int) dialog_id == 0) {
            continue;
        }

        ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            messagesByDialogs.put(dialog_id, arrayList);
            sortedDialogs.add(0, dialog_id);
        }
        arrayList.add(messageObject);
    }

    HashMap<Long, Integer> oldIdsWear = new HashMap<>();
    oldIdsWear.putAll(wearNotificationsIds);
    wearNotificationsIds.clear();

    for (int b = 0; b < sortedDialogs.size(); b++) {
        long dialog_id = sortedDialogs.get(b);
        ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
        int max_id = messageObjects.get(0).getId();
        int max_date = messageObjects.get(0).messageOwner.date;
        TLRPC.Chat chat = null;
        TLRPC.User user = null;
        String name;
        if (dialog_id > 0) {
            user = MessagesController.getInstance().getUser((int) dialog_id);
            if (user == null) {
                continue;
            }
        } else {
            chat = MessagesController.getInstance().getChat(-(int) dialog_id);
            if (chat == null) {
                continue;
            }
        }
        TLRPC.FileLocation photoPath = null;
        if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
            if (chat != null) {
                if (chat.photo != null && chat.photo.photo_small != null
                        && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                    photoPath = chat.photo.photo_small;
                }
            } else {
                if (user.photo != null && user.photo.photo_small != null
                        && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                    photoPath = user.photo.photo_small;
                }
            }
        }

        Integer notificationId = oldIdsWear.get(dialog_id);
        if (notificationId == null) {
            notificationId = (int) dialog_id;
        } else {
            oldIdsWear.remove(dialog_id);
        }

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                name).setLatestTimestamp((long) max_date * 1000);

        Intent msgHeardIntent = new Intent();
        msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        msgHeardIntent.setAction("net.bluehack.messenger.ACTION_MESSAGE_HEARD");
        msgHeardIntent.putExtra("dialog_id", dialog_id);
        msgHeardIntent.putExtra("max_id", max_id);
        PendingIntent msgHeardPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                notificationId, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        unreadConvBuilder.setReadPendingIntent(msgHeardPendingIntent);

        NotificationCompat.Action wearReplyAction = null;

        if (!ChatObject.isChannel(chat) && !AndroidUtilities.needShowPasscode(false)
                && !UserConfig.isWaitingForPasscodeEnter) {
            Intent msgReplyIntent = new Intent();
            msgReplyIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            msgReplyIntent.setAction("net.bluehack.messenger.ACTION_MESSAGE_REPLY");
            msgReplyIntent.putExtra("dialog_id", dialog_id);
            msgReplyIntent.putExtra("max_id", max_id);
            PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
                    ApplicationLoader.applicationContext, notificationId, msgReplyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputAuto = new RemoteInput.Builder(NotificationsController.EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            unreadConvBuilder.setReplyAction(msgReplyPendingIntent, remoteInputAuto);

            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
            replyIntent.putExtra("dialog_id", dialog_id);
            replyIntent.putExtra("max_id", max_id);
            PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                    notificationId, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            String replyToString;
            if (chat != null) {
                replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
            } else {
                replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
            }
            wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString,
                    replyPendingIntent).addRemoteInput(remoteInputWear).build();
        }

        Integer count = pushDialogs.get(dialog_id);
        if (count == null) {
            count = 0;
        }
        NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(null)
                .setConversationTitle(String.format("%1$s (%2$s)", name, LocaleController
                        .formatPluralString("NewMessages", Math.max(count, messageObjects.size()))));

        String text = "";
        for (int a = messageObjects.size() - 1; a >= 0; a--) {
            MessageObject messageObject = messageObjects.get(a);
            String message = getStringForMessage(messageObject, false);
            if (message == null) {
                continue;
            }
            if (chat != null) {
                message = message.replace(" @ " + name, "");
            } else {
                message = message.replace(name + ": ", "").replace(name + " ", "");
            }
            if (text.length() > 0) {
                text += "\n\n";
            }
            text += message;

            unreadConvBuilder.addMessage(message);
            messagingStyle.addMessage(message, ((long) messageObject.messageOwner.date) * 1000, null);
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if (chat != null) {
            intent.putExtra("chatId", chat.id);
        } else if (user != null) {
            intent.putExtra("userId", user.id);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
        if (wearReplyAction != null) {
            wearableExtender.addAction(wearReplyAction);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setGroup("messages").setContentText(text)
                        .setAutoCancel(true).setNumber(messageObjects.size()).setColor(0xff2ca5e0)
                        .setGroupSummary(false).setWhen(((long) messageObjects.get(0).messageOwner.date) * 1000)
                        .setStyle(messagingStyle).setContentIntent(contentIntent).extend(wearableExtender)
                        .extend(new NotificationCompat.CarExtender()
                                .setUnreadConversation(unreadConvBuilder.build()))
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                builder.setLargeIcon(img.getBitmap());
            } else {
                try {
                    float scaleFactor = 160.0f / AndroidUtilities.dp(50);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = scaleFactor < 1 ? 1 : (int) scaleFactor;
                    Bitmap bitmap = BitmapFactory
                            .decodeFile(FileLoader.getPathToAttach(photoPath, true).toString(), options);
                    if (bitmap != null) {
                        builder.setLargeIcon(bitmap);
                    }
                } catch (Throwable e) {
                    //ignore
                }
            }
        }

        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            builder.addPerson("tel:+" + user.phone);
        }

        notificationManager.notify(notificationId, builder.build());
        wearNotificationsIds.put(dialog_id, notificationId);
    }

    for (HashMap.Entry<Long, Integer> entry : oldIdsWear.entrySet()) {
        notificationManager.cancel(entry.getValue());
    }
}

From source file:com.ferdi2005.secondgram.NotificationsController.java

@SuppressLint("InlinedApi")
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast) {
    if (Build.VERSION.SDK_INT < 18) {
        return;//from   ww  w . jav a2 s.c om
    }

    ArrayList<Long> sortedDialogs = new ArrayList<>();
    HashMap<Long, ArrayList<MessageObject>> messagesByDialogs = new HashMap<>();
    for (int a = 0; a < pushMessages.size(); a++) {
        MessageObject messageObject = pushMessages.get(a);
        long dialog_id = messageObject.getDialogId();
        if ((int) dialog_id == 0) {
            continue;
        }

        ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
        if (arrayList == null) {
            arrayList = new ArrayList<>();
            messagesByDialogs.put(dialog_id, arrayList);
            sortedDialogs.add(0, dialog_id);
        }
        arrayList.add(messageObject);
    }

    HashMap<Long, Integer> oldIdsWear = new HashMap<>();
    oldIdsWear.putAll(wearNotificationsIds);
    wearNotificationsIds.clear();

    for (int b = 0; b < sortedDialogs.size(); b++) {
        long dialog_id = sortedDialogs.get(b);
        ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
        int max_id = messageObjects.get(0).getId();
        int max_date = messageObjects.get(0).messageOwner.date;
        TLRPC.Chat chat = null;
        TLRPC.User user = null;
        String name;
        if (dialog_id > 0) {
            user = MessagesController.getInstance().getUser((int) dialog_id);
            if (user == null) {
                continue;
            }
        } else {
            chat = MessagesController.getInstance().getChat(-(int) dialog_id);
            if (chat == null) {
                continue;
            }
        }
        TLRPC.FileLocation photoPath = null;
        if (AndroidUtilities.needShowPasscode(false) || UserConfig.isWaitingForPasscodeEnter) {
            name = LocaleController.getString("AppName", R.string.AppName);
        } else {
            if (chat != null) {
                name = chat.title;
            } else {
                name = UserObject.getUserName(user);
            }
            if (chat != null) {
                if (chat.photo != null && chat.photo.photo_small != null
                        && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
                    photoPath = chat.photo.photo_small;
                }
            } else {
                if (user.photo != null && user.photo.photo_small != null
                        && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
                    photoPath = user.photo.photo_small;
                }
            }
        }

        Integer notificationId = oldIdsWear.get(dialog_id);
        if (notificationId == null) {
            notificationId = (int) dialog_id;
        } else {
            oldIdsWear.remove(dialog_id);
        }

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder = new NotificationCompat.CarExtender.UnreadConversation.Builder(
                name).setLatestTimestamp((long) max_date * 1000);

        Intent msgHeardIntent = new Intent();
        msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        msgHeardIntent.setAction("com.ferdi2005.secondgram.ACTION_MESSAGE_HEARD");
        msgHeardIntent.putExtra("dialog_id", dialog_id);
        msgHeardIntent.putExtra("max_id", max_id);
        PendingIntent msgHeardPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                notificationId, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        unreadConvBuilder.setReadPendingIntent(msgHeardPendingIntent);

        NotificationCompat.Action wearReplyAction = null;

        if ((!ChatObject.isChannel(chat) || chat != null && chat.megagroup)
                && !AndroidUtilities.needShowPasscode(false) && !UserConfig.isWaitingForPasscodeEnter) {
            Intent msgReplyIntent = new Intent();
            msgReplyIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
            msgReplyIntent.setAction("com.ferdi2005.secondgram.ACTION_MESSAGE_REPLY");
            msgReplyIntent.putExtra("dialog_id", dialog_id);
            msgReplyIntent.putExtra("max_id", max_id);
            PendingIntent msgReplyPendingIntent = PendingIntent.getBroadcast(
                    ApplicationLoader.applicationContext, notificationId, msgReplyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputAuto = new RemoteInput.Builder(NotificationsController.EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            unreadConvBuilder.setReplyAction(msgReplyPendingIntent, remoteInputAuto);

            Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
            replyIntent.putExtra("dialog_id", dialog_id);
            replyIntent.putExtra("max_id", max_id);
            PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext,
                    notificationId, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
            String replyToString;
            if (chat != null) {
                replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
            } else {
                replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
            }
            wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString,
                    replyPendingIntent).setAllowGeneratedReplies(true).addRemoteInput(remoteInputWear).build();
        }

        Integer count = pushDialogs.get(dialog_id);
        if (count == null) {
            count = 0;
        }
        NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(null)
                .setConversationTitle(String.format("%1$s (%2$s)", name, LocaleController
                        .formatPluralString("NewMessages", Math.max(count, messageObjects.size()))));

        String text = "";
        for (int a = messageObjects.size() - 1; a >= 0; a--) {
            MessageObject messageObject = messageObjects.get(a);
            String message = getStringForMessage(messageObject, false);
            if (message == null) {
                continue;
            }
            if (chat != null) {
                message = message.replace(" @ " + name, "");
            } else {
                message = message.replace(name + ": ", "").replace(name + " ", "");
            }
            if (text.length() > 0) {
                text += "\n\n";
            }
            text += message;

            unreadConvBuilder.addMessage(message);
            messagingStyle.addMessage(message, ((long) messageObject.messageOwner.date) * 1000, null);
        }

        Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
        intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
        intent.setFlags(32768);
        if (chat != null) {
            intent.putExtra("chatId", chat.id);
        } else if (user != null) {
            intent.putExtra("userId", user.id);
        }
        PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
        if (wearReplyAction != null) {
            wearableExtender.addAction(wearReplyAction);
        }

        String dismissalID = null;
        if (chat != null)
            dismissalID = "tgchat" + chat.id + "_" + max_id;
        else if (user != null)
            dismissalID = "tguser" + user.id + "_" + max_id;

        wearableExtender.setDismissalId(dismissalID);

        NotificationCompat.WearableExtender summaryExtender = new NotificationCompat.WearableExtender();
        summaryExtender.setDismissalId("summary_" + dismissalID);
        notificationBuilder.extend(summaryExtender);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                ApplicationLoader.applicationContext).setContentTitle(name)
                        .setSmallIcon(R.drawable.notification).setGroup("messages").setContentText(text)
                        .setAutoCancel(true).setNumber(messageObjects.size()).setColor(0xff2ca5e0)
                        .setGroupSummary(false).setWhen(((long) messageObjects.get(0).messageOwner.date) * 1000)
                        .setStyle(messagingStyle).setContentIntent(contentIntent).extend(wearableExtender)
                        .extend(new NotificationCompat.CarExtender()
                                .setUnreadConversation(unreadConvBuilder.build()))
                        .setCategory(NotificationCompat.CATEGORY_MESSAGE);
        if (photoPath != null) {
            BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
            if (img != null) {
                builder.setLargeIcon(img.getBitmap());
            } else {
                try {
                    float scaleFactor = 160.0f / AndroidUtilities.dp(50);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = scaleFactor < 1 ? 1 : (int) scaleFactor;
                    Bitmap bitmap = BitmapFactory
                            .decodeFile(FileLoader.getPathToAttach(photoPath, true).toString(), options);
                    if (bitmap != null) {
                        builder.setLargeIcon(bitmap);
                    }
                } catch (Throwable e) {
                    //ignore
                }
            }
        }

        if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
            builder.addPerson("tel:+" + user.phone);
        }

        notificationManager.notify(notificationId, builder.build());
        wearNotificationsIds.put(dialog_id, notificationId);
    }

    for (HashMap.Entry<Long, Integer> entry : oldIdsWear.entrySet()) {
        notificationManager.cancel(entry.getValue());
    }
}