我在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());
确实会触发该通知,但未播放声音文件。 有人可以帮忙解决这个问题吗?
我还尝试按照建议here使用另一种获取mp3文件的方法:
const uri = android.net.Uri.parse("android.resource://" + context.getPackageName() + "/raw/a.mp3");
但这也无济于事。
我是否将“ a.mp3”声音文件放入了Android可以识别的正确文件夹中?
谢谢
答案 0 :(得分:0)
我找到了解决方案。我正在写答案,以便其他人可以从这个问题中学习。
关于我问的问题-我是否将“ 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的问答)。