Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(android): add support for badge count #207

Merged
merged 2 commits into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}