【问题标题】:How to make a list of notification's sound in android?如何在android中制作通知列表?
【发布时间】:2020-04-12 08:14:13
【问题描述】:

我希望用户通过选择通知的声音来自定义他们的应用,但我不知道如何制作声音列表以及如何将它们设置为通知的声音。我已经搜索并尝试了各种方法,但都没有成功,请帮助我!

这是我显示通知的代码:

NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelID)
                .setSmallIcon(R.drawable.logo) /// Set notification's icon
                .setSound(uri) /// Set sound
                .setAutoCancel(true) /// Allow sound to auto cancel
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) /// Set sound vibration
                .setOnlyAlertOnce(true) /// Only alert Once
                .setContentIntent(pendingIntent) /// Set intent it will show when click notification
                .setContent(getCustomDesign(title, message)); /// Set the design of notification

        /// Manage notification
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        /// Check if android version is Oreo or upper to show notification via NotificationChannel
        /// For each channel, you can set the and auditory behavior that is applied to all notifications in that channel
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            /// Set channel's ID, name, and importance
            NotificationChannel notificationChannel = new NotificationChannel(channelID, "DEMO", notificationManager.IMPORTANCE_HIGH);

            /// Set sound for channel
            notificationChannel.setSound(uri, null);

            /// Create channel
            notificationManager.createNotificationChannel(notificationChannel);
        }

        /// Show the notification
        notificationManager.notify(0, builder.build());

我尝试制作铃声选择器的意图:

btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select notification tone");
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
                startActivityForResult(intent, 5);
            }
        });

和 onActivityResult:

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (resultCode == NotificationActivity.RESULT_OK && requestCode == 5) {
            Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

            if (uri != null) {
                //NotificationActivity.this.chosenRingtone = uri.toString();
                RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION, uri);
            } else {
                //this.chosenRingtone = null;
            }
        }
    }

但是Logcat告诉我的项目没有被授予这个权限:android.permission.WRITE_SETTINGS。

【问题讨论】:

  • 好的,我已经编辑好了

标签: android firebase notifications


【解决方案1】:

试试这个来填充通知声音选择器:-

    private class ToneItem {

    private String title = null;
    private String uri = null;

    private ToneItem(String title, String uri) {
        this.title = title;
        this.uri = uri;
    }

    public String getTitle() {
        return title;
    }

    public String getUri() {
        return uri;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

}

     private ArrayList<ToneItem> listRingTones() {
    RingtoneManager manager = new RingtoneManager(this);
    manager.setType(RingtoneManager.TYPE_NOTIFICATION);

    ArrayList<ToneItem> toneItems = new ArrayList<>();
    toneItems.add(new ToneItem("None", "/"));
    Cursor cursor = manager.getCursor();
    if (cursor != null) {
        while (cursor.moveToNext()) {
            String id = cursor.getString(RingtoneManager.ID_COLUMN_INDEX);
            String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
            String uri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);

            ToneItem toneItem = new ToneItem(title, uri + "/" + id);
            toneItems.add(toneItem);
        }
    } else {
        //nothing
    }

    return toneItems;
}
private void showNotificationToneDialog( String title) {
    final ArrayList<ToneItem> toneItems = listRingTones();

    String[] options = new String[toneItems.size()];
    for (int index = 0; index < toneItems.size(); index++) {
        options[index] = toneItems.get(index).title;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);
    builder.setTitle(title)

            .setItems(options, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    ToneItem selectedTone = toneItems.get(which);
        //HERE SAVE THE SELECTED TONE URI(selectedTone.uri) IN SHARED PREFERENCES
                }
            });

    builder.create().show();
}

要设置通知声音,请从首选项中获取保存的 URI 并将其设置为通知生成器

     Uri uri = Uri.parse(//Get the saved uri from prefernce);
     mBuilder.setSound(uri);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2021-04-24
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多