uni-app iOS UniPush 发送本地推送只设置sound字段时抛空指针异常
uni-app iOS UniPush 发送本地推送只设置sound字段时抛空指针异常
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Mac | 11.4 | HBuilderX |
示例代码:
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; //标题
// content.title = @"本地消息";
// content.body = @"本地消息";
// content.userInfo = @{@"aps":@{@"content-available":@1}};
content.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.mp3",mp3Name]];
// repeats,是否重复,如果重复的话时间必须大于60s,要不会报错
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];
操作步骤:
- 必现
预期结果:
- 没有title和body应不处理
实际结果:
- 闪退
bug描述:
如图

更多关于uni-app iOS UniPush 发送本地推送只设置sound字段时抛空指针异常的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
调用 js 接口的代码发一下吧
更多关于uni-app iOS UniPush 发送本地推送只设置sound字段时抛空指针异常的实战教程也可以访问 https://www.itying.com/category-93-b0.html
没有使用js调用。我们在实现iOS推送扩展时发现创建本地推送只设置声音,就会抛出异常
根据你提供的代码和错误信息,这是一个典型的iOS本地通知配置问题。空指针异常通常是因为通知内容不完整导致的。
问题分析:
在iOS系统中,本地通知必须包含title、body或subtitle中的至少一个字段。你的代码只设置了sound字段,没有设置任何文本内容,这违反了iOS通知的基本要求。
解决方案:
在创建UNMutableNotificationContent时,必须至少设置以下其中一个属性:
title(标题)body(正文)subtitle(副标题)
修改你的代码,添加必要的文本内容:
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"通知标题"; // 必须设置
content.body = @"通知内容"; // 必须设置
content.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.mp3", mp3Name]];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.01 repeats:NO];

