iOS本地通知

AppDelegate

1,注册苹果的通知

1
2
3
4
5
6
7
8
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  //注册苹果的通知
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge
                                             |UIUserNotificationTypeSound
                                             |UIUserNotificationTypeAlert) categories:nil];
  [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  return YES; 
}

2,当用户退出app时创建一个通知,一定时间后调用,比如10秒。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//进入后台响应的方法 
- (void)applicationDidEnterBackground:(UIApplication *)application
{
  // 初始化本地通知对象
  UILocalNotification *notification = [[UILocalNotification alloc] init];
  if (notification) {
    // 设置通知的提醒时间
    NSDate *currentDate   = [NSDate date];
    notification.timeZone = [NSTimeZone defaultTimeZone]; // 使用本地时区
    notification.fireDate = [currentDate dateByAddingTimeInterval:10];
    // 设置提醒的文字内容
    notification.alertBody   = @"您一周都没有关注了";
    notification.alertAction = NSLocalizedString(@"关心", nil);
    // 通知提示音 使用默认的
    notification.soundName= UILocalNotificationDefaultSoundName;
    // 设置应用程序右上角的提醒个数
    notification.applicationIconBadgeNumber++;
    // 设定通知的userInfo,用来标识该通知
     
    NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:@"notification",@"nfkey",nil];
    [notification setUserInfo:dict];
    // 将通知添加到系统中
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
  }

}

3,在收到通知,点击进入应用的时候取消通知,讲外面显示的数字赋值为0,

1
application.applicationIconBadgeNumber=0;


didReceiveLocalNotification是app在前台运行,通知时间到了,调用的方法。如果程序在后台运行,时间到了以后是不会走这个方法的。


applicationDidBecomeActive是app在后台运行,通知时间到了,你从通知栏进入,或者直接点app图标进入时,会走的方法。


1
2
3
4
5
6
7
- (void)applicationDidBecomeActive:(UIApplication *)application {
     
     
    application.applicationIconBadgeNumber=0;
     
  
}
1
2
3
4
5
6
7
8
9
10
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
  application.applicationIconBadgeNumber=0;
  //取消通知
  for (UILocalNotification *noti in [application scheduledLocalNotifications]) {
    NSString *notiID = [noti.userInfo objectForKey:@"nfkey"];
    if ([notiID isEqualToString:@"notification"]) {
      [application cancelLocalNotification:noti];
    }
  }
}

4,当用户在没收到通知进入应用的时候取消通知。原因:当你第一次退出程序,就会创建一个通知a,10秒后推送,如果在这10秒内,重新登录退出又会创建 新的通知b,那么我们会连续收到两个通知。为了避免重复,在通知a时间还没有到情况下登录app我们就取消通知a,退出时创建通知b。

1
2
3
4
5
6
- (void)applicationWillEnterForeground:(UIApplication *)application
{
  //取消所有通知
    [application cancelAllLocalNotifications];
     
}

如果想要重复调用这个通知,

1
2
// 设置重复间隔
notification.repeatInterval = kCFCalendarUnitDay;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值