티스토리 뷰
[안드로이드] FCM 푸시 알림
 {
if (remoteMessage.getData().size() > 0) {
showNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("message"));
}
if (remoteMessage.getNotification() != null) {
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}
private RemoteViews getCustomDesign(String title, String message) {
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.noti);
remoteViews.setTextViewText(R.id.noti_title, title);
remoteViews.setTextViewText(R.id.noti_message, message);
remoteViews.setImageViewResource(R.id.noti_icon, R.mipmap.ic_launcher);
return remoteViews;
}
private void showNotification(String title, String message) {
Intent intent = new Intent(this, MainActivity.class);
String channel_id = "channel";
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channel_id)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(uri)
.setColor(Color.parseColor("#ff0000"))
.setAutoCancel(true)
.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000})
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
builder = builder.setContent(getCustomDesign(title, message));
}
else
{
builder = builder.setContentTitle(title)
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(channel_id, "web_app", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(uri, null);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(0, builder.build());
}
}
<service android:name=".MyFirebaseInstance">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
xml 파일
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/noti_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
android:src="@mipmap/ic_launcher"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/noti_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#000"
android:textStyle="bold" />
<TextView
android:id="@+id/noti_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
'ANDROID' 카테고리의 다른 글
JSON arr는 들어오는데 Adapter가 안먹을때 ( JSONArray는 length!) (0) | 2021.01.12 |
---|---|
fcm 서블릿(JSP) 참고 (0) | 2021.01.11 |
fragment 참고 (0) | 2021.01.09 |
참고 (JSP 이미지 파일 업로드) (0) | 2021.01.08 |
참고(프래그먼트) (0) | 2021.01.08 |
댓글