简体   繁体   English

奥利奥和馅饼通知

[英]Oreo & Pie Notifications

Problem: My app needs notifications that can be customized by the user.问题:我的应用程序需要可由用户自定义的通知。 eg.例如。 set different sounds, titles, texts for notifications为通知设置不同的声音、标题、文本

Issue: I'm aware notification channels are set only once and cannot be modified so I thought I could just use variables however even with variables once I have picked a sound it stays as that sound and cannot be changed at all.问题:我知道通知通道只设置一次并且无法修改,所以我认为我可以只使用变量,但是即使使用变量,一旦我选择了一个声音,它就会保持该声音并且根本无法更改。 The only solution I can think is a new channel each time something is changed which just seems silly.我能想到的唯一解决方案是每次更改某些内容时都会创建一个新频道,这似乎很愚蠢。 Surely there is another way??当然还有别的方法??

Also to add to all of this, on Android Pie the notification sound does not work at all I can't work out why.另外要补充的是,在 Android Pie 上,通知声音根本不起作用,我不知道为什么。

Uri soundUri = Uri.parse(notificationSound);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CH_ID")
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(customTextTitle)
        .setContentText(customTextBody)
        .setAutoCancel(true)
        .setSound(soundUri)
        .setContentIntent(pendingIntent);

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

    if(soundUri != null){
        // Changing Default mode of notification
        notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
        // Creating an Audio Attribute
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_ALARM)
                .build();

        // Creating Channel
        NotificationChannel notificationChannel = new NotificationChannel("CH_ID","Testing_Audio",NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setSound(soundUri,audioAttributes);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
}
mNotificationManager.notify(0, notificationBuilder.build());
}

I have solved the problem albeit in a hacky way :( I have ended up setting the sound to null if >=Oreo and using a media player to play the notification sound also setting the audioStream to STREAM_NOTIFICATION. Obviously not ideal but as it's a notification and not an alarm the audio shouldn't go over the time set to perform task during onReceive.我已经解决了这个问题,尽管以一种hacky的方式:(如果>=Oreo,我最终将声音设置为空,并使用媒体播放器播放通知声音并将audioStream设置为STREAM_NOTIFICATION。显然不理想,但因为它是一个通知而不是警报,音频不应超过在 onReceive 期间执行任务的时间设置。

The only problem I can see with this solution is if the user decides to mute the notification/make adjustments to the channel on their phone.我可以看到此解决方案的唯一问题是用户是否决定将通知静音/对手机上的频道进行调整。 Muting the sound there obviously will have no effect to the sound played by media player and will most likely show as no sound anyway which is unfortunate.将那里的声音静音显然不会影响媒体播放器播放的声音,并且很可能无论如何都会显示为没有声音,这是不幸的。

 try {
                    mp.setDataSource(context.getApplicationContext(), soundUri);
                    mp.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
                    mp.prepare();
                    mp.start();
                } catch (Exception e) {
                    //exception caught in the end zone
                }

For anyone else having this issue I found a really great post here which is the same solution as mine but more robust and in more depth.对于遇到此问题的其他人,我在这里找到了一篇非常棒的帖子,它与我的解决方案相同,但更强大且更深入。 Android O - Notification Channels - Change Vibration Pattern or Sound Type Android O - 通知频道 - 更改振动模式或声音类型

你有错误的实现,设置声音如下

notificationBuilder.setSound(soundUri,audioAttributes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM