Java Code Examples for android.service.notification.StatusBarNotification#getId()

The following examples show how to use android.service.notification.StatusBarNotification#getId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultMessageNotifier.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private static boolean isDisplayingSummaryNotification(@NonNull Context context) {
  if (Build.VERSION.SDK_INT >= 23) {
    try {
      NotificationManager     notificationManager = ServiceUtil.getNotificationManager(context);
      StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications();

      for (StatusBarNotification activeNotification : activeNotifications) {
        if (activeNotification.getId() == NotificationIds.MESSAGE_SUMMARY) {
          return true;
        }
      }

      return false;

    } catch (Throwable e) {
      // XXX Android ROM Bug, see #6043
      Log.w(TAG, e);
      return false;
    }
  } else {
    return false;
  }
}
 
Example 2
Source File: SnoozeHelper.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
protected boolean cancel(int userId, String pkg, String tag, int id) {
    if (mSnoozedNotifications.containsKey(userId)) {
        ArrayMap<String, NotificationRecord> recordsForPkg =
                mSnoozedNotifications.get(userId).get(pkg);
        if (recordsForPkg != null) {
            final Set<Map.Entry<String, NotificationRecord>> records = recordsForPkg.entrySet();
            String key = null;
            for (Map.Entry<String, NotificationRecord> record : records) {
                final StatusBarNotification sbn = record.getValue().sbn;
                if (Objects.equals(sbn.getTag(), tag) && sbn.getId() == id) {
                    record.getValue().isCanceled = true;
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 3
Source File: NotificationListenerService.java    From heads-up with GNU General Public License v3.0 6 votes vote down vote up
public boolean isNotificationValid(String pkg, String tag, int id) {
    final StatusBarNotification[] activeNotifications = getActiveNotifications();
    for (StatusBarNotification statusBarNotification : activeNotifications) {
        final String statusBarNotificationTag = statusBarNotification.getTag();
        final String statusBarNotificationPackageName = statusBarNotification.getPackageName();
        final int statusBarNotificationId = statusBarNotification.getId();
        if (statusBarNotificationPackageName.equals(pkg)
                && statusBarNotificationId == id) {
            if (tag == null && statusBarNotificationTag == null)
                return true;
            if (tag != null && statusBarNotificationTag != null)
                if (statusBarNotificationTag.equals(tag))
                    return true;
        }
    }
    return false;
}
 
Example 4
Source File: NotificationBuilderImplBase.java    From FCM-for-Mojo with GNU General Public License v3.0 6 votes vote down vote up
@Override
void clear(Chat chat, NotificationBuilder nb) {
    int id = (int) chat.getUniqueId();

    nb.getNotificationManager().cancel(id);

    boolean clearGroup = true;
    for (StatusBarNotification sbn : nb.getNotificationManager().getActiveNotifications()) {
        if (sbn.getId() != NOTIFICATION_ID_SYSTEM
                && sbn.getId() != NOTIFICATION_ID_GROUP_SUMMARY) {
            clearGroup = false;
        }
    }
    if (clearGroup) {
        nb.getNotificationManager().cancel(NOTIFICATION_ID_GROUP_SUMMARY);
    }
}
 
Example 5
Source File: NotificationListener.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
private NotificationBarInfoItem findExistItem(StatusBarNotification statusBarNotification) {
    if (statusBarNotification == null || statusBarNotification.getPackageName() == null) {
        return null;
    }
    if (CollectionUtils.isEmpty(notificationInfo.getData())) {
        return null;
    }
    CharSequence packageName = statusBarNotification.getPackageName();
    int id = statusBarNotification.getId();
    for (NotificationBarInfoItem notificationBarInfoItem : notificationInfo.getData()) {
        if (id == notificationBarInfoItem.getNotifyId() && TextUtils.equals(packageName, notificationBarInfoItem.getPackageName()) && notificationBarInfoItem.getType() == 1) {
            return notificationBarInfoItem;
        }
    }
    return null;
}
 
Example 6
Source File: NotificationController.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
public static void cancel(Context context, int id) {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    String groupId = null;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        StatusBarNotification[] activeNotifications = manager.getActiveNotifications();
        for (StatusBarNotification activeNotification : activeNotifications) {
            if (activeNotification.getId() == id) {
                groupId = activeNotification.getNotification().getGroup();
                break;
            }

        }
    }

    manager.cancel(id);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        if (groupId != null) {
            updateSummaryNotification(context, NotificationUtils.getPackageName(groupId), groupId);
        }
    }
}
 
Example 7
Source File: ProgressPublisher.java    From PocketMaps with MIT License 6 votes vote down vote up
@TargetApi(26) //OREO
private void updateTextFinalOrio(String txt)
{ // Crazy: Android is blocking when too much notification updates, so final message does not receive. Try later :(
  int sleepTime = 300;
  for (int i=0; i<10; i++)
  {
    updateTextFinalInternal(txt);
    NotificationManager nMgr = (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
    boolean found = false;
    for (StatusBarNotification n : nMgr.getActiveNotifications())
    {
      if (n.getId() != id) { continue; }
      found = true;
      if (!n.isOngoing()) { return; }
    }
    if (!found) { return; }
    try { Thread.sleep(sleepTime); } catch (Exception e) {}
    sleepTime = (sleepTime/2) * 3;
  }
}
 
Example 8
Source File: DownloadNotificationService.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * @return The summary {@link StatusBarNotification} if one is showing.
 */
@TargetApi(Build.VERSION_CODES.M)
private static StatusBarNotification getSummaryNotification(NotificationManager manager) {
    if (!useForegroundService()) return null;

    StatusBarNotification[] notifications = manager.getActiveNotifications();
    for (StatusBarNotification notification : notifications) {
        boolean isDownloadsGroup = TextUtils.equals(notification.getNotification().getGroup(),
                NotificationConstants.GROUP_DOWNLOADS);
        boolean isSummaryNotification =
                notification.getId() == NotificationConstants.NOTIFICATION_ID_DOWNLOAD_SUMMARY;
        if (isDownloadsGroup && isSummaryNotification) return notification;
    }

    return null;
}
 
Example 9
Source File: DownloadNotificationService.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether or not there are any download notifications showing that aren't the summary
 * notification.
 * @param notificationIdToIgnore If not -1, the id of a notification to ignore and
 *                               assume is closing or about to be closed.
 * @return Whether or not there are valid download notifications currently visible.
 */
@TargetApi(Build.VERSION_CODES.M)
private static boolean hasDownloadNotifications(
        NotificationManager manager, int notificationIdToIgnore) {
    if (!useForegroundService()) return false;

    StatusBarNotification[] notifications = manager.getActiveNotifications();
    for (StatusBarNotification notification : notifications) {
        boolean isDownloadsGroup = TextUtils.equals(notification.getNotification().getGroup(),
                NotificationConstants.GROUP_DOWNLOADS);
        boolean isSummaryNotification =
                notification.getId() == NotificationConstants.NOTIFICATION_ID_DOWNLOAD_SUMMARY;
        boolean isIgnoredNotification =
                notificationIdToIgnore != -1 && notificationIdToIgnore == notification.getId();
        if (isDownloadsGroup && !isSummaryNotification && !isIgnoredNotification) return true;
    }

    return false;
}
 
Example 10
Source File: NotificationHelper.java    From NotificationPeekPort with Apache License 2.0 5 votes vote down vote up
public static String getContentDescription(StatusBarNotification content) {
    if (content != null) {
        String tag = content.getTag() == null ? "null" : content.getTag();
        return content.getPackageName() + DELIMITER + content.getId() + DELIMITER + tag;
    }
    return null;
}
 
Example 11
Source File: Permissions.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
static void removeEventNotification(Context context)
{
    try {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.cancel(
                    PPApplication.GRANT_EVENT_PERMISSIONS_NOTIFICATION_TAG,
                    PPApplication.GRANT_EVENT_PERMISSIONS_NOTIFICATION_ID);
            //if (Build.VERSION.SDK_INT >= 23) {
            StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
            for (StatusBarNotification notification : notifications) {
                String tag = notification.getTag();
                if ((tag != null) && tag.contains(PPApplication.GRANT_EVENT_PERMISSIONS_NOTIFICATION_TAG+"_")) {
                    if (notification.getId() >= PPApplication.EVENT_ID_NOTIFICATION_ID) {
                        notificationManager.cancel(notification.getTag(), notification.getId());
                    }
                }
            }
        /*} else {
            int notificationId = intent.getIntExtra("notificationId", 0);
            manager.cancel(notificationId);
        }*/
        }
    } catch (Exception e) {
        PPApplication.recordException(e);
    }
}
 
Example 12
Source File: AlarmHandler.java    From openScale with GNU General Public License v3.0 5 votes vote down vote up
private void cancelAlarmNotification(Context context)
{
    NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        StatusBarNotification[] activeNotifications = mNotifyMgr.getActiveNotifications();
        for (StatusBarNotification notification : activeNotifications)
        {
            if (notification.getId() == ALARM_NOTIFICATION_ID) mNotifyMgr.cancel(ALARM_NOTIFICATION_ID);
        }
    }
    else mNotifyMgr.cancel(ALARM_NOTIFICATION_ID);
}
 
Example 13
Source File: NLService.java    From io.appium.settings with Apache License 2.0 5 votes vote down vote up
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    synchronized (notificationsBuffer) {
        for (StoredNotification storedNotification : notificationsBuffer) {
            if (storedNotification.getNotification().getId() == sbn.getId()) {
                storedNotification.setRemoved(true);
                Log.d(TAG, String.format("Successfully marked the removed notification identified by %s",
                        sbn.getId()));
                break;
            }
        }
    }
}
 
Example 14
Source File: Manager.java    From cordova-plugin-firebase-extended-notification with MIT License 5 votes vote down vote up
public boolean notificationExists(int notificationId) {
    NotificationManager notificationManager = (NotificationManager)
        this.context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= 23) {
        StatusBarNotification[] notifications = notificationManager
            .getActiveNotifications();
        for (StatusBarNotification notification : notifications)
            if (notification.getId() == notificationId)
                return true;
        return false;
    }
    //If lacks support
    return true;
}
 
Example 15
Source File: NotificationObject.java    From android-notification-log with MIT License 5 votes vote down vote up
NotificationObject(Context context, StatusBarNotification sbn, final boolean LOG_TEXT, int reason) {
	this.context = context;
	this.LOG_TEXT = LOG_TEXT;

	n           = sbn.getNotification();
	packageName = sbn.getPackageName();
	postTime    = sbn.getPostTime();
	systemTime  = System.currentTimeMillis();

	isClearable = sbn.isClearable();
	isOngoing   = sbn.isOngoing();

	nid         = sbn.getId();
	tag         = sbn.getTag();

	if(Build.VERSION.SDK_INT >= 20) {
		key     = sbn.getKey();
		sortKey = n.getSortKey();
	}

	removeReason = reason;

	extract();

	if(Const.ENABLE_ACTIVITY_RECOGNITION || Const.ENABLE_LOCATION_SERVICE) {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
		lastActivity = sp.getString(Const.PREF_LAST_ACTIVITY, null);
		lastLocation = sp.getString(Const.PREF_LAST_LOCATION, null);
	}
}
 
Example 16
Source File: NotificationPresenter.java    From HeadsUp with GNU General Public License v2.0 4 votes vote down vote up
@SuppressLint("NewApi")
public boolean isInitNotification(@NonNull Context context, @NonNull OpenNotification n) {
    StatusBarNotification sbn = n.getStatusBarNotification();
    return n.isMine() && sbn != null && sbn.getId() == App.ID_NOTIFY_INIT;
}
 
Example 17
Source File: NotificationPresenter.java    From AcDisplay with GNU General Public License v2.0 4 votes vote down vote up
@SuppressLint("NewApi")
public boolean isInitNotification(@NonNull Context context, @NonNull OpenNotification n) {
    StatusBarNotification sbn = n.getStatusBarNotification();
    return n.isMine() && sbn != null && sbn.getId() == App.ID_NOTIFY_INIT;
}
 
Example 18
Source File: NLService.java    From AsteroidOSSync with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Notification notification = sbn.getNotification();
    String packageName = sbn.getPackageName();

    String[] allowedOngoingApps = {"com.google.android.apps.maps"};
    if((notification.priority < Notification.PRIORITY_DEFAULT) ||
       ((notification.flags & Notification.FLAG_ONGOING_EVENT) != 0
        && !Arrays.asList(allowedOngoingApps).contains(packageName)) ||
       (NotificationCompat.getLocalOnly(notification)) ||
       (NotificationCompat.isGroupSummary(notification)))
        return;

    NotificationParser notifParser = new NotificationParser(notification);
    String summary = notifParser.summary;
    String body = notifParser.body;
    int id = sbn.getId();
    String appIcon = iconFromPackage.get(packageName);

    String appName = "";
    try {
        final PackageManager pm = getApplicationContext().getPackageManager();
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
        appName = pm.getApplicationLabel(ai).toString();
    } catch (PackageManager.NameNotFoundException ignored) {}

    if(summary == null) summary = "";
    else                summary = summary.trim();
    if(body == null) body = "";
    else                body = body.trim();
    if(packageName == null) packageName = "";
    if(appIcon == null) appIcon = "";

    Intent i = new  Intent("org.asteroidos.sync.NOTIFICATION_LISTENER");
    i.putExtra("event", "posted");
    i.putExtra("packageName", packageName);
    i.putExtra("id", id);
    i.putExtra("appName", appName);
    i.putExtra("appIcon", appIcon);
    i.putExtra("summary", summary);
    i.putExtra("body", body);

    sendBroadcast(i);
}
 
Example 19
Source File: NotificationPresenter.java    From AcDisplay with GNU General Public License v2.0 4 votes vote down vote up
@SuppressLint("NewApi")
public boolean isTestNotification(@NonNull Context context, @NonNull OpenNotification n) {
    StatusBarNotification sbn = n.getStatusBarNotification();
    return n.isMine() && sbn != null && sbn.getId() == App.ID_NOTIFY_TEST;
}
 
Example 20
Source File: NotificationService.java    From RelaxFinger with GNU General Public License v2.0 2 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onNotificationPosted(StatusBarNotification sbn) {

    StatusBarNotification[] notifies = getActiveNotifications();

    StatusBarNotification validSbn = null;

    for(int i = 0;i<notifies.length;i++){

        if(notifies[i].isClearable()){

            if(notifies[i].getPackageName().equals(sbn.getPackageName()) &&
                    notifies[i].getId()==sbn.getId()){

                validSbn = notifies[i];
                break;
            }
        }
    }

    if(validSbn != null && mNotifyPkgList.contains(validSbn.getPackageName()) && !validSbn.getPackageName().equals("android")
            && validSbn.getNotification().contentIntent != null){

        StatusBarNotification notify = mNotificationArray.get(validSbn.getId());

        if(notify == null){

            mNotificationArray.put(validSbn.getId(),validSbn);

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {

                Icon icon = validSbn.getNotification().getLargeIcon();

                notifyNewNotify(validSbn,icon);

            }else {

                notifyNewNotify(validSbn,null);
            }

        }

    }

    try {

        String title = sbn.getNotification().extras.getString("android.title");

        if (title == null) return;

        if(sKeyboardTitleList.contains(title)){

            if(sp.getBoolean("floatSwitch",false)){

                sendMsg(Config.FLOAT_AUTOMOVE, "move", true);
            }

        }

    }catch (Exception e){

        e.printStackTrace();
    }

}