【问题标题】:NativeScript android play sound on notificationNativeScript android在通知上播放声音
【发布时间】:2019-07-17 10:03:18
【问题描述】:

我正在一个 Nativescript Angular 应用程序中编写原生 Android 代码,以显示带有声音的通知。

我遵循了this answer 中的建议。

我在以下文件夹中有一个名为 a.mp3 的声音文件:

这里是配置通知声音的代码:

const uri = new android.net.Uri.Builder()
           .scheme(android.content.ContentResolver.SCHEME_ANDROID_RESOURCE)
            .authority(application.android.nativeApp.getPackageName())
            .appendPath("raw")
            .appendPath("a.mp3")
            .build();

        const AudioAttributes = android.media.AudioAttributes;
        const audioAttributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
            .build();

这是显示通知的代码:

const NOTIFICATION_ID = 234;
    const CHANNEL_ID = "my_channel_01";
    const name = "my notifications";
    const Description = "some desc";
    const title = "notif title";
    const message = "This notification has been triggered by me";

    const NotificationManager = android.app.NotificationManager;


    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        const importance = android.app.NotificationManager.IMPORTANCE_HIGH;

        const mChannel = new android.app.NotificationChannel(CHANNEL_ID, name, importance);
        mChannel.setSound(uri, audioAttributes);
        mChannel.setDescription(Description);
        mChannel.enableLights(true);
        mChannel.setLightColor(android.graphics.Color.RED);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern([100, 200, 300, 400, 500, 400, 300, 200, 400]);
        mChannel.setShowBadge(false);

        notificationManager.createNotificationChannel(mChannel);
    }


    ///////// Create an activity on tap (intent)
    const Intent = android.content.Intent;
    const PendingIntent = android.app.PendingIntent;
    const intent = new Intent(context, com.tns.NativeScriptActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);

    const pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    ///////// PRESERVE NAVIGATION ACTIVITY. To start a "regular activity" from your notification, set up
    ///////// the PendingIntent using TaskStackBuilder so that it creates a new back stack as follows.
    ///////// SEE: https://developer.android.com/training/notify-user/navigation.html
    const TaskStackBuilder = android.support.v4.app.TaskStackBuilder;

    const resultIntent = new Intent(context, com.tns.NativeScriptActivity.class);
    const stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(com.tns.NativeScriptActivity.class);
    stackBuilder.addNextIntent(resultIntent);


    ///////// Creating a notification
    var NotificationCompat = android.support.v4.app.NotificationCompat;
    const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSound(uri)
        .setSmallIcon(android.R.drawable.btn_star_big_on)
        .setContentTitle(title)
        .setContentText(message)
        .setStyle(
            new NotificationCompat.BigTextStyle()
            .bigText("By default, the notification's text content is truncated to fit one line. If you want your notification to be longer, you can enable an expandable notification by adding a style template with setStyle(). For example, the following code creates a larger text area:")
            )
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        // Set the intent that will fire when the user taps the notification
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);

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

确实会触发通知,但没有播放声音文件。 请问谁能帮忙解决一下?

我也尝试使用另一种获取 mp3 文件的方法,推荐 here:

const uri = android.net.Uri.parse("android.resource://" + context.getPackageName() + "/raw/a.mp3");

但这也无济于事。

我是否将“a.mp3”声音文件放在了 android 可识别的正确文件夹中?

谢谢

【问题讨论】:

  • 我也尝试使用默认铃声,但没有成功: var RingtoneManager = android.media.RingtoneManager; var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  • 您的测试设备上的 Android 操作系统版本是多少?你尝试过不同的设备吗?此外,您正在频道和通知上设置声音,您是否尝试删除一个声音,可能是频道上的声音,看看是否有任何区别。
  • 谢谢 Manoj,我设法解决了这个问题。我会尽快发布这个问题的答案。

标签: nativescript


【解决方案1】:

我找到了解决方案。我正在写答案,以便其他人可以从这个问题中学习。

关于我提出的问题 - 我是否将“a.mp3”声音文件放在了 android 识别的正确文件夹中?

答案是肯定的。 /App_Resources/Android/src/main/res/raw/ 是上述代码查找声音文件的位置。

但是,我需要做 2 处修改:

A_代码需要修改为不包含文件扩展名:

const uri = new android.net.Uri.Builder()
  .scheme(android.content.ContentResolver.SCHEME_ANDROID_RESOURCE)
  .authority(application.android.nativeApp.getPackageName())
  .appendPath("raw")
  .appendPath("a") // Referring to a.mp3
  .build();

然后声音 uri 将像问题中的代码一样使用:

const mChannel = new android.app.NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setSound(uri, audioAttributes);

const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSound(uri)

B_ 我需要告诉 webpack 从 /App_Resources/Android/src/main/res/raw/ 打包 .mp3 文件并将其放入 nativescript 构建中。为此,我必须将 { from: { glob: "**/*.mp3" } }, 添加到 webpack.config.js:

// Copy assets to out dir. Add your own globs as needed.
            new CopyWebpackPlugin([
                { from: { glob: "fonts/**" } },
                { from: { glob: "**/*.png" } },
                { from: { glob: "**/*.mp3" } },
            ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),

由于 webpack 未配置为自动从 App_Resources 复制所有文件。对于您在项目中使用的任何其他文件扩展名(例如:.jpg 文件)也是如此。

更多信息here(关于“nativescript-audio”插件的 git 的问答)。

【讨论】:

    猜你喜欢
    • 2012-01-14
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多