마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

fcm 푸시알림 오면 진동, 소리 나는 것 질문

0 추천

fcm 을 통해서

코드를 문서에 있는 것  거의 그대로 사용했습니다.

푸시 알림이 오는것 까진 잘 됩니다. 그런데, 푸시 알림이 와도 화면도 안켜지고, 아무런 소리, 진동도 울리지 않는 것이 문제네요..

소리, 진동, 화면켜짐 기능까지 넣으려면 추가적인 코드를 작성해야 하나요??

일단 setDefaults(NotificationCompat.DEFAULT_VIBRATE) 코드는 넣었고, 

퍼미션에서 vibrate 도 추가했습니다.

다음은 코드입니다..

 

 

 

 

    private void sendPushNotification(String message) {
        System.out.println("received message : " + message);
        // 클래스...
        Intent intent = new Intent(this, Activity_Login_Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0  //Request code
                , intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.backicon).setLargeIcon
                        (BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher) )
                .setContentTitle("Push Title ")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri).setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                .setLights(000000255,500,2000)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);

        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 // ID of notification
    , notificationBuilder.build());
    }
}

 

익명사용자 님이 2017년 1월 11일 질문

2개의 답변

0 추천
wakelock, vibrate 퍼미션 추가하셨나요.
개발자초심 (21,220 포인트) 님이 2017년 1월 11일 답변
<uses-permission android:name="android.permission.VIBRATE"/>

    <uses-permission android:name="android.permission.WAKE_LOCK"/>

이 두 퍼미션을 추가했는데도.. 푸시알림이 오면 그냥 화면도 안뜨고 진동도 없어요.
푸시 오는것은 문제가 없는데...
0 추천

.setDefaults(NotificationCompat.DEFAULT_VIBRATE 대신..

 

아래와 같이 vibrate를 명시해보세요.

.setVibrate(new long[]{100, 0, 100, 0})
개발자초심 (21,220 포인트) 님이 2017년 1월 11일 답변
ㅠㅠ안되네요.. 혹시 서버에서도 뭔가 해야 할 작업이 있나요??
public class FirebaseMessagingService  extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = "FirebaseMsgService";

    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

       // sendPushNotification(remoteMessage.getData().get("message"));

        Log.d("메시지", "From : "+remoteMessage.getFrom());

        if(remoteMessage.getData().size() > 0){
            Log.d("메시지", "message notification body : "+ remoteMessage.getNotification().getBody());
            sendPushNotification(remoteMessage.getNotification().getBody());

        }

    }

    private void sendPushNotification(String message) {
        System.out.println("received message : " + message);
        // 클래스...
        Intent intent = new Intent(this, Activity_Login_Main.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0  //Request code
                , intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.backicon).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher) )
                .setContentTitle("Push Title ")
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri).setVibrate(new long[]{100, 0, 100, 0})
                .setLights(000000255,500,2000)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        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 // ID of notification
    , notificationBuilder.build());
    }
}



전체클래스입니다.
글쎄요....
저도 서버 & 클라이언트 딱히 커스터마이징 한 것 없이 적용해놓은 상태라.
전 오히려 Vibrate 관련해서는 설정을 안해놓은 상태입니다.
매너모드다보니 자동으로 진동이 울려서요.



private void sendNotification(String title, String messageBody) {
        Intent intent = new Intent(this, Splash.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(title)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

//        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
        notificationManager.notify((int) SystemClock.currentThreadTimeMillis(), notificationBuilder.build());
    }
풀소스 부탁해도 될까요? 아무리 고쳐도 알림음과 진동이 안 오네요
이미 해결햇는지 모르겟지만 set default 가잇으면 소리가안날겁니다.
...