8

嗨,我想在一个视图中显示所有通知..并想更新状态栏中的通知数量...它更新所有信息但始终显示数字 1..请告诉我如何解决它...

@Override
public void onReceive(Context context, Intent intent)
{
    //Random randGen = new Random();
    //int notify_id = randGen.nextInt();
    NotificationManager notificationManager = (NotificationManager)
        context.getSystemService(Activity.NOTIFICATION_SERVICE);
    String title = intent.getStringExtra(TableUtils.KEY_TITLE);
    String occasion = intent.getStringExtra(TableUtils.KEY_OCCASION);
    Notification notification = 
        new Notification(R.drawable.icon, "Love Cardz" , 
                         System.currentTimeMillis());
    // notification.vibrate = new long[]{100,250,300,330,390,420,500};
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.number+=1;
    Intent intent1 = new Intent(context, ThemesBrowserActivity.class);
    PendingIntent activity = 
        PendingIntent.getActivity(context, 1 , intent1, 
                                  PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, occasion, title, activity);
    notificationManager.notify(1, notification);
}
4

3 回答 3

19

您必须跟踪计数。您可以扩展 Application 类:

public class AppName extends Application {
    private static int pendingNotificationsCount = 0;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public static int getPendingNotificationsCount() {
        return pendingNotificationsCount;
    }

    public static void setPendingNotificationsCount(int pendingNotifications) {
        pendingNotificationsCount = pendingNotifications;
    }
}

你应该修改onReceive:

@Override
public void onReceive(Context context, Intent intent) {
    ...
    int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1;
    AppName.setPendingNotificationsCount(pendingNotificationsCount);
    notification.number = pendingNotificationsCount;
    ...
}

您可以在用户打开通知时重置计数:

AppName.setPendingNotificationsCount(0);
于 2013-02-17T03:41:18.433 回答
4

这是我的代码,它有效。我只在旧的 Android 版本上进行了测试。我怀疑在较新的版本中,“数字”徽章已变得不可见,但我还没有机会对其进行测试。

void notifyMsgReceived(String senderName, int count) {
    int titleResId;
    String expandedText, sender;

    // Get the sender for the ticker text
    // i.e. "Message from <sender name>"
    if (senderName != null && TextUtils.isGraphic(senderName)) {
        sender = senderName;
    }
    else {
        // Use "unknown" if the sender is unidentifiable.
        sender = getString(R.string.unknown);
    }

    // Display the first line of the notification:
    // 1 new message: call name
    // more than 1 new message: <number of messages> + " new messages"
    if (count == 1) {
        titleResId = R.string.notif_oneMsgReceivedTitle;
        expandedText = sender;
    }
    else {
        titleResId = R.string.notif_missedCallsTitle;
        expandedText = getString(R.string.notif_messagesReceivedTitle, count);
    }

    // Create the target intent
    final Intent intent = new Intent(this, TargetActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final PendingIntent pendingIntent =
        PendingIntent.getActivity(this, REQUEST_CODE_MSG_RECEIVED, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Build the notification
    Notification notif = new Notification(
        R.drawable.ic_notif_msg_received,  // icon
        getString(R.string.notif_msgReceivedTicker, sender), // tickerText
        System.currentTimeMillis()); // when
        notif.setLatestEventInfo(this, getText(titleResId), expandedText, pendingIntent);
    notif.number = count;
    notif.flags = Notification.FLAG_AUTO_CANCEL;

    // Show the notification
    mNotificationMgr.notify(NOTIF_MSG_RECEIVED, notif);
}

稍后更新通知也很容易:您只需使用新值再次调用该方法。当且仅当创建通知时该数字大于零时,该数字才会显示在通知图标徽章中。

同样,如果您将数字设置为小于 1 的数字,则不会隐藏数字徽章(数字会隐藏)。也许在重新显示通知之前清除通知可以修复它。

于 2012-10-09T16:16:37.707 回答
0

您必须跟踪计数。您尝试在 notif.number 上执行的增量不起作用,因为该状态不可用(即 notif.number 始终为 0,然后您将其增加到 1)。跟踪应用程序中某处的数字(可能是共享首选项),并将其递增并存储在那里,然后当您构建通知时,设置

notif.number = myStoredValueForWhatShouldShowInNumber;

试试看。

于 2012-11-19T19:13:32.873 回答