美文网首页
iOS本地推送

iOS本地推送

作者: Rasho_Moon | 来源:发表于2016-09-11 16:22 被阅读0次

    通过UILocalNotification 实现本地通知的推送

    1 创建通知

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];  
    notification.alertTitle = @"打开广播";
    notification.alertBody = @"广播";
    notification.repeatInterval = NSCalendarUnitSecond;
    //设置本地通知的时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    //设置角标的个数
    notification.applicationIconBadgeNumber=1;
        
    notification.userInfo = @{@"info":@"broadcast"};
    notification.soundName = UILocalNotificationDefaultSoundName;
    

    2 取消通知

    UIApplication *app = [UIApplication sharedApplication];
    NSArray *array = [app scheduledLocalNotifications];        
    for (UILocalNotification * local in array) {
        NSDictionary *dic = local.userInfo;
        if ([dic[@"info"]  isEqual: @"broadcast"]) {
            //删除指定的通知
            [app cancelLocalNotification:local];
        }
    }
    //也可以使用[app cancelAllLocalNotifications]删除所有通知;
    

    3 app跳出到后台时,可在下列方法内添加通知提示

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
    

    4 app处于运行阶段时

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
        //判断应用程序当前的运行状态是否为激活状态
        if (application.applicationState == UIApplicationStateActive) {
            //只有激活状态 在app运行时本地通知才有效
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS本地推送

          本文链接:https://www.haomeiwen.com/subject/gjeuettx.html