单击通知后,我无法重置静态变量

时间:2014-12-12 03:58:42

标签: android google-cloud-messaging android-notifications

我有3种类型的通知。每个都有自己唯一的ID,我给服务器。我还使用了3个静态变量来计算通知。一切都在我需要的地方运作。但是,当我通过单击其中一个通知打开程序然后再次折叠时,如果计数器未重置,则会发出新通知。这就是我使用静态变量的原因。我重置时给我?如何访问它并在组通知的开头重置。

public class GCMIntentService extends GCMBaseIntentService {
    private static int ID_CHECKIN =1;
    private static int ID_CHECKOUT =1;
    private static int ID_NEW_COMMENT =1;
    public GCMIntentService() {
        super(SENDER_ID);
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        String title = intent.getStringExtra("title");
        String message = intent.getStringExtra("content");
        int groupid = Integer.parseInt(intent.getStringExtra("group_id"));
        switch (groupid){
            case 1:
                generateNotification(context,title, message,groupid,ID_CHECKIN++);
                break;
            case 2:
                generateNotification(context,title, message,groupid,ID_CHECKOUT++);
                break;
            case 3:
                generateNotification(context,title, message,groupid,ID_NEW_COMMENT++);
                break;
        }
    }

    private void generateNotification(Context context, String title, String message,int groupid,int count) {

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        Intent intent = new Intent(context,MyActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification   = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.drawable.ic_stat_gcm)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_stat_gcm))
                .setTicker("Новое сообщение")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle(title+": "+count)
                .setContentText(message)
                .build();
        notification.number=count;
        notificationManager.notify(groupid, notification);
    }
}

0 个答案:

没有答案