Skip to content

Commit

Permalink
feature(android): add support for badge count (#207)
Browse files Browse the repository at this point in the history
* feature(android): add support for badge count (#1)

Using Leolin's ShortcutBadger we are able to support any badge counts that are sent as part of the notifications data-payload.

* refactor(android): expand badge support (#3)
  • Loading branch information
sondremare authored and Libin Lu committed Dec 1, 2016
1 parent 1b1f886 commit 62dce36
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
build
xcuserdata
.idea/
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ android {
}
}

repositories {
mavenCentral()
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.facebook.react:react-native:+'
compile 'com.google.firebase:firebase-core:+'
compile 'com.google.firebase:firebase-messaging:+'
compile 'me.leolin:ShortcutBadger:1.1.10@aar'
}

43 changes: 43 additions & 0 deletions android/src/main/java/com/evollu/react/fcm/BadgeHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.evollu.react.fcm;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import me.leolin.shortcutbadger.ShortcutBadger;

public class BadgeHelper {

private static final String TAG = "BadgeHelper";
private static final String PREFERENCES_FILE = "BadgeCountFile";
private static final String BADGE_COUNT_KEY = "BadgeCount";

private Context mContext;
private SharedPreferences sharedPreferences = null;

public BadgeHelper(Context context) {
mContext = context;
sharedPreferences = (SharedPreferences) mContext.getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE);
}

public int getBadgeCount() {
return sharedPreferences.getInt(BADGE_COUNT_KEY, 0);
}

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);
}
}

private void storeBadgeCount(int badgeCount) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(BADGE_COUNT_KEY, badgeCount);
editor.apply();
}
}
12 changes: 12 additions & 0 deletions android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
public class FIRMessagingModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener {
private final static String TAG = FIRMessagingModule.class.getCanonicalName();
private FIRLocalMessagingHelper mFIRLocalMessagingHelper;
private BadgeHelper mBadgeHelper;

public FIRMessagingModule(ReactApplicationContext reactContext) {
super(reactContext);
mFIRLocalMessagingHelper = new FIRLocalMessagingHelper((Application) reactContext.getApplicationContext());
mBadgeHelper = new BadgeHelper(reactContext.getApplicationContext());
getReactApplicationContext().addLifecycleEventListener(this);
getReactApplicationContext().addActivityEventListener(this);
registerTokenRefreshHandler();
Expand Down Expand Up @@ -113,6 +115,16 @@ public void getScheduledLocalNotifications(Promise promise){
promise.resolve(array);
}

@ReactMethod
public void setBadgeNumber(int badgeNumber) {
mBadgeHelper.setBadgeCount(badgeNumber);
}

@ReactMethod
public void getBadgeNumber(Promise promise) {
promise.resolve(mBadgeHelper.getBadgeCount());
}

private void sendEvent(String eventName, Object params) {
getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
Expand Down
24 changes: 23 additions & 1 deletion android/src/main/java/com/evollu/react/fcm/MessagingService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.evollu.react.fcm;

import java.util.Map;
import android.content.Intent;
import android.util.Log;
import me.leolin.shortcutbadger.ShortcutBadger;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
Expand All @@ -16,6 +18,26 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "Remote message received");
Intent i = new Intent("com.evollu.react.fcm.ReceiveNotification");
i.putExtra("data", remoteMessage);
handleBadge(remoteMessage);
sendOrderedBroadcast(i, null);
}
}

public void handleBadge(RemoteMessage remoteMessage) {
BadgeHelper badgeHelper = new BadgeHelper(this);
if (remoteMessage.getData() == null) {
return;
}

Map data = remoteMessage.getData();
if (data.get("badge") == null) {
return;
}

try {
int badgeCount = Integer.parseInt((String)data.get("badge"));
badgeHelper.setBadgeCount(badgeCount);
} catch (Exception e) {
Log.e(TAG, "Badge count needs to be an integer", e);
}
}
}

0 comments on commit 62dce36

Please sign in to comment.