ionic3中cordova-plugin-geofence自定义通知铃声实现方案咨询
解决Ionic3中cordova-plugin-geofence自定义通知声音的问题
这个场景我之前处理过,确实cordova-plugin-geofence的官方文档没提自定义声音的配置,但有几种可行的办法能实现你的需求,下面给你详细拆解:
方案一:改用cordova-plugin-local-notifications触发通知(最省心)
既然你已经知道LocalNotification支持自定义声音,那可以完全绕过geofence插件自带的通知,自己在围栏触发时调用LocalNotification发通知,这样不管前台后台都能正常播放声音。
操作步骤:
- 先安装兼容Ionic3的LocalNotification插件:
ionic cordova plugin add cordova-plugin-local-notifications@0.9.0-beta.2 npm install @ionic-native/local-notifications@4.x
(Ionic3对应@ionic-native的4.x版本,别装错了)
- 初始化geofence时关闭插件自带通知,改用LocalNotification:
import { Geofence } from '@ionic-native/geofence'; import { LocalNotifications } from '@ionic-native/local-notifications'; import { Platform } from 'ionic-angular'; constructor( private geofence: Geofence, private localNotifications: LocalNotifications, private platform: Platform ) {} initGeofence() { this.geofence.initialize().then(() => { // 添加围栏时,把notifyOnEntry/notifyOnExit设为false,禁止插件自动发通知 this.geofence.addOrUpdate({ id: 'home_fence', latitude: 你的目标纬度, longitude: 你的目标经度, radius: 100, transitionType: 3, // 1=进入,2=离开,3=两者都触发 notifyOnEntry: false, notifyOnExit: false, // 这里的notification配置可以留空,因为我们不用它 notification: { title: '', text: '' } }); }); // 监听围栏触发事件 this.geofence.onTransitionReceived().subscribe(geofences => { geofences.forEach(fence => { const transitionText = fence.transitionType === 1 ? '进入' : '离开'; // 用LocalNotification发带自定义声音的通知 this.localNotifications.schedule({ id: Number(fence.id), title: `地理围栏提醒`, text: `您已${transitionText}指定区域`, // 自定义声音路径,注意平台差异 sound: this.platform.is('android') ? 'res://raw/custom_sound' // Android音频放res/raw目录,不用带后缀 : 'file://assets/sounds/custom_sound.wav' // iOS音频放www/assets/sounds目录 }); }); }); }
- 音频文件放置要求:
- Android:把音频文件(比如
custom_sound.mp3)放到platforms/android/app/src/main/res/raw目录(没有raw目录就新建),文件名要小写、无空格。 - iOS:推荐用wav格式的音频,放到
platforms/ios/你的项目名/Resources目录,或者www/assets/sounds下,确保打包时能被正确包含。
方案二:修改cordova-plugin-geofence的原生代码(适合不想换插件的情况)
如果不想引入新插件,可以直接修改geofence插件的原生代码,让它支持自定义声音参数:
Android端修改:
找到platforms/android/app/src/main/java/com/cowbell/cordova/geofence/GeofenceTransitionsIntentService.java文件,找到buildNotification方法,添加声音配置:
// 原代码中构建NotificationCompat.Builder的部分 NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId) .setContentTitle(notification.title) .setContentText(notification.text) .setSmallIcon(notification.icon) .setAutoCancel(true); // 新增:读取自定义sound参数 String soundName = notification.extra.getString("sound"); if (soundName != null && !soundName.isEmpty()) { Uri soundUri = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + soundName); builder.setSound(soundUri); }
iOS端修改:
找到platforms/ios/你的项目名/Plugins/cordova-plugin-geofence/GeofencePlugin.m文件,找到createNotificationContent方法,添加声音配置:
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = notification.title; content.body = notification.text; // 新增:读取自定义sound参数 NSString *soundName = notification.extra[@"sound"]; if (soundName && ![soundName isEqualToString:@""]) { content.sound = [UNNotificationSound soundNamed:soundName]; }
JS端调用:
添加围栏时,在notification的extra里传入sound参数:
this.geofence.addOrUpdate({ id: 'home_fence', latitude: 你的目标纬度, longitude: 你的目标经度, radius: 100, transitionType: 3, notification: { title: '地理围栏提醒', text: '您已进入指定区域', extra: { sound: 'custom_sound' // Android对应raw目录下的文件名,iOS要带后缀比如custom_sound.wav } } });
关键注意事项
- 后台权限:确保你的应用已经申请了必要的后台权限,Android需要开启“后台弹出界面”“允许后台运行”权限,iOS需要在
Info.plist中添加UIBackgroundModes包含location和remote-notification。 - 音频格式:Android支持mp3、wav,iOS推荐wav/caf格式,且文件名不能有特殊字符或空格。
内容的提问来源于stack exchange,提问作者abhi12335
相关产品推荐
相关产品推荐

