Android Studio Custom Sound по уведомлению FCM по теме - PullRequest
0 голосов
/ 29 апреля 2018

У меня есть тема по FCM, и она работает нормально .. проблема в том, как мне установить пользовательский звук база уведомлений на тема уведомлений ? например

Topic A = hello.mp3
Topic B = merra.mp3


Пример кода

   private void sendNotification(String messageBody, Bitmap image, String link) {
        Intent intent = new Intent(this, FullscreenActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("LINK", link);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        if (FirebaseMessaging.getInstance().subscribeToTopic("topic2"))
            customSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

        else if (FirebaseMessaging.getInstance().subscribeToTopic("topic1")
        customSoundUri = Uri.parse("android.resource://" + getPackageName() + "/raw/sound.mp3");
        )
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setLargeIcon(image)/*Notification icon image*/
                .setSmallIcon(R.mipmap.ic_launcher_dishroomlogo)
                .setContentTitle(messageBody)
                .setStyle(new NotificationCompat.BigPictureStyle()
                .bigPicture(image))/*Notification with Image*/
                .setAutoCancel(true)
                .setSound(customSoundUri)
                .setContentIntent(pendingIntent);
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

1 Ответ

0 голосов
/ 29 апреля 2018

Вы можете использовать собственный звук из ресурсов для каждого уведомления, но я привожу пример со стандартным RingtoneManager

Отправьте название темы в виде поля в теле уведомления как данные, затем

  data  = remoteMessage.getData();
  topic = data.getString(“topicName”);




Uri customSoundUri;

  if(topic.equalIgnoreCase("topic1"))
     customSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

  else if(topic.equalIgnoreCase("topic2")
     customSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(new MyUtils().getNotificationIcon())
            .setContentTitle(messageBody)
            .setColor(ContextCompat.getColor(context, R.color.orange))
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setPriority(Notification.PRIORITY_MAX)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...