Commit 62dce368 authored by sondremare's avatar sondremare Committed by Libin Lu

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

* 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)
parent 1b1f8869
node_modules node_modules
build build
xcuserdata xcuserdata
.idea/
\ No newline at end of file
...@@ -12,10 +12,15 @@ android { ...@@ -12,10 +12,15 @@ android {
} }
} }
repositories {
mavenCentral()
}
dependencies { dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs') compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.facebook.react:react-native:+' compile 'com.facebook.react:react-native:+'
compile 'com.google.firebase:firebase-core:+' compile 'com.google.firebase:firebase-core:+'
compile 'com.google.firebase:firebase-messaging:+' compile 'com.google.firebase:firebase-messaging:+'
compile 'me.leolin:ShortcutBadger:1.1.10@aar'
} }
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();
}
}
...@@ -36,10 +36,12 @@ import java.util.UUID; ...@@ -36,10 +36,12 @@ import java.util.UUID;
public class FIRMessagingModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener { public class FIRMessagingModule extends ReactContextBaseJavaModule implements LifecycleEventListener, ActivityEventListener {
private final static String TAG = FIRMessagingModule.class.getCanonicalName(); private final static String TAG = FIRMessagingModule.class.getCanonicalName();
private FIRLocalMessagingHelper mFIRLocalMessagingHelper; private FIRLocalMessagingHelper mFIRLocalMessagingHelper;
private BadgeHelper mBadgeHelper;
public FIRMessagingModule(ReactApplicationContext reactContext) { public FIRMessagingModule(ReactApplicationContext reactContext) {
super(reactContext); super(reactContext);
mFIRLocalMessagingHelper = new FIRLocalMessagingHelper((Application) reactContext.getApplicationContext()); mFIRLocalMessagingHelper = new FIRLocalMessagingHelper((Application) reactContext.getApplicationContext());
mBadgeHelper = new BadgeHelper(reactContext.getApplicationContext());
getReactApplicationContext().addLifecycleEventListener(this); getReactApplicationContext().addLifecycleEventListener(this);
getReactApplicationContext().addActivityEventListener(this); getReactApplicationContext().addActivityEventListener(this);
registerTokenRefreshHandler(); registerTokenRefreshHandler();
...@@ -113,6 +115,16 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li ...@@ -113,6 +115,16 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li
promise.resolve(array); 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) { private void sendEvent(String eventName, Object params) {
getReactApplicationContext() getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
......
package com.evollu.react.fcm; package com.evollu.react.fcm;
import java.util.Map;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
import me.leolin.shortcutbadger.ShortcutBadger;
import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage; import com.google.firebase.messaging.RemoteMessage;
...@@ -16,6 +18,26 @@ public class MessagingService extends FirebaseMessagingService { ...@@ -16,6 +18,26 @@ public class MessagingService extends FirebaseMessagingService {
Log.d(TAG, "Remote message received"); Log.d(TAG, "Remote message received");
Intent i = new Intent("com.evollu.react.fcm.ReceiveNotification"); Intent i = new Intent("com.evollu.react.fcm.ReceiveNotification");
i.putExtra("data", remoteMessage); i.putExtra("data", remoteMessage);
handleBadge(remoteMessage);
sendOrderedBroadcast(i, null); 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);
}
}
} }
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment