'Android Studio/개발'에 해당되는 글 4건

  1. 2019.04.03 [안드로이드] -FCM 푸시알림
  2. 2019.04.01 [안드로이드]-버튼 배경 투명하게 하는 방법

가끔 휴대폰을 하다보면 앱에서 알림이 온것을 본적 있으신가요?


오늘은 구글의 FCM을 이용하여 앱 사용자에게 메세지를 보내는 방법에 대해 알아보겠습니다.


현재 안드로이드 신규 버전인 9.0파이버전부터는 GCM이 정상적인 작동을 하지 않습니다.

( 관련 링크 : https://developers.google.com/cloud-messaging/faq )







먼저 FCM관련 콘솔 홈페이지로 접속합니다.

( 주소 : https://console.firebase.google.com/ )


▲fcm페이지


페이지에 접속후 왼쪽에 + 모양으로 프로젝트 추가를 눌러줍니다.



▲프로젝트 추가 화면


프로젝트 이름과 위치를 대한민국 으로 설정해줍니다.



▲FCM 화면


그후 안드로이드 모양의 아이콘을 클릭해줍니다.


▲안드로이드 모양 클릭시 화면


안드로이드 패키지이름 , 앱 닉네임 을 입력하신뒤 앱등록을 눌러줍니다.

※패키지이름은 안드로이드 build.gradle(Module:app) 부분의 6번째줄에 존재합니다.



▲FCM 이미지1


▲FCM 이미지2





구글 구성파일을 다운로드 하신뒤 설명에 나온대로 따라합니다.


이후 메니페스트에 들어가셔서



</application>위에 

<service android:name=".My_Firebase_Messaging_Service" tools:ignore="ExtraText">
    <intent-filter>
        <action
android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
   
ACCESS_FINE_LOCATION
</service>

코드를 넣어줍니다


▲메니페스트 사진



여기까지 하셨다면 FCM의 기본적인 셋팅과 프로젝트 내부 Gradle 셋팅 은 모두 끝나셨습니다.

이제 FCM 구현이 남아있습니다.




프로젝트 내에 My_Firebase_Messaging_Service 라는 클래스 파일을 생성한 뒤 아래와 같이 구현합니다.


onMessageReceived

매소드를 생성후 


String bodyFromServer = remoteMessage.getNotification().getBody();

 
NotificationCompat.Builder mBuilder =
        
new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher)
       

.setContentTitle("PHSH NOtification")

.setContentText(bodyFromServer);
// Creates an explicit intent for an Activity in your ap


Intent resultIntent = new Intent(this, Activity_Firebase_Result.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(Activity_Firebase_Result.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent =
           stackBuilder.getPendingIntent(
               
0,
            
PendingIntent.FLAG_UPDATE_CURRENT
        
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
       (NotificationManager) getSystemService(Context.
NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.

mNotificationManager.notify(0, mBuilder.build());


메소드 안에 위 코드를 넣어줍니다.




\

My_Firebase_Messaging_Service.Class



이후에 Activity_Firebase_Result.Class도 하나 생성해줍니다.


Activity_Firebase_Result.Class




여기까지 하셨다면 이제 모두 끝났습니다.



이제 Firebase 홈페이지 왼쪽 아래 Cloud Messaging을 클릭해서 새알림 클릭후 메세지를 보내면 됩니다!


▲Cliud Messaging



소스 코드는 https://github.com/AddChan/FCM_Push_project 에 있습니다.



Posted by AddChan
,

안드로이드 스튜디오 버튼 배경 투명색 코드는 

 

#00FFFFFF 입니다.

 

Background 에 넣어주면 클릭 효과가 사라지는 현상이 있습니다.

 

해결방법

 

xml 코드에

▲35번째 줄

 

35번째 줄을 넣어주면 뒷 배경은 투명하며 버튼 클릭효과는 그대로 나타납니다.

 

 

 

 

 

 

Posted by AddChan
,