【问题标题】:FCM Custom notification soundFCM 自定义通知声音
【发布时间】:2017-12-13 07:01:36
【问题描述】:

我的 Android 应用程序有 Firebase 消息传递。我正在使用 Firebase 发送推送通知。我想将默认通知声音更改为自定义声音。我该怎么做 ?

    Uri defaultSoundUri = 
    RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setLargeIcon(image)/*Notification icon image*/
            .setSmallIcon(R.mipmap.ic_notif)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setCustomBigContentView(remoteViews)
            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))
            ;


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

    notificationManager.notify(Integer.parseInt(id) /* ID of notification */, notificationBuilder.build());
}

【问题讨论】:

  • 你可以找到this的文章非常有用。它解释了如何处理自定义声音。

标签: android firebase notifications firebase-cloud-messaging


【解决方案1】:
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;

使用上面的代码从资源中添加自定义声音。

如果我们使用 Notification 类,可以使用上面的代码。

Notification notification = new Notification(icon, tickerText, when);

当您使用 NotificationBuilder 时,请使用以下代码。

Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setLargeIcon(image)/*Notification icon image*/
        .setSmallIcon(R.mipmap.ic_notif)
        .setContentTitle(title)
        .setAutoCancel(true)
        .setSound(sound)
        .setContentIntent(pendingIntent)
        .setCustomBigContentView(remoteViews)
        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))
        ;

【讨论】:

  • 我应该在哪里更换它?我们可以用我们的或修改上面的代码吗?
  • 它 [有效] 非常感谢兄弟 :) 它帮了我很多 :)
  • 很高兴为您提供帮助。请接受答案,因为它正在工作。 :)
【解决方案2】:

使用setSound()方法设置声音

if(!silent) {   // check if phone is not in silent mode
       notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
       NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

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

或者你可以使用

{
    "to" : "XXYYXXYY...",

    "notification" : {
         "body" : "The stock opened on a bullish note at Rs. 449 and touched a high of Rs. 461.35, up 5.06 per cent over its previous closing price on the BSE. A similar movement was seen on the NSE where the stock opened at Rs. 450 and hit a high of Rs. 463.70, up 5.32 per cent.",
         "title" : "Stocks in focus: Kalpataru Power, Punj Lloyd, J B Chem, Bharti Airtel",
         "icon" : "ic_stock",
         "sound" : "res_notif_sound"
    }
 }

如果你想使用设备的默认声音,你应该使用: “声音”:“默认”。

【讨论】:

  • @JackN,无法理解您的评论
  • res_notif_sound 是系统文件吗?如果我们想引用 res 文件夹中的文件。该怎么做?
  • 谢谢,对我帮助很大。
  • @AnuChaudhary 乐于助人 ;)
猜你喜欢
  • 2018-11-08
  • 2020-04-10
  • 1970-01-01
  • 2021-11-15
  • 2020-01-09
  • 2022-07-12
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多