繁体   English   中英

将徽章添加到android应用程序

[英]Add badge to android application

我在我的应用程序中使用GCM,当我收到通知时,会以徽章递增的方式接收数字,我在接收中使用此库implementation "me.leolin:ShortcutBadger:1.1.21@aar"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);

但是收到通知后,证章的数量没有增加,有人可以帮忙吗?

这是因为您没有增加计数。 您需要将值保存在SharedPreferences中 像这样:

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);

请不要忘记ShortcutBadger.applyNotification()仅适用于小米设备。 小米设备支持中了解更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM