2012-09-23 9 views
8

こんにちは、私はすべての通知を単一のビューに表示したいと思うし、ステータスバーの通知の数を更新したい...そのすべての情報を更新しているが、常に数字を表示している。それを解決するために...通知番号を更新するには

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

答えて

18

件数を把握する必要があります。あなたは、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); 
+12

かなりばかげフレームワークは単純にこれをチェックするための単純な 'getNotifications(int id)'コールを持っていません... – clu

+5

残念なことに、アプリケーションが終了した場合、カウンタはリセットされます。永続性のSharedPreference – xnagyg

+1

通知を開いてカウンタをリセットするタイミングを知る方法を教えてください... – Micro

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

通知を後で更新することも簡単です。新しい値で再度メソッドを呼び出すだけで済みます。通知アイコンが作成されたときに番号が0より大きい場合に限り、通知アイコンバッジに番号が表示されます。

同様に、数字が1未満の番号に設定されていると、番号バッジは非表示になりません(番号はあなたになります)。再表示する前に通知をクリアしてしまう可能性があります。

0

件数を把握しておく必要があります。 notif.numberで実行しようとしているインクリメントは、その状態が利用できないため動作しません(notif.numberは常に0で、次に1に増やします)。どこかのアプリ(おそらく共有好み、)内の数字を追跡すると、増分およびそこにそれを格納し、あなたが通知を構築する際に、

notif.number = myStoredValueForWhatShouldShowInNumber; 

を設定する試みをすることを与えます。

関連する問題