26 03 2018
Android push topics
- 안드로이드 Firebase 안내
https://firebase.google.com/docs/cloud-messaging/android/client
- 1)Firebase에 앱추가
1)프로젝트를 생성후 앱 추가를 누른다.
앱프로젝트의 >app> build.gradle에서
1 2 3 4 5 6 7 8 |
defaultConfig { applicationId "kr.or.xxxx.xxxx" minSdkVersion 16 targetSdkVersion 25 versionCode 112 versionName "1.1.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } |
2)패키지명을 kr.or.xxxx.xxxx 로 안드로이드 패키지 이름으로 지정후
google-sevices.json을 다운받아 앱프로젝트> app 팀에 복사한다.
3)완료
위에서와 같이
프로젝트>build.gradle에
1 2 3 4 5 6 7 8 9 10 11 12 13 |
buildscript { repositories { jcenter() google() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.google.gms:google-services:3.1.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } |
그리고 프로젝트>app>build.gradle
1 |
apply plugin: 'com.google.gms.google-services' |
추가한다.
그리고 dependencies부분에
1 |
compile 'com.google.firebase:firebase-messaging:11.8.0' |
넣어준다.
- 2) 앱에 Firebase 추가
Android Studio 에서 Firebase Assistant를 여는 방법
- Tools> Firebase 를 클릭 Assistant 창이 열립니다.
- 나열된 기능중 Cloud Messaging를 선택하고
- Set up Firebase Cloud Messaging
- (1) Connect to Firebase클릭후 (2)Add FCM to your app
- 프로그램 수정
1)AndroidManifest.xml 에 </appliction>위에 추가
1 2 3 4 5 |
<service android:name=".GoogleFirebaseMessagingService"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> |
2)GoogleFireMessagingService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.util.Log; import android.support.v4.app.NotificationCompat; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; public class GoogleFirebaseMessagingService extends FirebaseMessagingService { private static final String TAG = "GoogleFirebaseMessagingService"; private static final int REQUEST_CODE = 0; private static final int NOTIFICATION_ID = 0; public GoogleFirebaseMessagingService() { super(); } @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); // TODO(developer): Handle FCM messages here. Log.d(TAG, "From: " + remoteMessage.getFrom()); if (remoteMessage.getNotification() !=null) { String title = remoteMessage.getNotification().getTitle(); String message = remoteMessage.getNotification().getBody(); Log.d(TAG, "Message Notification title: " + title); Log.d(TAG, "Message Notification Message:" + message); sendNotification(title, message); } } /** * Create and show a simple notification containing the received FCM message. * * @param message FCM message body received. */ private void sendNotification(String title, String message) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build()); } } |
3)MainActivity나 기타 Activity의 onCreate함수에
1 |
FirebaseMessaging.getInstance().subscribeToTopic("탑픽주제"); |
하시면 됩니다.
iOS 에서 Swift에 FCM을 이용한 Push Topics 전송 Troubleshooting: Failed to find byte code for okhttp3/Callback