android - 通知不播放自定义声音

时间:2017-11-14 04:56:14

标签: android notifications android-notifications

我尝试使用notification custom sound.firebase console显示throw an notification

这是我的代码:

    Notification notification = builder.setSmallIcon(icon).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(pendingIntent)
            .setSound(notificationSound)
            .setStyle(inboxStyle)
            .setWhen(getTimeInMilliSec(timeStamp))
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), icon))
            .setContentText(message).build();

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID_SMALL_IMAGE, notification);


Here notificationSound = Uri.parse(SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/notification");

I placed my notification.mp3 file here: res/raw/notification.

我按预期获得了notification,但我没有得到任何notification sound.

以下是我logs时的notification

11-14 10:08:31.200 16509-16509/com.hago.ntr D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
11-14 10:08:31.220 16509-16509/com.hago.ntr D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
11-14 10:08:31.272 16509-16509/com.hago.ntr I/FA: App measurement is starting up, version: 9683
11-14 10:08:31.272 16509-16509/com.hago.ntr I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
11-14 10:08:31.272 16509-16509/com.hago.ntr D/FA: Debug-level message logging enabled
11-14 10:08:31.272 16509-16509/com.hago.ntr D/FA: AppMeasurement singleton hash: 266608187
11-14 10:08:31.299 16509-16509/com.hago.ntr V/FA: Collection enabled
11-14 10:08:31.299 16509-16509/com.hago.ntr V/FA: App package, google app id: com.hago.ntr, 1:317122300577:android:6c0b483c0a4e3f40
11-14 10:08:31.299 16509-16509/com.hago.ntr I/FA: To enable faster debug mode event logging run:
                                                adb shell setprop firebase.analytics.debug-mode com.hago.ntr
11-14 10:08:31.343 16509-16509/com.hago.ntr V/FA: Registered activity lifecycle callback
11-14 10:08:31.345 16509-16509/com.hago.ntr I/FirebaseInitProvider: FirebaseApp initialization successful
11-14 10:08:31.347 16509-16509/com.hago.ntr I/InstantRun: starting instant run server: is main process
11-14 10:08:31.354 16509-16552/com.hago.ntr V/FA: Using measurement service
11-14 10:08:31.368 16509-16552/com.hago.ntr V/FA: Connecting to remote service
11-14 10:08:31.459 16509-16552/com.hago.ntr I/FA: Tag Manager is not found and thus will not be used
11-14 10:08:31.465 16509-16552/com.hago.ntr D/FA: Logging event (FE): _nr, Bundle[{_o=fcm, _ndt=0, _nmt=1510634308, _nmid=7758633434821297510}]
11-14 10:08:31.468 16509-16552/com.hago.ntr V/FA: Using measurement service
11-14 10:08:31.468 16509-16552/com.hago.ntr V/FA: Connection attempt already in progress
11-14 10:08:31.473 16509-16552/com.hago.ntr D/FA: Connected to remote service
11-14 10:08:31.473 16509-16552/com.hago.ntr V/FA: Processing queued up service tasks: 2
11-14 10:08:36.494 16509-16552/com.hago.ntr V/FA: Inactivity, disconnecting from the service

可能是什么问题?

1 个答案:

答案 0 :(得分:2)

尝试将此代码添加到Pending intent

下面
     try {
            Uri defaultSoundUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.fileName);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), defaultSoundUri);
            r.play();
        } catch (Exception e) {
            e.printStackTrace();
        }

有关详情,请点击以下链接

https://github.com/firebase/quickstart-android/tree/master/messaging

https://firebase.google.com/docs/cloud-messaging/android/client

此外,如果您想管理或自定义通知栏,请点击以下链接

https://developer.android.com/guide/topics/ui/notifiers/notifications.html