본문 바로가기

Programming/안드로이드

안드로이드 스튜디오 Firebase - Login(auth) / Push [푸쉬코드적용] (2019.06.03)

환경설정에 이어서 코드작성을 해보겠습니다.

환경설정은 아래서 확인하세요.

https://moblieandlife.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9C-%EC%8A%A4%ED%8A%9C%EB%94%94%EC%98%A4-Firebase-Loginauth-Push-%ED%99%98%EA%B2%BD%EC%84%A4%EC%A0%95-20190528

----

----

- 푸쉬 설정

1. 파이어베이스 서비스 클래스 생성

2. AndroidManifest.xml 에 서비스 등록

3. 테스트 메세지 보내기

 

1. firebaseMessage 를 받는 서비스 클래스 생성

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {
              @Override
              public void onMessageReceived(RemoteMessage remoteMessage) {

                            if (remoteMessage.getNotification() != null) {
                                          String _messageBody = remoteMessage.getNotification().getBody());

                                          Intent intent = new Intent(this, MainActivity.class);
                                          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                          PendingIntent pendingIntent = PendingIntent.getActivity

                                          (this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
                            }
              
                            String channelId = "c id";
                            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

                            NotificationCompat.Builder notificationBuilder =

                                          new NotificationCompat.Builder(this, channelId)
                                          .setSmallIcon(R.mipmap.ic_launcher)
                                          .setContentText(messageBody)
                                          .setAutoCancel(true)
                                          .setSound(defaultSoundUri)
                                          .setContentIntent(pendingIntent);

                            NotificationManager notificationManager =

                                          (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                                          String channelName = "test";
                                          NotificationChannel channel = new NotificationChannel

                                          (channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
                                          notificationManager.createNotificationChannel(channel);
                            }

                            notificationManager.notify(0, notificationBuilder.build());

              }
}

 

2. 서비스 등록

<manifest>
         <applcation>
                  <service android:name=".MyFirebaseMessagingService">
                           <intent-filter>
                                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                           </intent-filter>
                  </service>

         </application>
</manifest>

 

3. 테스트 메세지 보내기

-firebase로 이동합니다.

모든 Firebase 기능보기 를 누릅니다.

 

Cloud Messaging 을 누릅니다.

 

Send your first message 를 누릅니다.

 

3번까지 입력하시고 우측 아래 컴토를 누릅니다.

알림소리가 울리며 푸쉬가 왔을것입니다.