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부터 다시 시작을 해야 하는데 계속적인 카운터가 되서 들어 옵니다. 이것을 어떻게 해야 하는지 문의 드립니다.