* Gradle - Project
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.google.gms:google-services:4.2.0'
}
}
allprojects {
repositories {
google()
}
}
* Gradle - Module
dependencies {
implementation 'com.google.firebase:firebase-core:17.0.1'
implementation 'com.google.firebase:firebase-messaging:20.0.0'
}
apply plugin: 'com.google.gms.google-services'
* AndroidManifest.xml
<application
<service android:name=".service.FirebaseMessaging">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
* FirebaseMessaging.java
public class FirebaseMessaging extends FirebaseMessagingService {
@Override
public void onNewToken(String token) {
Log.d("@@", "Refreshed token: " + token);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
Log.d("@@", "알림 메시지: " + 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());
}
}
}
* MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
MapFragment mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w("@@", "getInstanceId failed", task.getException());
return;
}
// String token = task.getResult().getToken();
// Log.d("FCM Log", "FCM 토큰: " + token);
// Toast.makeText(MainActivity.this, token, Toast.LENGTH_SHORT).show();
}
});
}
}
* 메시지 보내기
1. Firebase Console(https://console.firebase.google.com)
2. 프로젝트 선택
3. 좌측 메뉴에서 Cloud Messaging
4. 우측의 Send your first message
5. 타이틀, 제목 입력 후 전송
'android' 카테고리의 다른 글
GPS 현재 위치, 위치 업데이트(latitude, longitude, altitude) (0) | 2020.03.07 |
---|---|
구글맵 화면에 보이는 범위 위도 경도 (0) | 2020.03.05 |
Geocoder 주소 > 위도, 경도 변환 (0) | 2020.03.02 |
가로세로 전환시 새로고침 안되게 유지 (0) | 2020.02.21 |
YouTube API 플레이어 (0) | 2020.02.19 |