Example usage for android.service.notification StatusBarNotification getNotification

List of usage examples for android.service.notification StatusBarNotification getNotification

Introduction

In this page you can find the example usage for android.service.notification StatusBarNotification getNotification.

Prototype

public Notification getNotification() 

Source Link

Document

The android.app.Notification supplied to android.app.NotificationManager#notify(int,Notification) .

Usage

From source file:com.android.car.cluster.sample.MessagingConverter.java

public static MessageContactDetails convert(StatusBarNotification sbn) {
    CarExtender ce = new CarExtender(sbn.getNotification());
    UnreadConversation uc = ce.getUnreadConversation();

    Bitmap largeIcon = ce.getLargeIcon();
    if (largeIcon == null) {
        largeIcon = sbn.getNotification().largeIcon;
    }//from   w  w w. ja  va 2 s . com
    String name = uc.getParticipant();

    return new MessageContactDetails(largeIcon, name);
}

From source file:com.android.car.cluster.sample.MessagingConverter.java

public static boolean canConvert(StatusBarNotification sbn) {
    Notification notification = sbn.getNotification();
    if (notification == null) {
        if (DebugUtil.DEBUG) {
            Log.d(TAG, "Notification is empty.");
        }//w w w . ja  v a2s .  c  o m
        return false;
    }
    CarExtender ce = new CarExtender(sbn.getNotification());
    if (ce.getUnreadConversation() == null) {
        if (DebugUtil.DEBUG) {
            Log.d(TAG, "Notification with no messaging component.");
        }
        return false;
    }

    CarExtender.UnreadConversation uc = ce.getUnreadConversation();
    String[] messages = uc.getMessages();
    if (messages == null || messages.length == 0) {
        Log.w(TAG, "Car message notification with no messages.");
        return false;
    }

    if (TextUtils.isEmpty(uc.getParticipant())) {
        Log.w(TAG, "Car message notification with no participant.");
        return false;
    }

    if (uc.getReplyPendingIntent() == null) {
        Log.w(TAG, "Car message notification with no reply intent.");
        return false;
    }

    for (String m : messages) {
        if (m == null) {
            Log.w(TAG, "Car message with null text.");
            return false;
        }
    }
    return true;
}

From source file:com.ademsha.appnotifico.NotificationDataHelper.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private static JSONObject getNotificationExtras(JSONObject notification,
        StatusBarNotification statusBarNotification) {
    try {// w  w  w  .  ja  va2  s .  c o  m
        Bundle extras = statusBarNotification.getNotification().extras;
        if (extras != null) {
            notification.put("text", extras.getString(Notification.EXTRA_TEXT));
            notification.put("sub_text", extras.getString(Notification.EXTRA_SUB_TEXT));
            notification.put("summary_text", extras.getString(Notification.EXTRA_SUMMARY_TEXT));
            notification.put("text_lines", Arrays
                    .toString(extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES)).replace("null", ""));
            notification.put("icon", String.valueOf(extras.getInt(Notification.EXTRA_SMALL_ICON)));
            if (extras.getParcelable(Notification.EXTRA_LARGE_ICON) != null) {
                notification.put("large_icon",
                        String.valueOf(extras.getParcelable(Notification.EXTRA_LARGE_ICON).toString())
                                .replace("null", ""));
            }
            notification.put("title", extras.getString(Notification.EXTRA_TITLE));
            notification.put("title_big", extras.getString(Notification.EXTRA_TITLE_BIG));
            notification.put("progress", extras.getInt(Notification.EXTRA_PROGRESS));
            notification.put("progress_indeterminate",
                    String.valueOf(extras.getBoolean(Notification.EXTRA_PROGRESS_INDETERMINATE)));
            notification.put("progress_max", String.valueOf(extras.getInt(Notification.EXTRA_PROGRESS_MAX)));
            notification.put("people", extras.getStringArray(Notification.EXTRA_PEOPLE));
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return notification;
}

From source file:com.achep.acdisplay.notifications.OpenNotification.java

/**
 * Creates empty notification instance./*from  w  w w. j a  va2  s  .c  o  m*/
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static OpenNotification newInstance(@NonNull StatusBarNotification sbn) {
    return new OpenNotification(sbn, sbn.getNotification());
}

From source file:com.stasbar.knowyourself.alarms.AlarmNotifications.java

@TargetApi(Build.VERSION_CODES.N)
private static int getActiveNotificationsCount(Context context, String group) {
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    StatusBarNotification[] notifications = nm.getActiveNotifications();
    int count = 0;
    for (StatusBarNotification statusBarNotification : notifications) {
        final Notification n = statusBarNotification.getNotification();
        if ((n.flags & Notification.FLAG_GROUP_SUMMARY) != Notification.FLAG_GROUP_SUMMARY
                && group.equals(n.getGroup())) {
            count++;//from w w w  .  j a  va 2 s. c om
        }
    }
    return count;
}

From source file:com.ademsha.appnotifico.NotificationDataHelper.java

public static JSONObject getStatusBarNotificationDataAsJSON(StatusBarNotification statusBarNotification) {
    JSONObject notification = new JSONObject();
    try {//from w w w.ja  v  a  2 s. c  om
        notification.put("id", String.valueOf(statusBarNotification.getId()).replace("null", ""));
        notification.put("ticker_text",
                String.valueOf(statusBarNotification.getNotification().tickerText).replace("null", ""));
        notification.put("priority",
                String.valueOf(statusBarNotification.getNotification().priority).replace("null", ""));
        notification.put("number",
                String.valueOf(statusBarNotification.getNotification().number).replace("null", ""));
        notification.put("tag", String.valueOf(statusBarNotification.getTag()).replace("null", ""));
        notification.put("posted_at", String.valueOf(statusBarNotification.getPostTime()).replace("null", ""));
        notification.put("source", String.valueOf(statusBarNotification.getPackageName()).replace("null", ""));
        notification.put("when",
                String.valueOf(statusBarNotification.getNotification().when).replace("null", ""));
        notification.put("led_argb",
                String.valueOf(statusBarNotification.getNotification().ledARGB).replace("null", ""));
        notification.put("led_OnMS",
                String.valueOf(statusBarNotification.getNotification().ledOnMS).replace("null", ""));
        notification.put("led_OnMS",
                String.valueOf(statusBarNotification.getNotification().ledOffMS).replace("null", ""));
        notification.put("vibrate",
                Arrays.toString(statusBarNotification.getNotification().vibrate).replace("null", ""));
        if (statusBarNotification.getNotification().sound != null) {
            notification.put("sound", String.valueOf(statusBarNotification.getNotification().sound.getPath()));
        }
        notification.put("action_intent_package",
                String.valueOf(statusBarNotification.getNotification().contentIntent.getCreatorPackage()));
        notification.put("action_intent_uid",
                String.valueOf(statusBarNotification.getNotification().contentIntent.getCreatorUid()));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            notification.put("visibility", String.valueOf(statusBarNotification.getNotification().visibility));
            notification.put("color", String.valueOf(statusBarNotification.getNotification().color));
            notification.put("category", String.valueOf(statusBarNotification.getNotification().category));
            notification.put("user", String.valueOf(statusBarNotification.getUser().toString()));
            notification.put("group_key", String.valueOf(statusBarNotification.getGroupKey()));
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            notification.put("key", String.valueOf(statusBarNotification.getKey()));
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            notification = getNotificationExtras(notification, statusBarNotification);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return notification;
}

From source file:com.oasisfeng.nevo.StatusBarNotificationEvo.java

public static StatusBarNotificationEvo from(final StatusBarNotification sbn) {
    if (sbn instanceof StatusBarNotificationEvo)
        return (StatusBarNotificationEvo) sbn;
    return new StatusBarNotificationEvo(sbn.getPackageName(), null, sbn.getId(), sbn.getTag(), getUid(sbn), 0,
            0, sbn.getNotification(), getUser(sbn), sbn.getPostTime());
}

From source file:com.androidinspain.deskclock.alarms.AlarmNotifications.java

@TargetApi(Build.VERSION_CODES.N)
private static Notification getActiveGroupSummaryNotification(Context context, String group) {
    final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final StatusBarNotification[] notifications = nm.getActiveNotifications();
    for (StatusBarNotification statusBarNotification : notifications) {
        final Notification n = statusBarNotification.getNotification();
        if (isGroupSummary(n) && group.equals(n.getGroup())) {
            return n;
        }//  w w  w .j  a v a2  s.c o  m
    }
    return null;
}

From source file:com.androidinspain.deskclock.alarms.AlarmNotifications.java

/**
 * Method which returns the first active notification for a given group. If a notification was
 * just posted, provide it to make sure it is included as a potential result. If a notification
 * was just canceled, provide the id so that it is not included as a potential result. These
 * extra parameters are needed due to a race condition which exists in
 * {@link NotificationManager#getActiveNotifications()}.
 *
 * @param context Context from which to grab the NotificationManager
 * @param group The group key to query for notifications
 * @param canceledNotificationId The id of the just-canceled notification (-1 if none)
 * @param postedNotification The notification that was just posted
 * @return The first active notification for the group
 *///from w w w.  j a  va 2  s  . c o  m
@TargetApi(Build.VERSION_CODES.N)
private static Notification getFirstActiveNotification(Context context, String group,
        int canceledNotificationId, Notification postedNotification) {
    final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    final StatusBarNotification[] notifications = nm.getActiveNotifications();
    Notification firstActiveNotification = postedNotification;
    for (StatusBarNotification statusBarNotification : notifications) {
        final Notification n = statusBarNotification.getNotification();
        if (!isGroupSummary(n) && group.equals(n.getGroup())
                && statusBarNotification.getId() != canceledNotificationId) {
            if (firstActiveNotification == null
                    || n.getSortKey().compareTo(firstActiveNotification.getSortKey()) < 0) {
                firstActiveNotification = n;
            }
        }
    }
    return firstActiveNotification;
}

From source file:org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationsPlugin.java

public void sendNotification(StatusBarNotification statusBarNotification, boolean requestAnswer) {

    Notification notification = statusBarNotification.getNotification();
    AppDatabase appDatabase = new AppDatabase(context);

    if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0
            || (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0
            || (notification.flags & Notification.FLAG_LOCAL_ONLY) != 0) {
        //This is not a notification we want!
        return;//  w  w w.  j a va  2s . c  o  m
    }

    appDatabase.open();
    if (!appDatabase.isEnabled(statusBarNotification.getPackageName())) {
        return;
        // we dont want notification from this app
    }
    appDatabase.close();

    String key = getNotificationKeyCompat(statusBarNotification);
    String packageName = statusBarNotification.getPackageName();
    String appName = AppsHelper.appNameLookup(context, packageName);

    if ("com.facebook.orca".equals(packageName) && (statusBarNotification.getId() == 10012)
            && "Messenger".equals(appName) && notification.tickerText == null) {
        //HACK: Hide weird Facebook empty "Messenger" notification that is actually not shown in the phone
        return;
    }

    if (packageName.equals("com.google.android.googlequicksearchbox")) {
        //HACK: Hide Google Now notifications that keep constantly popping up (and without text because we don't know how to read them properly)
        return;
    }

    NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_NOTIFICATION);

    if (packageName.equals("org.kde.kdeconnect_tp")) {
        //Make our own notifications silent :)
        np.set("silent", true);
        np.set("requestAnswer", true); //For compatibility with old desktop versions of KDE Connect that don't support "silent"
    }
    /*
            if (sendIcons) {
    try {
        Drawable drawableAppIcon = AppsHelper.appIconLookup(context, packageName);
        Bitmap appIcon = ImagesHelper.drawableToBitmap(drawableAppIcon);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        if (appIcon.getWidth() > 128) {
            appIcon = Bitmap.createScaledBitmap(appIcon, 96, 96, true);
        }
        appIcon.compress(Bitmap.CompressFormat.PNG, 90, outStream);
        byte[] bitmapData = outStream.toByteArray();
        np.setPayload(bitmapData);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("NotificationsPlugin", "Error retrieving icon");
    }
            }
    */
    np.set("id", key);
    np.set("appName", appName == null ? packageName : appName);
    np.set("isClearable", statusBarNotification.isClearable());
    np.set("ticker", getTickerText(notification));
    np.set("time", Long.toString(statusBarNotification.getPostTime()));
    if (requestAnswer) {
        np.set("requestAnswer", true);
        np.set("silent", true);
    }

    device.sendPackage(np);
}