[안드로이드] Android 8.1 SDK 27 FCM PUSH 알림 문제
안드로이드 8.1 오레오 이상 버전에서 푸쉬는 오는데 알림 소리가 안울리는 문제가 있네요~
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FCMMessageService extends FirebaseMessagingService {
private static final String TAG = "FCMMessageService";
static String registration_id = null;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
sendNotification(remoteMessage.getData().get("message"));
}
}
private void sendNotification(String messageBody) {
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);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = null;
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "default_channel_id";
String channelDescription = "Default Channel";
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
if (notificationChannel == null) {
int importance = NotificationManager.IMPORTANCE_HIGH;
notificationChannel = new NotificationChannel(channelId, channelDescription, importance);
notificationChannel.setLightColor(Color.GREEN);
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationBuilder = new NotificationCompat.Builder(this, channelId);
} else {
notificationBuilder = new NotificationCompat.Builder(this);
}
notificationBuilder
.setSmallIcon(R.drawable.noti_icon)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
wakelock.acquire(5000);
notificationManager.notify(0, notificationBuilder.build());
}
}
SDK 버전이 올라갈때 마다 가지가지 하네요~ ㅎ
'안드로이드' 카테고리의 다른 글
[안드로이드] AsyncTask 파일 다운로드 예제 (0) | 2019.04.24 |
---|---|
[안드로이드] Android Studio resource linking failed (0) | 2019.04.10 |
[안드로이드] FCM PHP 서버 PUSH (0) | 2019.03.08 |
[안드로이드] API 23 앱 권한 설정 (0) | 2019.03.04 |
[안드로이드] FileUriExposedException 에러 (0) | 2019.03.04 |