Android显示并删除通知徽章编号

时间:2020-08-07 09:54:16

标签: java android firebase notifications

我遇到了有关通知徽章编号的问题。

现在在我的代码中,用户能够从通知栏或应用程序本身打开通知,但是一旦打开应用程序,徽章就会消失。

任何人都知道:

  1. 如何在启动器图标上添加通知徽章编号。
  2. 删除通知栏,并在用户打开应用程序时将徽章编号保留在启动器图标上。
  3. 当API中的数据返回为0时,删除徽章编号。

我使用https://github.com/leolin310148/ShortcutBadger作为显示徽章的库,但似乎无法正常工作。徽章根本不显示。

这些是我有关FCM通知的代码

public class FCMService extends FirebaseMessagingService

{ @Override 公共无效onNewToken(@NonNull字符串令牌) { Log.d(“ TAG”,“令牌已刷新” +令牌); }

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
    if(remoteMessage.getData() != null)
    {
        Map<String, String> data = remoteMessage.getData();

        String title = data.get("title");
        String content = data.get("content");

        if(MyApplication.isActivityVisible())
        {
            showNotification(title, content);
        }
        else
        {
            showNotification(title, content);
        }
    }
}

private RemoteViews getCustomDesign(String title, String message)
{
    RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification);
    remoteViews.setTextViewText(R.id.tvNotificationTitle, title);
    remoteViews.setTextViewText(R.id.tvNotificationMessage, message);
    remoteViews.setImageViewResource(R.id.icon, R.mipmap.cre_logo);

    return remoteViews;
}

public void showNotification(String title, String message)
{
    Intent intent = new Intent(this, LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

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

    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NotificationUtil.NOTIFICATION_CHANNEL_ID_LOCATION)
            .setSmallIcon(R.mipmap.cre_logo)
            .setSound(uri)
            .setAutoCancel(true)
            .setNumber(99)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setVibrate(new long[]{ 1000,1000,1000,1000,1000 })
            .setOnlyAlertOnce(true)
            .setContentIntent(pendingIntent);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
    {
        builder = builder.setContent(getCustomDesign(title, message));
    }
    else
    {
        builder = builder.setContentTitle(title)
                .setContentText(message)
                .setSmallIcon(R.mipmap.cre_logo);
    }

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        NotificationChannel notificationChannel =  new NotificationChannel(NotificationUtil.NOTIFICATION_CHANNEL_ID_LOCATION,"MitsubishiApp", NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setSound(uri,null);
        notificationChannel.enableLights(true);
        notificationChannel.enableVibration(true);
        notificationChannel.setShowBadge(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        notificationChannel.setVibrationPattern(new long[]{ 100, 300, 300, 300, 300});

        notificationManager.createNotificationChannel(notificationChannel);
    }

    notificationManager.notify(NotificationChannelID.getID(), builder.build());
}

我用这行代码删除了我应用中的通知栏/徽章

NotificationManager notificationManager = (NotificationManager)getActivity().getSystemService(NOTIFICATION_SERVICE);

notificationManager.cancelAll();

0 个答案:

没有答案