Nice programing

Android의 Firebase에서 알림을 보낼 때 알림 소리가 나지 않습니다.

nicepro 2020. 12. 3. 19:41
반응형

Android의 Firebase에서 알림을 보낼 때 알림 소리가 나지 않습니다.


firebase에서 Android 애플리케이션으로 푸시 알림을 보내고 있습니다. 하지만 내 앱이 백그라운드에있을 때 firebase onMessageReceived 메서드가 호출되지 않고 대신 firebase가 시스템 트레이에 알림을 표시하기 위해 시스템에 알림을 보냅니다. 알림이 시스템 트레이에 나타나지만 시스템 설정에서 내 앱에 대한 알림 소리를 허용 한 경우에도 알림 소리가 나지 않습니다.

firebase에서 알림을받을 때 알림 음을 재생하려면 어떻게해야하나요?

이것이 firebase 에서 내 앱 Blogpost 링크 로 알림을 보내는 방법 입니다.

Android 애플리케이션에 Firebase를 추가하는 방법


알림의 알림 페이로드에는 사운드 키가 있습니다.

공식 문서에서 사용은 다음과 같습니다.

장치가 알림을받을 때 재생할 소리를 나타냅니다. 앱에 번들 된 사운드 리소스의 기본값 또는 파일 이름을 지원합니다. 사운드 파일은 / res / raw /에 있어야합니다.

예 :

{
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",

    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon",
      "sound" : "mySound"
    }
  }

장치의 기본 사운드를 사용하려면 다음을 사용해야합니다 "sound": "default"..

페이로드에서 가능한 모든 키는 다음 링크를 참조하십시오. https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

firebase를 모르는 사람들을 위해 앱이 백그라운드에있을 때 알림을 다르게 처리합니다. 이 경우 onMessageReceived 함수가 호출되지 않습니다.

앱이 백그라운드에있을 때 Android는 알림 메시지를 시스템 트레이로 보냅니다. 사용자가 알림을 탭하면 기본적으로 앱 런처가 열립니다. 여기에는 알림 및 데이터 페이로드가 모두 포함 된 메시지가 포함됩니다. 이러한 경우 알림은 기기의 시스템 트레이에 전달되고 데이터 페이로드는 런처 활동의 의도에 따라 추가로 전달됩니다.


adavaced 옵션선택 고급 옵션을메시지를 작성 및 사운드를 선택 활성화 활성화 선택

이것은 내 솔루션입니다


이 시도

{
    "to" : "DEVICE-TOKEN",

    "notification" : {
      "body"  : "NOTIFICATION BODY",
      "title" : "NOTIFICATION TITILE",
      "sound" : "default"
    }
  }

사용자 지정 알림 소리에 대한 @note :-> "sound" : "MyCustomeSound.wav"


onMessageReceived메서드는 앱이 포 그라운드에 있거나 알림 페이로드에 데이터 유형 만 포함 된 경우에만 실행됩니다.

로부터 중포 기지의 문서

다운 스트림 메시징의 경우 FCM은 알림데이터 라는 두 가지 유형의 페이로드를 제공 합니다 .

대한 통지 유형, FCM은 자동으로 클라이언트 응용 프로그램을 대신하여 최종 사용자 장치에 메시지를 표시합니다. 알림에는 사용자가 볼 수있는 미리 정의 된 키 세트가 있습니다.
를 들어 , 데이터 유형, 클라이언트 응용 프로그램은 데이터를 처리하는 메시지에 대한 책임이 있습니다. 데이터 메시지에는 맞춤 키-값 쌍만 있습니다.

Use notifications when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app, or if you want to send messages to iOS devices when there is a direct FCM connection.

Further down the docs

App behaviour when receiving messages that include both notification and data payloads depends on whether the app is in the background or the foreground—essentially, whether or not it is active at the time of receipt.
When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.
When in the foreground, your app receives a message object with both payloads available.

If you are using the firebase console to send notifications, the payload will always contain the notification type. You have to use the Firebase API to send the notification with only the data type in the notification payload. That way your app is always notified when a new notification is received and the app can handle the notification payload.

If you want to play notification sound when app is in background using the conventional method, you need to add the sound parameter to the notification payload.


With HTTP v1 API it is different

Documentation

Example:

{
 "message":{
    "topic":"news",
    "notification":{
       "body":"Very good news",
       "title":"Good news"
    },
    "android":{
       "notification":{
          "body":"Very good news",
          "title":"Good news",
          "sound":"default"
       }
    }
  }
}

do like this

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    //codes..,.,,

    Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(sound);

}

try this....

  public  void buildPushNotification(Context ctx, String content, int icon, CharSequence text, boolean silent) {
    Intent intent = new Intent(ctx, Activity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 1410, intent, PendingIntent.FLAG_ONE_SHOT);

    Bitmap bm = BitmapFactory.decodeResource(ctx.getResources(), //large drawable);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
            .setSmallIcon(icon)
            .setLargeIcon(bm)
            .setContentTitle(content)
            .setContentText(text)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    if(!silent)
       notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

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

        notificationManager.notify(1410, notificationBuilder.build());
    }

and in onMessageReceived, call it

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    Log.d("Msg", "Message received [" + remoteMessage.getNotification().getBody() + "]");

    buildPushNotification(/*your param*/);
}

or follow KongJing, Is also correct as he says, but you can use a Firebase Console.


firebase 콘솔에서 보내도 알림 음을 재생할 수 있습니다. 이를 위해서는 사전 옵션에 "default"값이있는 "sound"키를 추가하기 만하면됩니다.

참고 URL : https://stackoverflow.com/questions/37959588/no-notification-sound-when-sending-notification-from-firebase-in-android

반응형