Push 알림 등록방법
2020.08.31 23:50
안드로이드에서 푸시 알림을 사용을 하기위해서는 FireBase에 가입해야 한다.
1. 가입후 새로운 프로젝을 생성하면 아래 그림처럼 google.service.json 파일을 다운로드 하여 안내에 따라 해당 경로(app폴더 아래)에 복사한다.
2. 안드로이드 스튜디오 메뉴에서 Tools > Firebase 를 클릭하여 푸시 알림에 필요한 기본 설정을 완료한다.
예전에는 gradle 파일을 수정해주어야 했지만, 지금은 간단히 클릭만으로 모든 설정을 완료할 수 있다.
참고로 스튜디오 오른쪽 패널의 Gradle Scripts 항목의 build gradle(Module: app) 을 클릭하여 dependencies 에 최신 버전의 소스 코드를 등록/수정한다.
dependencies {
...
implementation 'com.google.firebase:firebase-messaging:20.2.4'
}
또한 build gradle(Project) 를 클릭해서 denpondencies 에 최신 버전의 소스 코드를 등록/수정한다.
dependencies {
...
classpath 'com.google.gms:google-services:4.3.3'
}
3. AndroidManifest.xml 창을 열어서 아래 내용을 추가한다.
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.phteahang.m">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".IntroActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
</activity>
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
4. 위에서 등록한 서비스 이름의 MyFirebaseMessagingService.java 클래스 파일을 생성한다.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("FCM Log", "Refreshed token: "+s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
Log.d("FCM Log", "알림 메시지: " + remoteMessage.getNotification().getBody());
String messageBody = remoteMessage.getNotification().getBody();
String messageTitle = remoteMessage.getNotification().getTitle();
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 = "Channel ID";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageTitle)
.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 = "Channel Name";
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
}
Comment 0
No. | Subject | Author | Date | Views |
---|---|---|---|---|
» | Push 알림 등록방법 | Admin | 2020.08.31 | 14 |
7 | Intro View | Admin | 2020.08.30 | 11 |
6 | 안드로이드 Fragment 에서 WebView 사용하기 | Admin | 2020.08.28 | 9 |
5 | 파일들의 역활과 관계 [1] | Study4U | 2012.03.25 | 1899 |
4 | View 클래스 | Study4U | 2012.03.25 | 1545 |
3 | 프로젝트 구성과 Avtivity Lifecycle | Study4U | 2012.03.10 | 1351 |
2 | 안녕! 안드로이드 | Study4U | 2012.03.08 | 1501 |
1 | 안드로이드 개발 환경 구축 | Study4U | 2012.03.07 | 1609 |