0

I use GCM in my application I want when I receive notification the number in badge increment I'm using this library implementation "me.leolin:ShortcutBadger:1.1.21@aar" in the receive I implemented this code in onMessageReceived:

PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri1 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(appGCMModel.getGcmTitle())
        .setContentText(appGCMModel.getGcmMessage())
        .setAutoCancel(true)
        .setSound(defaultSoundUri1)
        .setContentIntent(pendingIntent2)
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(appGCMModel.getGcmMessage()));

Notification notification = notificationBuilder.build();

ShortcutBadger.applyNotification(getApplicationContext(), notification, i);

NotificationManager notificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);

but when the notification is received the number of badge isn't incremented can any one help me?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Jhon
  • 101
  • 1
  • 11

1 Answers1

1

This is because you didn't increment the count. you need to save the value in SharedPreferences. Something like this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

// get previous count
int count = prefs.getInt("badge_count", 0);
// increment the count
count = count + 1;

// save the new value.
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("badge_count", count);
editor.commit();

// Set to the badge
ShortcutBadger.applyNotification(getApplicationContext(), notification, count);

Please do not forget that ShortcutBadger.applyNotification() is only for Xiaomi devices. Read more at Xiaomi Device Support.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96