Skip to content

Commit

Permalink
Show notification badges on android
Browse files Browse the repository at this point in the history
  • Loading branch information
kunall17 committed Jun 25, 2017
1 parent badae49 commit 86fe30c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ buildscript {
}
//apply plugin: 'io.fabric'
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}

Expand All @@ -159,6 +160,7 @@ dependencies {
compile project(':react-native-vector-icons')
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "me.leolin:ShortcutBadger:1.1.16@aar"
compile "com.facebook.react:react-native:+" // From node_modules
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
transitive = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.LinkedHashMap;
import java.util.Locale;

import me.leolin.shortcutbadger.ShortcutBadger;

import static com.zulipmobile.notifications.NotificationHelper.buildNotificationContent;
import static com.zulipmobile.notifications.NotificationHelper.clearConversations;
import static com.zulipmobile.notifications.NotificationHelper.extractNames;
Expand Down Expand Up @@ -58,6 +60,11 @@ protected PushNotificationsProp getProps() {
public void onOpened() {
super.onOpened();
clearConversations(conversations);
try {
ShortcutBadger.removeCount(mContext);
} catch (Exception e) {
Log.e("BADGE ERROR", e.toString());
}
}


Expand All @@ -74,6 +81,7 @@ protected Notification.Builder getNotificationBuilder(PendingIntent intent) {
String stream = getProps().getStream();
String topic = getProps().getTopic();
String baseURL = getProps().getBaseURL();
int totalMessagesCount = extractTotalMessagesCount(conversations);

builder.setSmallIcon(R.drawable.zulip_notification);
builder.setAutoCancel(true);
Expand All @@ -97,10 +105,17 @@ protected Notification.Builder getNotificationBuilder(PendingIntent intent) {
}
}
} else {
builder.setContentTitle(String.format(Locale.ENGLISH, "%d messages in %d conversations", extractTotalMessagesCount(conversations), conversations.size()));
builder.setContentTitle(String.format(Locale.ENGLISH, "%d messages in %d conversations", totalMessagesCount, conversations.size()));
builder.setStyle(new Notification.BigTextStyle().bigText(buildNotificationContent(conversations)));
builder.setContentText("Messages from " + TextUtils.join(",", extractNames(conversations)));
}

try {
ShortcutBadger.applyCount(mContext, totalMessagesCount);
} catch (Exception e) {
Log.e("BADGE ERROR", e.toString());
}

if (time != null) {
long timStamp = Long.parseLong(getProps().getTime()) * 1000;
builder.setWhen(timStamp);
Expand Down

0 comments on commit 86fe30c

Please sign in to comment.