Local Notification的作用

Local Notification的作用 
Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判断需要发出通知的行为都可以或者说应该使用Local Notification来处理。比方说:iBeacon中进入了某个Beacon region,或者说自定义的一些定时提醒等。 

构建Local Notification 
在iOS中,构建LocalNotification非常简单,只需要掌握好NSLocalNotification这个类就够用了,基本步骤如下: 

1. 创建并初始化 UILocalNotification对象 
2. 配置该对象的属性: 

* 触发时间(fireDate,timeZone,repeatInterval,repeatCalendar),如果你想根据时间来触发。 
* 通知行为(alertAction,hasAction),定义查看通知的操作名。 
* 触发通知时的启动画面(alertLaunchImage) 
* 通知的正文内容(alertBody), 
* 通知的背景声(soundName) 
* 通知消息数的展示(applicationIconBadgeNumber),就是强迫症们最讨厌的App图标上左上角的那个小数字 
* 其它(userInfo),可以给通知绑定一些处理通知时需要的额外信息。 

3.展示通知,展示通知有两个方式: 

* - (void)scheduleLocalNotification:(UILocalNotification *)notification:根据触发时间的配置展示通知消息, 
* - (void)presentLocalNotificationNow:(UILocalNotification *)notification:立即发送通知到本机 

栗子: 
Object-c代码  收藏代码
  1. - (void)showNotificationWithAction:(NSString *)action andContent:(NSString *)content  
  2. {  
  3.     UILocalNotification *notification = [[UILocalNotification alloc] init];  
  4.     notification.alertBody = content;  
  5.     notification.alertAction = action;  
  6.     notification.soundName = UILocalNotificationDefaultSoundName;  
  7.     [[UIApplication sharedApplication] presentLocalNotificationNow:notification];  
  8. }  
  9.   
  10. - (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {  
  11.     NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];  
  12.     NSDateComponents *dateComps = [[NSDateComponents alloc] init];  
  13.     [dateComps setDay:item.day];  
  14.     [dateComps setMonth:item.month];  
  15.     [dateComps setYear:item.year];  
  16.     [dateComps setHour:item.hour];  
  17.     [dateComps setMinute:item.minute];  
  18.     NSDate *itemDate = [calendar dateFromComponents:dateComps];  
  19.    
  20.     UILocalNotification *localNotif = [[UILocalNotification alloc] init];  
  21.     if (localNotif == nil)  
  22.         return;  
  23.     localNotif.fireDate = [itemDate dateByAddingTimeIntervalInterval:-(minutesBefore*60)];  
  24.     localNotif.timeZone = [NSTimeZone defaultTimeZone];  
  25.    
  26.     localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),  
  27.          item.eventName, minutesBefore];  
  28.     localNotif.alertAction = NSLocalizedString(@"View Details", nil);  
  29.    
  30.     localNotif.soundName = UILocalNotificationDefaultSoundName;  
  31.     localNotif.applicationIconBadgeNumber = 1;  
  32.    
  33.     NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];  
  34.     localNotif.userInfo = infoDict;  
  35.    
  36.     [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];  
  37. }  


处理Local Notification 

在处理本地通知时,我们需要考虑三种情况: 

1. App没有启动, 

这种情况下,当点击通知时,会启动App,而在App中,开发人员可以通过实现*AppDelegate中的方法:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,然后从lauchOptions中获取App启动的原因,若是因为本地通知,则可以App启动时对App做对应的操作,比方说跳转到某个画面等等。栗子: 
Object-c代码  收藏代码
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     NSLog(@"Start App....");  
  4.     ....  
  5.     UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];  
  6.     if ([self isWelcomeNotification:localNotification]) {  
  7.         NSLog(@"start with welcome notification");  
  8.         [self.mainViewController showOfficeIntroduction];  
  9.     }  
  10.     return YES;  
  11. }  


2. App运行在后台 
3. App运行在前台 

上面的2种情况的处理基本一致, 不同点只有当运行再后台的时候,会有弹窗提示用户另外一个App有通知,对于本地通知单的处理都是通过*AppDelegate的方法:- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification来处理的。 栗子: 
Object-c代码  收藏代码
  1. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification  
  2. {  
  3.     if ([self isWelcomeNotification:notification]) {  
  4.        [self.mainViewController showOfficeIntroduction];   
  5.     }if ([self isCustomerDataNotification:notification]) {  
  6.         [self.mainViewController showCustomerDataIntroduction];  
  7.     }  
  8. }  
内容概要:本文档详细介绍了基于MATLAB实现的无人机三维路径规划项目,核心算法采用蒙特卡罗树搜索(MCTS)。项目旨在解决无人机在复杂三维环境中自主路径规划的问题,通过MCTS的随机模拟与渐进式搜索机制,实现高效、智能化的路径规划。项目不仅考虑静态环境建模,还集成了障碍物检测与避障机制,确保无人机飞行的安全性和效率。文档涵盖了从环境准备、数据处理、算法设计与实现、模型训练与预测、性能评估到GUI界面设计的完整流程,并提供了详细的代码示例。此外,项目采用模块化设计,支持多无人机协同路径规划、动态环境实时路径重规划等未来改进方向。 适合人群:具备一定编程基础,特别是熟悉MATLAB和无人机技术的研发人员;从事无人机路径规划、智能导航系统开发的工程师;对MCTS算法感兴趣的算法研究人员。 使用场景及目标:①理解MCTS算法在三维路径规划中的应用;②掌握基于MATLAB的无人机路径规划项目开发全流程;③学习如何通过MCTS算法优化无人机在复杂环境中的飞行路径,提高飞行安全性和效率;④为后续多无人机协同规划、动态环境实时调整等高级应用打下基础。 其他说明:项目不仅提供了详细的理论解释和技术实现,还特别关注了实际应用中的挑战和解决方案。例如,通过多阶段优化与迭代增强机制提升路径质量,结合环境建模与障碍物感知保障路径安全,利用GPU加速推理提升计算效率等。此外,项目还强调了代码模块化与调试便利性,便于后续功能扩展和性能优化。项目未来改进方向包括引入深度强化学习辅助路径规划、扩展至多无人机协同路径规划、增强动态环境实时路径重规划能力等,展示了广阔的应用前景和发展潜力。
### 如何使用 `PushNotification.localNotification` 方法 #### 使用方法概述 为了在 React Native 应用程序中发送本地通知,可以调用 `PushNotification.localNotification` 函数。此函数允许应用程序向用户设备发送即时的通知消息[^3]。 #### 参数详情 该方法接受一个对象作为参数,这个对象包含了构建通知所需的各种属性。这些属性定义了通知的内容、行为以及外观等特性。常见的配置项包括但不限于: - **message**: 通知正文字符串。 - **title**: 通知标题字符串。 - **playSound**: 布尔值,指示是否播放声音,默认为 false。 - **soundName**: 如果设置了 playSound,则指定要播放的声音文件名(默认 'default' 表示系统默认提示音)。 - **vibrate**: 数组形式表示振动模式;如果设为 true 或者是非空数组则启用震动效果。 - **largeIcon**, **smallIcon**: Android 特有的图标路径设置选项。 - **bigText**: 当有大量文本时用于显示更详细的描述信息。 - **subText**: 子文本,在某些情况下会出现在主标题下方。 - **color**: 设置状态栏颜色 (仅限于Android)。 - **actions**: 定义点击动作按钮的行为列表。 - ...更多其他可选字段依据具体需求而定。 #### 示例代码展示 下面是一个简单的例子来演示如何触发带有基本配置的本地通知: ```javascript import PushNotification from 'react-native-push-notification'; // 发送一条简单的地方通知 function sendLocalNotification() { PushNotification.localNotification({ /* iOS 和 Android 都支持 */ id: "0", // (可选)唯一标识符,默认自动生成. ticker: "This is a ticker message", // (可选) title: "My notification title", // (必填) message: "Hello world!", // (必填) /* Android only properties */ largeIcon: "ic_launcher", smallIcon: "ic_notification", /* iOS and Android properties */ playSound: true, soundName: 'default', vibrate: true, /* 更多可能的属性... */ }); } ``` 通过上述方式即可轻松集成并测试本地推送功能到自己的 React Native 工程当中去。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值