환경설정에 이어서 코드작성을 해보겠습니다.
환경설정은 아래서 확인하세요.
----
----
- 푸쉬 설정
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번까지 입력하시고 우측 아래 컴토를 누릅니다.
알림소리가 울리며 푸쉬가 왔을것입니다.
'Programming > 안드로이드' 카테고리의 다른 글
안드로이드 스튜디오 Firebase - Login(auth) / Push [구글로그인코드적용] (2019.06.03) (0) | 2019.06.03 |
---|---|
안드로이드 스튜디오 Firebase - Login(auth) / Push [환경설정] (2019.05.28) (0) | 2019.05.31 |
빌드오류 (build error) : java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader 2019.04.29 (0) | 2019.04.29 |
Json Rest Api (0) | 2019.04.05 |
Java TCP 통신 : 서버 (채팅가능한 구조) (0) | 2018.11.09 |