登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

本地推送 可以拿去做闹钟了

[复制链接]
来自: 你丫的 分类: iOS精品源码 上传时间: 2016-6-3 11:42:53
Tag:推送

项目介绍:

求赞求赞
本地推送
[Objective-C] 查看源文件 复制代码
//
//  LocalPushCenter.h
//  LocalPush
//
//  Created by zhuochenming on 3/11/16.
//  Copyright (c) 2016 zhuochenming. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface LocalPushCenter : NSObject

+ (NSDate *)fireDateWithWeek:(NSInteger)week
                        hour:(NSInteger)hour
                      minute:(NSInteger)minute
                      second:(NSInteger)second;

//本地发送推送(先取消上一个 再push现在的)
+ (void)localPushForDate:(NSDate *)fireDate
                  forKey:(NSString *)key
               alertBody:(NSString *)alertBody
             alertAction:(NSString *)alertAction
               soundName:(NSString *)soundName
             launchImage:(NSString *)launchImage
                userInfo:(NSDictionary *)userInfo
              badgeCount:(NSUInteger)badgeCount
          repeatInterval:(NSCalendarUnit)repeatInterval;

#pragma mark - 退出
+ (void)cancelAllLocalPhsh;

+ (void)cancleLocalPushWithKey:(NSString *)key;

@end



[Objective-C] 查看源文件 复制代码
//  LocalPushCenter.m
//  LocalPush
//
//  Created by zhuochenming on 3/11/16.
//  Copyright (c) 2016 zhuochenming. All rights reserved.
//

#import "LocalPushCenter.h"

@implementation LocalPushCenter

+ (NSDate *)fireDateWithWeek:(NSInteger)week
                        hour:(NSInteger)hour
                      minute:(NSInteger)minute
                      second:(NSInteger)second {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setTimeZone:[NSTimeZone defaultTimeZone]];
    
    unsigned currentFlag = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitSecond;
    
    NSDateComponents *component = [calendar components:currentFlag fromDate:[NSDate date]];
    
    if (week) {
        component.weekday = week;
    }
    if (hour) {
        component.hour = hour;
    }
    
    if (minute) {
        component.minute = minute;
    }
    if (second) {
        component.second = second;
    } else {
        component.second = 0;
    }
    
     return [[calendar dateFromComponents:component] dateByAddingTimeInterval:0];
}

#pragma mark - 本地推送
+ (void)localPushForDate:(NSDate *)fireDate
                  forKey:(NSString *)key
               alertBody:(NSString *)alertBody
             alertAction:(NSString *)alertAction
               soundName:(NSString *)soundName
             launchImage:(NSString *)launchImage
                userInfo:(NSDictionary *)userInfo
              badgeCount:(NSUInteger)badgeCount
          repeatInterval:(NSCalendarUnit)repeatInterval {
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (!localNotification) {
        return;
    }
    
    [self cancleLocalPushWithKey:key];
    
    NSUInteger notificationType; //UIUserNotificationType(>= iOS8) and UIRemoteNotificatioNType(< iOS8) use same value
    UIApplication *application = [UIApplication sharedApplication];
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
        notificationType = [[application currentUserNotificationSettings] types];
    } else {
        notificationType = [application enabledRemoteNotificationTypes];
    }
    if (notificationType == UIRemoteNotificationTypeNone) {
        return;
    }
    
    // ios8后,需要添加这个注册,才能得到授权
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        // 通知重复提示的单位,可以是天、周、月
        localNotification.alertBody        = alertBody;
        localNotification.alertAction      = alertAction;
        localNotification.alertLaunchImage = launchImage;
        localNotification.repeatInterval   = repeatInterval;
    } else {
        localNotification.alertBody        = alertBody;
        localNotification.alertAction      = alertAction;
        localNotification.alertLaunchImage = launchImage;
        localNotification.repeatInterval   = repeatInterval;
    }

    //Sound
    if (soundName) {
        localNotification.soundName = soundName;
    } else {
        localNotification.soundName = UILocalNotificationDefaultSoundName;
    }
    
    //Badge
    if ((notificationType & UIRemoteNotificationTypeBadge) != UIRemoteNotificationTypeBadge) {
    } else {
        localNotification.applicationIconBadgeNumber = badgeCount;
    }
    
    if (!fireDate) {
        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    } else {
        localNotification.fireDate = fireDate;
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }
    
}

#pragma mark - 退出
+ (void)cancelAllLocalPhsh {
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

+ (void)cancleLocalPushWithKey:(NSString *)key {
    NSArray *notiArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    if (notiArray) {
        for (UILocalNotification *notification in notiArray) {
            NSDictionary *dic = notification.userInfo;
            if (dic) {
                for (NSString *key in dic) {
                    if ([key isEqualToString:key]) {
                        [[UIApplication sharedApplication] cancelLocalNotification:notification];
                    }
                }
            }
        }
    }
}

@end


https://github.com/zhuochenming/LocalPushCenter

相关源码推荐:

我来说两句
*滑动验证:
所有评论(28)
yu2610 2016-6-4 10:27:53
感谢分享,Code4App有你更精彩
回复
1586673438 2016-6-4 15:22:47
虽不明,但觉厉!我都快忘了这玩意了!
回复
lichengwuios 2016-6-5 09:42:30
感谢分享,Code4App有你更精彩
回复
zhao3031461 2016-6-6 08:52:56
虽不明,但觉厉!
回复
RYC 2016-6-6 09:17:47
号。。。。。。。。。。。。。。。。。。
回复
半吱湮 2016-6-6 10:27:40
感谢分享,Code4App有你更精彩
回复
⒎冄31.號 2016-6-7 08:32:49
强烈支持楼主ing……
回复
狼小逸 2016-6-7 10:13:34
正需要啊,感谢楼主无私分享!
回复
胡英俊.App 2016-6-7 15:21:43
虽不明,但觉厉!
回复
123下一页
7060 5 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542

如何获得代码达人称号?

代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表