From f03372adbd4424e2b85ca83b27ed6114a73f9edd Mon Sep 17 00:00:00 2001 From: Arindam Nayak Date: Mon, 3 Apr 2017 18:20:07 +0530 Subject: [PATCH 1/4] added shortcutbadger for badge in android --- src/android/FCMPlugin.gradle | 1 + src/android/MyFirebaseMessagingService.java | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/android/FCMPlugin.gradle b/src/android/FCMPlugin.gradle index e69195eef..c560e0f2c 100644 --- a/src/android/FCMPlugin.gradle +++ b/src/android/FCMPlugin.gradle @@ -6,6 +6,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:+' classpath 'com.google.gms:google-services:+' + compile 'me.leolin:ShortcutBadger:1.1.11@aar' } } // apply plugin: 'com.google.gms.google-services' diff --git a/src/android/MyFirebaseMessagingService.java b/src/android/MyFirebaseMessagingService.java index 941e7ca75..42c78cd08 100644 --- a/src/android/MyFirebaseMessagingService.java +++ b/src/android/MyFirebaseMessagingService.java @@ -14,6 +14,8 @@ import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; +import me.leolin.shortcutbadger.ShortcutBadgeException; +import me.leolin.shortcutbadger.ShortcutBadger; /** * Created by Felipe Echanique on 08/06/2016. */ @@ -35,6 +37,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) { // message, here is where that should be initiated. See sendNotification method below. Log.d(TAG, "==> MyFirebaseMessagingService onMessageReceived"); + setBadgeCount(remoteMessage); if( remoteMessage.getNotification() != null){ Log.d(TAG, "\tNotification Title: " + remoteMessage.getNotification().getTitle()); Log.d(TAG, "\tNotification Message: " + remoteMessage.getNotification().getBody()); @@ -54,6 +57,15 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } // [END receive_message] + private void setBadgeCount(RemoteMessage remoteMessage){ + try { + int badgeCount = Integer.parseInt(remoteMessage.getData().get("badge")); + ShortcutBadger.applyCountOrThrow(getApplicationContext(), badgeCount); + Log.d(TAG, "showBadge worked!"); + } catch (ShortcutBadgeException e) { + Log.e(TAG, "showBadge failed: " + e.getMessage()); + } + } /** * Create and show a simple notification containing the received FCM message. * From 9fc0a3781f0112dc995d0f5239c94d0e89d98e62 Mon Sep 17 00:00:00 2001 From: Arindam Nayak Date: Tue, 4 Apr 2017 18:18:21 +0530 Subject: [PATCH 2/4] on tapping notification it will update badge count and fixed gradle issue --- plugin.xml | 1 + src/android/BadgeHelper.java | 40 +++++++++++++++++++++ src/android/FCMPlugin.gradle | 7 ++-- src/android/FCMPluginActivity.java | 3 +- src/android/MyFirebaseMessagingService.java | 3 +- 5 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 src/android/BadgeHelper.java diff --git a/plugin.xml b/plugin.xml index 2d9aeb2df..196fdcb15 100644 --- a/plugin.xml +++ b/plugin.xml @@ -74,6 +74,7 @@ + diff --git a/src/android/BadgeHelper.java b/src/android/BadgeHelper.java new file mode 100644 index 000000000..8b88b6809 --- /dev/null +++ b/src/android/BadgeHelper.java @@ -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; + } + } +} diff --git a/src/android/FCMPlugin.gradle b/src/android/FCMPlugin.gradle index c560e0f2c..398122c81 100644 --- a/src/android/FCMPlugin.gradle +++ b/src/android/FCMPlugin.gradle @@ -6,9 +6,12 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:+' classpath 'com.google.gms:google-services:+' - compile 'me.leolin:ShortcutBadger:1.1.11@aar' + } } // 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 \ No newline at end of file +apply plugin: com.google.gms.googleservices.GoogleServicesPlugin +dependencies { + compile 'me.leolin:ShortcutBadger:1.1.11@aar' +} \ No newline at end of file diff --git a/src/android/FCMPluginActivity.java b/src/android/FCMPluginActivity.java index c613f5ee7..4489abf36 100644 --- a/src/android/FCMPluginActivity.java +++ b/src/android/FCMPluginActivity.java @@ -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(); diff --git a/src/android/MyFirebaseMessagingService.java b/src/android/MyFirebaseMessagingService.java index 42c78cd08..2dbe945d4 100644 --- a/src/android/MyFirebaseMessagingService.java +++ b/src/android/MyFirebaseMessagingService.java @@ -14,8 +14,6 @@ import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; -import me.leolin.shortcutbadger.ShortcutBadgeException; -import me.leolin.shortcutbadger.ShortcutBadger; /** * Created by Felipe Echanique on 08/06/2016. */ @@ -50,6 +48,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 ); From 93097362af806b4e529328ced17c304fe170d3d3 Mon Sep 17 00:00:00 2001 From: Arindam Nayak Date: Tue, 4 Apr 2017 18:20:13 +0530 Subject: [PATCH 3/4] removed redundant code --- src/android/MyFirebaseMessagingService.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/android/MyFirebaseMessagingService.java b/src/android/MyFirebaseMessagingService.java index 2dbe945d4..f513a0a56 100644 --- a/src/android/MyFirebaseMessagingService.java +++ b/src/android/MyFirebaseMessagingService.java @@ -35,7 +35,6 @@ public void onMessageReceived(RemoteMessage remoteMessage) { // message, here is where that should be initiated. See sendNotification method below. Log.d(TAG, "==> MyFirebaseMessagingService onMessageReceived"); - setBadgeCount(remoteMessage); if( remoteMessage.getNotification() != null){ Log.d(TAG, "\tNotification Title: " + remoteMessage.getNotification().getTitle()); Log.d(TAG, "\tNotification Message: " + remoteMessage.getNotification().getBody()); @@ -56,15 +55,6 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } // [END receive_message] - private void setBadgeCount(RemoteMessage remoteMessage){ - try { - int badgeCount = Integer.parseInt(remoteMessage.getData().get("badge")); - ShortcutBadger.applyCountOrThrow(getApplicationContext(), badgeCount); - Log.d(TAG, "showBadge worked!"); - } catch (ShortcutBadgeException e) { - Log.e(TAG, "showBadge failed: " + e.getMessage()); - } - } /** * Create and show a simple notification containing the received FCM message. * From d4c649e9d7463d0fc6663d9f4a39799194488e71 Mon Sep 17 00:00:00 2001 From: Arindam Nayak Date: Mon, 5 Jun 2017 22:36:09 +0530 Subject: [PATCH 4/4] Update FCMPlugin.gradle --- src/android/FCMPlugin.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/android/FCMPlugin.gradle b/src/android/FCMPlugin.gradle index 80e4fbdd0..6ab69d5e1 100644 --- a/src/android/FCMPlugin.gradle +++ b/src/android/FCMPlugin.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:+' - classpath 'com.google.gms:google-services:+' + classpath 'com.google.gms:google-services:3.0.0' } } // apply plugin: 'com.google.gms.google-services' @@ -13,4 +13,4 @@ buildscript { apply plugin: com.google.gms.googleservices.GoogleServicesPlugin dependencies { compile 'me.leolin:ShortcutBadger:1.1.11@aar' -} \ No newline at end of file +}