마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

GCM 카운트 문제점....

0 추천

 

package com.example.testview;
 
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
 
import com.google.android.gcm.GCMBaseIntentService;
 
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class GCMIntentService extends GCMBaseIntentService {
 
static String re_message = null;
private static int SDK_INT = android.os.Build.VERSION.SDK_INT;
 
static int number;
static int count = 0;
 
static NotificationManager notifier = null;
static final int NOTIFY_ID = 0x1001;
 
@SuppressWarnings("deprecation")
private static void generateNotification(Context context, String message) {
// int count = 0;
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
re_message = message;
 
if (SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
// count++;
// 진저브래드 이상의 버전에서만 실행 가능한 API 사용
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
 
Intent intent = new Intent(context, MainActivity.class);
 
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
System.out.println("Notification 더하기 전 ::" + count);
builder.setSmallIcon(icon).setContentTitle("ContentTitle")
.setContentText(message)
// .setContentInfo("message")
 
.setNumber(count++)
 
// builder.setTicker("Ticker");
.setLights(0xFFFF0000, 500, 500) // setLights (int argb, int
// onMs, int offMs)
.setContentIntent(pendingIntent);
System.out.println("Notification 더하기 후 ::" + count);
builder.setAutoCancel(true);
// notificationManager.cancel(notifyID);
 
// Notification notification = builder.getNotification();
 
// notificationManager.notify(R.drawable.ic_launcher, notification);
notificationManager.notify(0, builder.getNotification());
} else {
System.out.println("Notification 버전  === ::" + "진저버전");
 
System.out.println("Notification number값 전  === ::" + number);
 
notifier = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
//showNotification();
Notification notify = new Notification(icon, message, when);
 
String title = context.getString(R.string.app_name);
 
number = 0;
if (re_message != null) {
notify.number = number++;
System.out.println("Notification 증가  === ::" + notify.number);
 
} else if (re_message == null) {
number = 0;
System.out.println("Notification 초기화  === ::" + notify.number);
}
 
System.out
.println("Notification number값 후  === ::" + notify.number);
 
Intent notificationIntent = new Intent(context, MainActivity.class);
 
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
 
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
 
notify.setLatestEventInfo(context, title, message, intent);
 
notifier.notify(NOTIFY_ID, notify);
}
}
 
@Override
protected void onError(Context arg0, String arg1) {
if (BuildConfig.DEBUG)
Log.d("오류 처리", "onError: " + arg1);
// 오류 발생 시 처리
 
}
 
@Override
protected void onMessage(Context context, Intent intent) {
 
String msg = intent.getStringExtra("msg");
 
Log.e("메세지 오는것??", "메세지????:::::::::::::::" + msg);
generateNotification(context, msg);
 
}
 
@Override
protected void onRegistered(Context context, String reg_id) {
 
Log.e("키를 등록합니다.(GCM INTENTSERVICE)", reg_id);
}
 
@Override
protected void onUnregistered(Context arg0, String arg1) {
 
Log.e("키를 제거합니다.(GCM INTENTSERVICE)", "제거되었습니다.");
 
}
 
@Override
protected void onDeletedMessages(Context context, int total) {
if (BuildConfig.DEBUG)
Log.d("메세지를 삭제", "onDeletedMessages");
// 메세지 삭제
}
 
@Override
protected boolean onRecoverableError(Context context, String errorId) {
if (BuildConfig.DEBUG)
Log.d("리시벌 에러", "onRecoverableError: " + errorId);
return super.onRecoverableError(context, errorId);
}
 
}
 
소스는 위와 같습니다. 문제는 카운트부분입니다. static 이 부분을 사용하는데 초기화 방법을 어찌 해야 할지 모르겠고..
 
알림 Cancel 취소를 하면 1부터 다시 시작을 해야 하는데 계속적인 카운터가 되서 들어 옵니다. 이것을 어떻게 해야 하는지 문의 드립니다.
 
이전이후 (280 포인트) 님이 2013년 2월 21일 질문
아 저도 이거 문제던데 어떻게 처리할지 고수분이 않나타나는군요......

노티눌렀을경우 캔슬리스너라도 있다면 처리해볼텐데 막막하군요

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...