me.leolin.shortcutbadger.ShortcutBadger Java Examples

The following examples show how to use me.leolin.shortcutbadger.ShortcutBadger. 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: UnreadNotificationsService.java    From ghwatch with Apache License 2.0 6 votes vote down vote up
/**
 * Call this when you want to mark android notification issued by this app as read - remove it
 */
public void markAndroidNotificationBundledDetailRead(int androidNotificationIdToCancel) {
  Utils.getNotificationManager(context).cancel(androidNotificationIdToCancel);
  long c = PreferencesUtils.getLong(context, NUM_OF_BUNDLED_ANDROID_NOTIFICATIONS,0);
  if(c>1){
    PreferencesUtils.storeLong(context, NUM_OF_BUNDLED_ANDROID_NOTIFICATIONS, --c);
  } else {
    markAndroidNotificationsRead();
  }

  int bc = PreferencesUtils.getInt(context, NUM_OF_BADGED_ANDROID_NOTIFICATIONS,0);
  if(bc>1){
    PreferencesUtils.storeInt(context, NUM_OF_BADGED_ANDROID_NOTIFICATIONS, --bc);
    ShortcutBadger.applyCount(context, bc);
  } else {
    ShortcutBadger.removeCount(context);
  }

}
 
Example #2
Source File: HelperNotificationAndBadge.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
public static int[] updateBadgeOnly(Realm realm, long roomId) {
    int unreadMessageCount = 0;
    int chatCount = 0;
    int[] result = new int[2];

    RealmResults<RealmRoom> realmRooms = realm.where(RealmRoom.class).equalTo(RealmRoomFields.KEEP_ROOM, false).
            equalTo(RealmRoomFields.MUTE, false).equalTo(RealmRoomFields.IS_DELETED, false).notEqualTo(RealmRoomFields.ID, roomId).findAll();

    for (RealmRoom realmRoom1 : realmRooms) {
        if (realmRoom1.getUnreadCount() > 0) {
            unreadMessageCount += realmRoom1.getUnreadCount();
            ++chatCount;
        }
    }

    try {
        ShortcutBadger.applyCount(G.context, unreadMessageCount);
    } catch (Exception e) {
        e.printStackTrace();
    }

    result[0] = unreadMessageCount;
    result[1] = chatCount;
    return result;
}
 
Example #3
Source File: AppUtils.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
public static int[] updateBadgeOnly(Realm realm, long roomId) {
    int unreadMessageCount = 0;
    int chatCount = 0;
    int[] result = new int[2];

    RealmResults<RealmRoom> realmRooms = realm.where(RealmRoom.class).equalTo(RealmRoomFields.KEEP_ROOM, false).
            equalTo(RealmRoomFields.MUTE, false).equalTo(RealmRoomFields.IS_DELETED, false).notEqualTo(RealmRoomFields.ID, roomId).findAll();

    for (RealmRoom realmRoom1 : realmRooms) {
        if (realmRoom1.getUnreadCount() > 0) {
            unreadMessageCount += realmRoom1.getUnreadCount();
            ++chatCount;
        }
    }

    try {
        ShortcutBadger.applyCount(G.context, unreadMessageCount);
    } catch (Exception e) {
        e.printStackTrace();
    }

    result[0] = unreadMessageCount;
    result[1] = chatCount;
    return result;
}
 
Example #4
Source File: FirebaseMessagingPlugin.java    From cordova-plugin-firebase-messaging with MIT License 5 votes vote down vote up
@CordovaMethod
private void setBadge(int value, CallbackContext callbackContext) {
    if (value >= 0) {
        Context context = cordova.getActivity().getApplicationContext();
        ShortcutBadger.applyCount(context, value);

        callbackContext.success();
    } else {
        callbackContext.error("Badge value can't be negative");
    }
}
 
Example #5
Source File: UnreadNotificationsService.java    From ghwatch with Apache License 2.0 5 votes vote down vote up
protected void fireAndroidNotification(NotificationStream newStream, NotificationStream oldStream) {
  if (newStream == null || !PreferencesUtils.getBoolean(context, PreferencesUtils.PREF_NOTIFY, true))
    return;

  Log.d(TAG, "fireAndroidNotification count before filter " + newStream.size());
  newStream = filterForAndroidNotification(newStream);
  Log.d(TAG, "fireAndroidNotification count after filter " + newStream.size());
  if (newStream.isNewNotification(oldStream)) {
    ShortcutBadger.applyCount(context, newStream.size());
    PreferencesUtils.storeInt(context, NUM_OF_BADGED_ANDROID_NOTIFICATIONS, newStream.size());

    //TEST with only one notification
    if(false) {
      Notification on = newStream.get(0);
      Notification on2 = newStream.get(1);
      newStream = new NotificationStream();
      newStream.addNotification(on);
      newStream.addNotification(on2);
    }

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
      fireAndroidNotificationInboxStyle(newStream);
    } else {
      fireAndroidNotificationBundledStyle(newStream);
    }
    ActivityTracker.sendEvent(context, ActivityTracker.CAT_NOTIF, "new_notif", "notif count: " + newStream.size(), Long.valueOf(newStream.size()));
  } else if (newStream.isEmpty()) {
    // #54 dismiss previous android notification if no any Github notification is available (as it was read on another device)
    Utils.getNotificationManager(context).cancel(ANDROID_NOTIFICATION_MAIN_ID);
    ShortcutBadger.removeCount(context);
  }
}
 
Example #6
Source File: MainActivity.java    From ghwatch with Apache License 2.0 5 votes vote down vote up
@Override
protected void onResume() {
  super.onResume();
  if (!checkUserLoggedIn()) {
    finish();
    return;
  }
  ActivityTracker.sendView(this, TAG);

  Intent intent = getIntent();
  Log.d(TAG, "onResume() intent: " + getIntent());
  if (intent != null && INTENT_ACTION_DISMISS_ALL.equals(intent.getAction())) {
    showMarkAllNotificationsAsReadDialog();
  } else {
    if (intent != null && INTENT_ACTION_RESET_FILTER.equals(intent.getAction())) {
      resetNotificationsFilter();
    }
    if (NewVersionInfoDialogFragment.isShowScheduled(this)) {
      showDialog(new NewVersionInfoDialogFragment());
    } else if (SupportAppDevelopmentDialogFragment.isAutoShowScheduled(this)) {
      showSupportAppDevelopmentDialog();
    }
  }
  if (intent != null)
    intent.setAction(null);
  refreshList(refreshOnNextResume ? ViewDataReloadStrategy.ALWAYS : ViewDataReloadStrategy.IF_TIMED_OUT, false);
  refreshOnNextResume = false;
  unreadNotificationsService.markAndroidWidgetsAsRead();
  unreadNotificationsService.markAndroidNotificationsRead();
  ShortcutBadger.removeCount(getApplicationContext());
}
 
Example #7
Source File: Badger.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
public static void updateBadgerCount(final int unreadCount) {
    if (!support) {
        return; // O版本及以上不再支持
    }

    if (handler == null) {
        handler = Handlers.sharedInstance().newHandler("Badger");
    }

    handler.removeCallbacksAndMessages(null);
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            int badgerCount = unreadCount;
            if (badgerCount < 0) {
                badgerCount = 0;
            } else if (badgerCount > 99) {
                badgerCount = 99;
            }

            boolean res = ShortcutBadger.applyCount(NimUIKit.getContext(), badgerCount);
            if (!res) {
                support = false; // 如果失败就不要再使用了!
            }
            Log.i(TAG, "update badger count " + (res ? "success" : "failed"));
        }
    }, 200);
}
 
Example #8
Source File: CommonActivityUtils.java    From matrix-android-console with Apache License 2.0 5 votes vote down vote up
/**
 * Logout the current user.
 * @param activity the caller activity
 */
public static void logout(Activity activity) {
    stopEventStream(activity);

    try {
        ShortcutBadger.setBadge(activity, 0);
    } catch (Exception e) {
    }

    // warn that the user logs out
    Collection<MXSession> sessions = Matrix.getMXSessions(activity);
    for(MXSession session : sessions) {
        // Publish to the server that we're now offline
        MyPresenceManager.getInstance(activity, session).advertiseOffline();
        MyPresenceManager.remove(session);
    }

    // clear the preferences
    PreferenceManager.getDefaultSharedPreferences(activity).edit().clear().commit();

    // reset the GCM
    Matrix.getInstance(activity).getSharedGcmRegistrationManager().reset();

    // clear credentials
    Matrix.getInstance(activity).clearSessions(activity, true);

    // ensure that corrupted values are cleared
    Matrix.getInstance(activity).getLoginStorage().clear();

    // reset the contacts
    PIDsRetriever.getIntance().reset();
    ContactsManager.reset();

    MXMediasCache.clearThumbnailsCache(activity);

    // go to login page
    activity.startActivity(new Intent(activity, LoginActivity.class));
    activity.finish();
}
 
Example #9
Source File: ApplicationBadgeHelper.java    From react-native-push-notification with MIT License 5 votes vote down vote up
private void tryAutomaticBadge(Context context, int number) {
    if (null == applyAutomaticBadger) {
        applyAutomaticBadger = ShortcutBadger.applyCount(context, number);
        if (applyAutomaticBadger) {
            FLog.i(LOG_TAG, "First attempt to use automatic badger succeeded; permanently enabling method.");
        } else {
            FLog.i(LOG_TAG, "First attempt to use automatic badger failed; permanently disabling method.");
        }
        return;
    } else if (!applyAutomaticBadger) {
        return;
    }
    ShortcutBadger.applyCount(context, number);
}
 
Example #10
Source File: InputActivity.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@OnClick({R.id.set_badge_num, R.id.get, R.id.format, R.id.getMac1, R.id.getMac2, R.id.link})
public void Click(View view) {
    switch (view.getId()) {
        case R.id.set_badge_num:
            int count = 0;
            if (!TextUtils.isEmpty(mInput.getText().toString())) {
                count = Integer.parseInt(mInput.getText().toString());
            }

            if (ShortcutBadger.applyCount(this, count)) {
                TT.showSToast(this, "success");
            } else {
                TT.showSToast(this, "fail");
            }


            break;
        case R.id.get:
            mResult.setText(mInput.getText().toString());
            break;
        case R.id.format:
            String temp = mResult.getText().toString();

            temp = temp.replace("\n", "");

            mResult.setText(temp);
            break;
        case R.id.getMac1:
            getMacAddress1();
            break;
        case R.id.getMac2:
            getMacAddress2();
            break;
        case R.id.link:
            Toast.makeText(this, "link !", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
    }
}
 
Example #11
Source File: DefaultMessageNotifier.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static void updateBadge(Context context, int count) {
  try {
    if (count == 0) ShortcutBadger.removeCount(context);
    else            ShortcutBadger.applyCount(context, count);
  } catch (Throwable t) {
    // NOTE :: I don't totally trust this thing, so I'm catching
    // everything.
    Log.w(TAG, t);
  }
}
 
Example #12
Source File: BadgeHelper.java    From react-native-fcm with MIT License 5 votes vote down vote up
public void setBadgeCount(int badgeCount) {
    storeBadgeCount(badgeCount);
    if (badgeCount == 0) {
        ShortcutBadger.removeCount(mContext);
        Log.d(TAG, "Remove count");
    } else {
        ShortcutBadger.applyCount(mContext, badgeCount);
        Log.d(TAG, "Apply count: " + badgeCount);
    }
}
 
Example #13
Source File: AppUtils.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void cleanBadge() {
    try {
        ShortcutBadger.applyCount(G.context, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #14
Source File: HelperNotificationAndBadge.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
static void cleanBadge() {
    try {
        ShortcutBadger.applyCount(G.context, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #15
Source File: NotificationManager.java    From weMessage with GNU Affero General Public License v3.0 5 votes vote down vote up
public void subtractUnreadMessages(String chatId){
    unreadMessages.remove(chatId);

    try {
        ShortcutBadger.applyCount(app, getTotalUnread());
    }catch (Exception ex){
        AppLogger.error("An error occurred while trying to create a badge", ex);
    }
}
 
Example #16
Source File: NotificationManager.java    From weMessage with GNU Affero General Public License v3.0 5 votes vote down vote up
public void addUnreadMessages(String chatId, int amount){
    int unread = 0;

    if (unreadMessages.containsKey(chatId)){
        unread = unreadMessages.get(chatId);
    }

    unreadMessages.put(chatId, unread + amount);

    try {
        ShortcutBadger.applyCount(app, getTotalUnread());
    }catch (Exception ex){
        AppLogger.error("An error occurred while trying to create a badge", ex);
    }
}
 
Example #17
Source File: ApplicationBadgeHelper.java    From react-native-push-notification-CE with MIT License 5 votes vote down vote up
private void tryAutomaticBadge(Context context, int number) {
    if (null == applyAutomaticBadger) {
        applyAutomaticBadger = ShortcutBadger.applyCount(context, number);
        if (applyAutomaticBadger) {
            FLog.i(LOG_TAG, "First attempt to use automatic badger succeeded; permanently enabling method.");
        } else {
            FLog.i(LOG_TAG, "First attempt to use automatic badger failed; permanently disabling method.");
        }
        return;
    } else if (!applyAutomaticBadger) {
        return;
    }
    ShortcutBadger.applyCount(context, number);
}
 
Example #18
Source File: BadgeModule.java    From react-native-android-badge with MIT License 4 votes vote down vote up
@ReactMethod
public void setBadge(int number) {
  ShortcutBadger.applyCount(getReactApplicationContext(), number);
}
 
Example #19
Source File: CommonActivityUtils.java    From matrix-android-console with Apache License 2.0 4 votes vote down vote up
public static void updateUnreadMessagesBadge(Context context, int badgeValue) {
    try {
        ShortcutBadger.setBadge(context, badgeValue);
    } catch (Exception e) {
    }
}