public class MyGcmListenerService extends GcmListenerService {
private final String TAG = "MyGcmListenerService";
//알림
private final String GCM_DATA_KEY_TITLE = "title";
private final String GCM_DATA_KEY_BODY = "body";
private static int NOTIFICATION_ID = 0;
private final long[] ALARM_PATTERN = new long[]{ 100, 100, 100, 100 };
//메시지 수신시 호출
@Override
public void onMessageReceived(String s, Bundle bundle) {
//메시지 수신 후 알림 띄우기
if( bundle != null ) {
Log.d(TAG, "onMessageReceived : "+bundle.toString());
String title = bundle.getString(GCM_DATA_KEY_TITLE , "");
String body = bundle.getString(GCM_DATA_KEY_BODY , "");
sendNotificaion(title , body);
}
}
//알림에 띄우기
public void sendNotificaion(String title, String body) {
NotificationManager notiManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String chGroupId = getString(R.string.alarm_ch_group_id_gcm);
String channelId = getString(R.string.alarm_ch_id_gcm);
//26 오레오 이상
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if( !checkNotificationChannelGroup(chGroupId) ) {
//알림 채널 그룹
NotificationChannelGroup notiGroup = new NotificationChannelGroup( chGroupId, getString(R.string.alarm_ch_group_name_gcm) );
notiManager.createNotificationChannelGroup( notiGroup );
}
if( !checkNotificationChannel(channelId) ) {
//알림 채널
NotificationChannel notiChannel = new NotificationChannel(channelId, getString(R.string.alarm_ch_name_gcm), NotificationManager.IMPORTANCE_HIGH);
notiChannel.setGroup(chGroupId);
notiChannel.enableLights(true);
notiChannel.setLightColor(Color.GREEN);
notiChannel.enableVibration(true);
notiChannel.setVibrationPattern( ALARM_PATTERN );
notiChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notiChannel.setShowBadge(true);
notiManager.createNotificationChannel(notiChannel);
}
}
//알림 생성
String appName = getString(R.string.app_name);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //이미 존재할 경우 이를 유지하고 추가 데이터만 새로운 Intent에 있는 것으로 대체
NotificationCompat.Builder noti = new NotificationCompat.Builder(this, channelId);
noti.setAutoCancel(true); //클릭시 알림 제거
noti.setContentTitle(title);
noti.setContentText(body);
noti.setTicker(appName);
noti.setSmallIcon(R.mipmap.ic_launcher);
noti.setContentIntent(pi);
noti.setVibrate( ALARM_PATTERN );
noti.setSound( RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) );
noti.setOngoing(false); //지우기 버튼으로 삭제 (true - 일반 알림 위에 정렬, 지우기 버튼으로 삭제 못함)
noti.setOnlyAlertOnce(true); //알림이 아직 표시되지 않은 경우 사운드, 진동, 티커만 재생되도록
noti.setWhen( System.currentTimeMillis() ); //알림에서 보여지는 발생시간
noti.setNumber(1); //알림의 오른쪽 번호
noti.setGroupSummary(true);
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
noti.setVisibility( Notification.VISIBILITY_SECRET ); //보안화면에서 알림 보이지 않음
}
try {
notiManager.notify(NOTIFICATION_ID++ , noti.build() );
} catch (Exception e) {
e.printStackTrace();
}
}
//notification channel group 등록 체크
public boolean checkNotificationChannelGroup(String groupId) {
boolean result = false; //true : 이미 등록됨 , false : 미등록
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
List<NotificationChannelGroup> groups = notificationManager.getNotificationChannelGroups();
if( groups != null && groups.size() > 0 ) {
for ( NotificationChannelGroup group : groups ) {
if( groupId.equalsIgnoreCase(group.getId()) ) {
result = true;
break;
}
}
}
}
return result;
}
//notification channel 등록 체크
public boolean checkNotificationChannel( String channelId ) {
if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
return notificationManager.getNotificationChannel(channelId) != null ? true : false;
}
return false;
}
}