iOS UIKit框架:本地通知 UILocalNotification

本文介绍了在iOS应用中实现本地通知的具体步骤,包括如何创建、触发和监听本地通知,并对比了本地通知与远程通知的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

程序需要做 聊天消息通知,因为之前没有做过通知,不知道用本地通知还是远程通知,就看了下本地通知

1、先看下添加本地通知代码

    UILocalNotification* noti = [[UILocalNotification alloc]init];
     // 设置通知的信息
    noti.alertBody = @"本地通知";
    noti.alertAction = @"打开程序";
    noti.applicationIconBadgeNumber = 1;
    noti.repeatInterval = NSCalendarUnitMinute;
    noti.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
// ios8后,需要添加这个注册,才能得到授权  
  if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {  
    UIUserNotificationType type =  UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;  
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type  
                                                                             categories:nil];  
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];  

 


// 发出通知 [[UIApplication sharedApplication] scheduleLocalNotification:noti];

然后运行程序,就可以发出通知了, 但是程序 必须在后台才可以看到通知的效果,而如果应用程序正在运行时,是看不到通知的效果。

 2、移除通知

    // 移除所有的通知
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

3、监听本地通知的点击

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"%s",__func__);
}

总结下:

  本地通知,只能定时发送些消息,不能满足我的需求,看来是要用远程通知

转载于:https://www.cnblogs.com/16zj/p/4975976.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值