FCM 푸시 알림
manifest 에 서비스 등록
<service android:name=”.MyFireBaseMessagingService”>
<intent-filter>
<action android:name=”com.google.firebase.MESSAGING_EVENT” />
</intent-filter>
</service>
인터넷 권한 허용
<uses–permission android:name=“android.permission.INTERNET”></uses–permission>
MyFireBaseMessagingService.java
public class MyFireBaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String token) {
Log.d("FCM Log", "Refreshed token: " + token);
}
@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());
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (!task.isSuccessful()) {
Log.w("FCM Log", "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();
}
});
}
}
Firebase
https://console.firebase.google.com/
안드로이드 앱 FCM 푸시 알림 예제 (+ 서버에서 푸시 알림 보내기)
http://blog.naver.com/PostView.nhn?blogId=ndb796&logNo=221553341369
안드로이드 FCM 푸시 알림 구현
https://triest.tistory.com/40
안드로이드 앱 만들기 #19 (FCM 푸시알림) – 홍드로이드
https://www.youtube.com/watch?v=pPMEgVarhWU
홍드로이드 github
https://github.com/hongdroid94/19_FCM