Push 서비스에서 Push가 올 경우 카카오톡 처럼 어플 icon이 아닌 어플 안에 들어있는 버튼에
Badge를 추가 해주려고 하는데 잘 안되서 이렇게 글을 적습니다.
같은 .JAVA 에서는 버튼을 생성하여 클릭 시 뱃지 화면이 뜨고 있으나,
다른 .JAVA에서 푸시 노티피케이션 부분과 메시지 받는 부분에 이벤트 발생시 SharedPreferences를 이용하여 뱃지를 다른 .JAVA에 있는 버튼화면에 나오도록 설정 해보았으나 반응이 없습니다.
GCMIntentService.java 에는 setNotification과 onMessage가 있는데 각각 내용에는 아래와 같이 소스값을 작성하였습니다.
@Override
protected void onMessage(Context context, Intent intent) {
// TODO Auto-generated method stub
...
생략
...
if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
...
생략
...
if (BuildConfig.DEBUG)
Log.d(TAG, "GCMReceiver Message");
try {
// 뱃지 카운터를 올리기 위한 부분
// InActivity.java에 badge1 값을 가져와서 사용하겠다고 선언 하는 부분
InActivity BdK = new InActivity();
BadgeView bg = BdK.bd1;
InActivity BuK = new InActivity();
bg = new BadgeView(this, BuK.btn3);
bg.setBackgroundResource(R.drawable.badge);
nbs.setText("N");
nbs.setTextColor(Color.WHITE);
nbs.setTextSize(12);
SharedPreferences NB = getSharedPreferences("NB", MODE_PRIVATE);
boolean isMess = NB.getBoolean("isMess", true);
if(isMess) {
nbs.show();
isMess = true;
SharedPreferences.Editor sEdit = NB.edit();
sEdit.putBoolean("isMess", isMess);
sEdit.commit();
}
...
메시지 보여주는곳, 생략
...
} catch (Exception e) {
Log.e(TAG, "[onMess] : " + e.getMessage());
}
}
}
private void setNotification(Context context, String title, String message) {
NotificationManager notificationManager = null;
Notification notification = null;
try {
// 뱃지 부분 체크해 봐야 함
InActivity BdK = new InActivity();
BadgeView bg = BdK.bd1;
InActivity BuK = new InActivity();
bg = new BadgeView(this, BuK.btn3);
bg.setBackgroundResource(R.drawable.badge);
nbs.setText("N");
nbs.setTextColor(Color.WHITE);
nbs.setTextSize(12);
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.icon, message, System.currentTimeMillis());
Intent intent = new Intent(context, WebBrowser3.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, title, message, pi);
notificationManager.notify(0, notification);
if(WB3.class.isAnnotation()) {
SharedPreferences NB = getSharedPreferences("NB", MODE_PRIVATE);
boolean isMess = NB.getBoolean("isMess", true);
if(isMess) {
// 뱃지 값 숨기기
bg.hide();
// 다이얼로그를 닫을 때 처리
isMess = true;
SharedPreferences.Editor sEdit = NB.edit();
sEdit.remove("isMess");
sEdit.commit();
}
}
} catch (Exception e) {
Log.e(TAG, "[setNotification] : " + e.getMessage());
}
}
InActivity.java 에는 다음과 같이 정의를 해 두었습니다.
public class InActivity extends Activity {
...
생략
...
Button btn3;
BadgeView bd1;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
생략
...
btn3 = (Button) findViewById(R.id.am3);
// 뱃지에 들어가는 값 선언
bd1 = new BadgeView(this, btn3);
bd1.setBackgroundResource(R.drawable.badge);
bd1.setText("N");
bd1.setTextColor(Color.WHITE);
bd1.setTextSize(12);
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(bd1.isShown()) {
SharedPreferences NB = getSharedPreferences("NB", MODE_PRIVATE);
boolean isMessage = NB.getBoolean("isMess", true);
if(isMess) {
Intent int3 = new Intent(InActivity.this, WB3.class);
startActivity(int3);
// 뱃지 값 숨기기
bd1.hide();
// 다이얼로그를 닫을 때 처리
isMess = true;
SharedPreferences.Editor sEdit = NB.edit();
sEdit.remove("isMess");
sEdit.commit();
}
} else {
Intent int3 = new Intent(InActivity.this, WB3.class);
startActivity(int3);
}
}
});
...
생략
...
}