如何增加android中的通知计数

时间:2014-08-14 10:09:29

标签: android

public class GcmIntentService extends IntentService {
    Context context;
    public static final int NOTIFICATION_ID = 100;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    public static final String TAG = "GCM Demo";
    String msg;
    String senderID;
    String recieverID;
    String parameter = "5";
    String time;
    private Handler handler;

    int count = 0;

    public GcmIntentService() {
        super("GcmIntentService");
        // TODO Auto-generated constructor stub
        handler = new Handler();
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        // Bundle extras = intent.getExtras();
        msg = intent.getStringExtra("message");
        senderID = intent.getStringExtra("sender_id");
        recieverID = intent.getStringExtra("reccvier_id");
        // parameter = intent.getStringExtra("parameter");
        time = intent.getStringExtra("time");

        sendNotification(msg, senderID);

        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg, String sender_id) {

        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        int params = 0;
        Intent myintent;

        if (parameter != null && !"".equalsIgnoreCase(parameter)) {
            params = Integer.parseInt(parameter);
        }

        if (params == 1) {
            myintent = new Intent(this, HomeActivity.class);
            myintent.putExtra("message", msg);
            myintent.putExtra("parmater", 2);
            myintent.putExtra("to_id", sender_id);
            myintent.putExtra("time", time);
        } else {
            myintent = new Intent(this, ChatActivity.class);
            myintent.putExtra("message", msg);
            myintent.putExtra("to_id", sender_id);
            myintent.putExtra("time", time);

        }
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.lokii_notification_icon)
                .setContentTitle("Locii Messge").setAutoCancel(true)
                .setNumber(count++)
                // .setSound(Uri.parse("android.resource://package_name/raw/sound"))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setNumber(++count).setContentText(msg);

        mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        mBuilder.setContentIntent(contentIntent);

        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

这是我的屏幕:http://snag.gy/dUFrM.jpg

此通知代码我在通知管理器中收到通知。但是,如果他们发送了5个通知并且我没有阅读通知,我就无法显示计数器。

我正在尝试使用count = 0;在通知构建器计数++但我总是设置1而我发送超过1个通知我的通知消息已更新但计数器没有增加请建议我在哪里做错了。

1 个答案:

答案 0 :(得分:1)

将静态变量用于计数

static int count=0;

希望它会帮助你

    Context context;
    public static final int NOTIFICATION_ID = 100;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    public static final String TAG = "GCM Demo";
    String msg;
    String senderID;
    String recieverID;
    String parameter = "5";
    String time;
    private Handler handler;

    int count = 0;

    public GcmIntentService() {
        super("GcmIntentService");
        // TODO Auto-generated constructor stub
        handler = new Handler();
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        // Bundle extras = intent.getExtras();
        msg = intent.getStringExtra("message");
        senderID = intent.getStringExtra("sender_id");
        recieverID = intent.getStringExtra("reccvier_id");
        // parameter = intent.getStringExtra("parameter");
        time = intent.getStringExtra("time");

        sendNotification(msg, senderID);

        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    private void sendNotification(String msg, String sender_id) {

        mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        int params = 0;
        Intent myintent;

        if (parameter != null && !"".equalsIgnoreCase(parameter)) {
            params = Integer.parseInt(parameter);
        }

        if (params == 1) {
            myintent = new Intent(this, HomeActivity.class);
            myintent.putExtra("message", msg);
            myintent.putExtra("parmater", 2);
            myintent.putExtra("to_id", sender_id);
            myintent.putExtra("time", time);
        } else {
            myintent = new Intent(this, ChatActivity.class);
            myintent.putExtra("message", msg);
            myintent.putExtra("to_id", sender_id);
            myintent.putExtra("time", time);

        }
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                myintent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.lokii_notification_icon)
                .setContentTitle("Locii Messge").setAutoCancel(true)
                // .setSound(Uri.parse("android.resource://package_name/raw/sound"))
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setNumber(count).setContentText(msg);


        mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
        mBuilder.setContentIntent(contentIntent);

        mNotificationManager.notify(count, mBuilder.build());

 count++;
        }
    }