FCM自定义通知声音

时间:2017-12-13 07:01:37

标签: android firebase notifications firebase-cloud-messaging

我的andriod应用程序有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());
}

2 个答案:

答案 0 :(得分:2)

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))
        ;

答案 1 :(得分:1)

使用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"
    }
 }
  

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