Example usage for android.service.notification StatusBarNotification getId

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

Introduction

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

Prototype

public int getId() 

Source Link

Document

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

Usage

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

public static boolean equals(StatusBarNotification n, StatusBarNotification n2) {
    return n == n2 || n != null && n2 != null && new EqualsBuilder().append(n.getId(), n2.getId())
            .append(n.getPackageName(), n2.getPackageName()).append(n.getTag(), n2.getTag()).isEquals();
}

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

public static JSONObject getStatusBarNotificationDataAsJSON(StatusBarNotification statusBarNotification) {
    JSONObject notification = new JSONObject();
    try {//from   w w  w  . j ava 2  s .c o  m
        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.google.android.apps.muzei.datalayer.ActivateMuzeiIntentService.java

public static void maybeShowActivateMuzeiNotification(Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    if (preferences.getBoolean(ACTIVATE_MUZEI_NOTIF_SHOWN_PREF_KEY, false)) {
        return;//from w  w  w  .  ja  v a 2  s. co  m
    }
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(NOTIFICATION_SERVICE);
    StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
    boolean hasInstallNotification = false;
    boolean hasActivateNotification = false;
    for (StatusBarNotification notification : notifications) {
        if (notification.getId() == INSTALL_NOTIFICATION_ID) {
            hasInstallNotification = true;
        } else if (notification.getId() == ACTIVATE_NOTIFICATION_ID) {
            hasActivateNotification = true;
        }
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.ic_stat_muzei)
            .setColor(ContextCompat.getColor(context, R.color.notification))
            .setPriority(NotificationCompat.PRIORITY_MAX).setDefaults(NotificationCompat.DEFAULT_VIBRATE)
            .setAutoCancel(true).setContentTitle(context.getString(R.string.activate_title));
    Intent deleteIntent = new Intent(context, ActivateMuzeiIntentService.class);
    deleteIntent.setAction(ACTION_MARK_NOTIFICATION_READ);
    builder.setDeleteIntent(PendingIntent.getService(context, 0, deleteIntent, 0));
    // Check if the Muzei main app is installed
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(Wearable.API).build();
    ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
    Set<Node> nodes = new TreeSet<>();
    if (connectionResult.isSuccess()) {
        nodes = Wearable.CapabilityApi
                .getCapability(googleApiClient, "activate_muzei", CapabilityApi.FILTER_ALL).await()
                .getCapability().getNodes();
        googleApiClient.disconnect();
    }
    if (nodes.isEmpty()) {
        if (hasInstallNotification) {
            // No need to repost the notification
            return;
        }
        // Send an install Muzei notification
        if (PlayStoreAvailability.getPlayStoreAvailabilityOnPhone(
                context) != PlayStoreAvailability.PLAY_STORE_ON_PHONE_AVAILABLE) {
            builder.setContentText(context.getString(R.string.activate_no_play_store));
            FirebaseAnalytics.getInstance(context).logEvent("activate_notif_no_play_store", null);
        } else {
            builder.setContentText(context.getString(R.string.activate_install_muzei));
            Intent installMuzeiIntent = new Intent(context, ActivateMuzeiIntentService.class);
            installMuzeiIntent.setAction(ACTION_REMOTE_INSTALL_MUZEI);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, installMuzeiIntent, 0);
            builder.addAction(new NotificationCompat.Action.Builder(R.drawable.open_on_phone,
                    context.getString(R.string.activate_install_action), pendingIntent)
                            .extend(new NotificationCompat.Action.WearableExtender()
                                    .setHintDisplayActionInline(true).setAvailableOffline(false))
                            .build());
            builder.extend(new NotificationCompat.WearableExtender().setContentAction(0));
            FirebaseAnalytics.getInstance(context).logEvent("activate_notif_play_store", null);
        }
        notificationManager.notify(INSTALL_NOTIFICATION_ID, builder.build());
        return;
    }
    // else, Muzei is installed on the phone/tablet, but not activated
    if (hasInstallNotification) {
        // Clear any install Muzei notification
        notificationManager.cancel(INSTALL_NOTIFICATION_ID);
    }
    if (hasActivateNotification) {
        // No need to repost the notification
        return;
    }
    String nodeName = nodes.iterator().next().getDisplayName();
    builder.setContentText(context.getString(R.string.activate_enable_muzei, nodeName));
    Intent launchMuzeiIntent = new Intent(context, ActivateMuzeiIntentService.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, launchMuzeiIntent, 0);
    builder.addAction(new NotificationCompat.Action.Builder(R.drawable.open_on_phone,
            context.getString(R.string.activate_action, nodeName), pendingIntent)
                    .extend(new NotificationCompat.Action.WearableExtender().setHintDisplayActionInline(true)
                            .setAvailableOffline(false))
                    .build());
    Bitmap background = null;
    try {
        background = BitmapFactory.decodeStream(context.getAssets().open("starrynight.jpg"));
    } catch (IOException e) {
        Log.e(TAG, "Error reading default background asset", e);
    }
    builder.extend(new NotificationCompat.WearableExtender().setContentAction(0).setBackground(background));
    FirebaseAnalytics.getInstance(context).logEvent("activate_notif_installed", null);
    notificationManager.notify(ACTIVATE_NOTIFICATION_ID, builder.build());
}

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:org.smssecure.smssecure.notifications.MessageNotifier.java

private static void cancelActiveNotifications(@NonNull Context context) {
    NotificationManager notifications = ServiceUtil.getNotificationManager(context);
    notifications.cancel(SUMMARY_NOTIFICATION_ID);

    if (Build.VERSION.SDK_INT >= 23) {
        try {// w  w w  .  java2  s  .com
            StatusBarNotification[] activeNotifications = notifications.getActiveNotifications();

            for (StatusBarNotification activeNotification : activeNotifications) {
                notifications.cancel(activeNotification.getId());
            }
        } catch (Throwable e) {
            // XXX Appears to be a ROM bug, see https://github.com/WhisperSystems/Signal-Android/issues/6043
            Log.w(TAG, e);
            notifications.cancelAll();
        }
    }
}

From source file:org.smssecure.smssecure.notifications.MessageNotifier.java

private static void cancelOrphanedNotifications(@NonNull Context context, NotificationState notificationState) {
    if (Build.VERSION.SDK_INT >= 23) {
        try {/* ww  w.  ja v  a  2 s.c o  m*/
            NotificationManager notifications = ServiceUtil.getNotificationManager(context);
            StatusBarNotification[] activeNotifications = notifications.getActiveNotifications();

            for (StatusBarNotification notification : activeNotifications) {
                boolean validNotification = false;

                if (notification.getId() != SUMMARY_NOTIFICATION_ID) {
                    for (NotificationItem item : notificationState.getNotifications()) {
                        if (notification.getId() == (SUMMARY_NOTIFICATION_ID + item.getThreadId())) {
                            validNotification = true;
                            break;
                        }
                    }

                    if (!validNotification) {
                        notifications.cancel(notification.getId());
                    }
                }
            }
        } catch (Throwable e) {
            // XXX Android ROM Bug, see https://github.com/WhisperSystems/Signal-Android/issues/6043
            Log.w(TAG, e);
        }
    }
}

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 ww  . j  a v  a 2s .  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 static String getNotificationKeyCompat(StatusBarNotification statusBarNotification) {
    String result;//from   w  w  w  .  j  a v  a2 s .com
    // first check if it's one of our remoteIds
    String tag = statusBarNotification.getTag();
    if (tag != null && tag.startsWith("kdeconnectId:"))
        result = Integer.toString(statusBarNotification.getId());
    else if (Build.VERSION.SDK_INT >= 21) {
        result = statusBarNotification.getKey();
    } else {
        String packageName = statusBarNotification.getPackageName();
        int id = statusBarNotification.getId();
        String safePackageName = (packageName == null) ? "" : packageName;
        String safeTag = (tag == null) ? "" : tag;
        result = safePackageName + ":" + safeTag + ":" + id;
    }
    return result;
}

From source file:com.example.android.activenotifications.ActiveNotificationsFragment.java

/**
 * Adds/updates/removes the notification summary as necessary.
 *//*from   www.j  a  v  a 2s  .  c  o m*/
protected void updateNotificationSummary() {
    final StatusBarNotification[] activeNotifications = mNotificationManager.getActiveNotifications();

    int numberOfNotifications = activeNotifications.length;
    // Since the notifications might include a summary notification remove it from the count if
    // it is present.
    for (StatusBarNotification notification : activeNotifications) {
        if (notification.getId() == NOTIFICATION_GROUP_SUMMARY_ID) {
            numberOfNotifications--;
            break;
        }
    }

    if (numberOfNotifications > 1) {
        // Add/update the notification summary.
        String notificationContent = getString(R.string.sample_notification_summary_content,
                "" + numberOfNotifications);
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity())
                .setSmallIcon(R.mipmap.ic_notification)
                .setStyle(new NotificationCompat.BigTextStyle().setSummaryText(notificationContent))
                .setGroup(NOTIFICATION_GROUP).setGroupSummary(true);
        final Notification notification = builder.build();
        mNotificationManager.notify(NOTIFICATION_GROUP_SUMMARY_ID, notification);
    } else {
        // Remove the notification summary.
        mNotificationManager.cancel(NOTIFICATION_GROUP_SUMMARY_ID);
    }
}

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

/**
 * {@inheritDoc}/*w  w w  .  j  a  va 2 s.c o  m*/
 */
@Override
public boolean hasIdenticalIds(@Nullable OpenNotification n) {
    if (n == null)
        return false;
    StatusBarNotification sbn = getStatusBarNotification();
    StatusBarNotification sbn2 = n.getStatusBarNotification();
    assert sbn2 != null;
    return new EqualsBuilder().append(sbn.getId(), sbn2.getId()).append(getPackageName(), n.getPackageName())
            .append(sbn.getTag(), sbn2.getTag()).isEquals();
}