iOS之本地通知 NSLocalNotification

      iOS 推送通知分为本地推送和远程推送通知,远程推送通知就类似于我们平时使用微信时,即使锁屏了,也能收到好友发送给我们的消息,然后在主屏幕显示一个alertview,远程推送需要远程服务端的支持,比较复杂. 本地推送相对比较简单,不需要服务端的支持。

       本地通知是NSLocalNotification 实现的,通过实例化一个NSLocalNotification类型的通知,同时设置通知的fireDate 属性,即通知的触发时间;设置timeZone属性,即时区;设置alertBody,显示的内容;设置alertAction;设置soundName,即推送发生时的声音;设置applicationIconBadgeNumber,即图标上的数字;设置userInfo属性,该属性是一个NSDictionary类型的变量。然后在使用UIApplication 的 实例方法scheduleLocalNotification:或 presentLocalNotificationNow: 推送通知。

      在Appdelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加如下代码:

NSDate * itemDate = [NSDate date];
    
    UILocalNotification * localNotif = [[UILocalNotification alloc] init];
    if(localNotif == nil)
        return ;
    localNotif.fireDate = [itemDate dateByAddingTimeInterval:60];
    NSLog(@"fireDate is %@",localNotif.fireDate);
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes", nil),item.eventName,minutesBefore];
    localNotif.alertAction = NSLocalizedString(@"View Details", nil);
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    
    NSDictionary * infoDict = [NSDictionary dictionaryWithObjectsAndKeys:item.eventName,ToDoItemKey,@"Local Push reveived while running",MessageTitleKey ,nil];
    localNotif.userInfo = infoDict;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    NSLog(@"scheduledLocalNotifications are %@",[[UIApplication sharedApplication] scheduledLocalNotifications]);
 当应用程序收到本地推送通知是发调用Appdelegate的 -( void )application:( UIApplication *)application didReceiveLocalNotification:( UILocalNotification *)notification方法,在该方法中显示通知的内容:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"application : didReceiveLocalNotificaiton:");
    NSString * itemName = [notification.userInfo objectForKey:ToDoItemKey];
    NSString * messageTitle = [notification.userInfo objectForKey:MessageTitleKey];
    [self _showAlert:itemName withTitle:messageTitle];
    NSLog(@"Receive Local Notification while the app is still running ...");
    NSLog(@"current notification is %@",notification);
    application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber - 1;
}
-(void)_showAlert:(NSString *)pushmessage withTitle:(NSString *)title
{
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alertView show];
}

转载于:https://my.oschina.net/CarlHuang/blog/139104

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值