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

Added support to show badge count on app icon in supported android devices #279

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
<source-file src="src/android/MyFirebaseMessagingService.java" target-dir="src/com/gae/scaffolder/plugin" />
<source-file src="src/android/MyFirebaseInstanceIDService.java" target-dir="src/com/gae/scaffolder/plugin" />
<source-file src="src/android/FCMPluginActivity.java" target-dir="src/com/gae/scaffolder/plugin" />
<source-file src="src/android/BadgeHelper.java" target-dir="src/com/gae/scaffolder/plugin" />
</platform>

<!-- IOS CONFIGURATION -->
Expand Down
40 changes: 40 additions & 0 deletions src/android/BadgeHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.gae.scaffolder.plugin;

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

import me.leolin.shortcutbadger.ShortcutBadgeException;
import me.leolin.shortcutbadger.ShortcutBadger;

/**
* Created by ArindamN on 04/04/2017.
*/
public class BadgeHelper {
private static String TAG = "FCM_BADGE_HELPER";
public final static String CONST_BADGE_KEY = "badge";

public static void setBadgeCount(String badge, Context ctx){
try {
//String badge = (String) remoteMessage.get("badge");
int badgeCount = 0;
if(badge!=null && !badge.isEmpty()){
badgeCount = tryParseInt(badge);
}
if(badgeCount>0)
{
ShortcutBadger. applyCountOrThrow(ctx, badgeCount);
Log.d(TAG, "showBadge worked!");
}

} catch (ShortcutBadgeException e) {
Log.e(TAG, "showBadge failed: " + e.getMessage());
}
}
static int tryParseInt(String value) {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
return 0;
}
}
}
5 changes: 4 additions & 1 deletion src/android/FCMPlugin.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ buildscript {
}
// apply plugin: 'com.google.gms.google-services'
// class must be used instead of id(string) to be able to apply plugin from non-root gradle file
apply plugin: com.google.gms.googleservices.GoogleServicesPlugin
apply plugin: com.google.gms.googleservices.GoogleServicesPlugin
dependencies {
compile 'me.leolin:ShortcutBadger:1.1.11@aar'
}
3 changes: 2 additions & 1 deletion src/android/FCMPluginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void onCreate(Bundle savedInstanceState) {
data.put(key, value);
}
}


BadgeHelper.setBadgeCount((String) data.get(BadgeHelper.CONST_BADGE_KEY), getApplicationContext());
FCMPlugin.sendPushPayload(data);

finish();
Expand Down
1 change: 1 addition & 0 deletions src/android/MyFirebaseMessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "\tKey: " + key + " Value: " + value);
data.put(key, value);
}
BadgeHelper.setBadgeCount((String)data.get(BadgeHelper.CONST_BADGE_KEY), getApplicationContext());

Log.d(TAG, "\tNotification Data: " + data.toString());
FCMPlugin.sendPushPayload( data );
Expand Down