本地通知

//.h

//注册

+ (void)registerLocalNotification;

//推送

+ (UILocalNotification *)pushLocalNotificationFor:(NSString *)alertBody;

+ (UILocalNotification *)pushLocalNotificationFor:(NSString *)alertBody userInfo:(NSDictionary *)userInfo;

+ (UILocalNotification *)pushLocalNotificationFor:(NSString *)alertBody userInfo:(NSDictionary *)userInfo fireSinceNow:(NSTimeInterval)time;

//.m

//注册

+ (void)registerLocalNotification {

//ios8之后注册通知 和之前不一样

#ifdef __IPHONE_8_0

//ios8  注册本地通知

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {

UIUserNotificationSettings *noteSetting =[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:noteSetting];

}

if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {

UIUserNotificationSettings    *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

[[UIApplication sharedApplication]registerUserNotificationSettings:setting];

}else {

UIRemoteNotificationType  myType = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

[[UIApplication sharedApplication]registerForRemoteNotificationTypes:myType];

}

#else

#endif

}

//推送

+ (UILocalNotification *)pushLocalNotificationFor:(NSString *)alertBody {

//在ios下应用分为两种不同的Notification种类

//1.本地      //2.远程(APNS)

//本地      Notification 是由ios系统下NotificationManger统一管理.只需要将组装好的本地通知对象添加到系统的Notification管理队列,系统就会在指定的时间激发该通知

//本地通知  只能在前台和后台触发,不能在其他状态下激发

//本地  流程

//1.1创建一个本地通知对象

UILocalNotification  *localNotification = [[UILocalNotification alloc]init];

//调度

//1.2 时间上的调度

//fireDate 就是激发的准确时间

localNotification.fireDate = [NSDate  dateWithTimeIntervalSinceNow:1.0];

//timeZone 是通知激发的时间是否根据时区的改变而改变,如果是nil,通知会在一段时间后激发,而不是某一确切的时间被激发

//repeatInteval 被重复激发的时间,时间差会根据日历单位,例如每周,如果不设置,将会被重复

//1.3 内容

localNotification.alertBody = kSTR(@"%@", alertBody);

//1.4其他内容

UIApplication  *app = [UIApplication sharedApplication];

app.applicationIconBadgeNumber ++;

//最后一步

//注册

[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

return localNotification;

}

+ (UILocalNotification *)pushLocalNotificationFor:(NSString *)alertBody userInfo:(NSDictionary *)userInfo {

UILocalNotification  *mzLocalNotification = [self pushLocalNotificationFor:alertBody];

mzLocalNotification.userInfo = userInfo;

return mzLocalNotification;

}

+ (UILocalNotification *)pushLocalNotificationFor:(NSString *)alertBody userInfo:(NSDictionary *)userInfo fireSinceNow:(NSTimeInterval)time {

UILocalNotification  *mzLocalNotification = [self pushLocalNotificationFor:alertBody userInfo:userInfo];

mzLocalNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:time];

return mzLocalNotification;

}

谢谢阅读!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 许多集成的步骤个推官网都有了,这里只写关于推送的远程推送和本地通知的步骤和代码。APP在后台时:走苹果的APNS通...
    AllureJM阅读 2,771评论 1 9
  • 这是我写的第一篇简书文章,写的不好,希望大家多多指导,多多交流. iOS的本地通知,多用于定时发送通知,比如游戏中...
    FR_Zhang阅读 2,757评论 13 4
  • 第一步:创建本地推送 本地通知 UILocalNotification // 创建⼀一个本地推送 UILocalN...
    夜千寻墨阅读 501评论 0 2
  • 本地通知简介: 本地通知简单来说app本身给应用程序推送消息,不需要服务器支持。使用场景:闹钟,定时操作。 1.创...
    DanDing阅读 251评论 0 1
  • 截至目前,你是成功和幸福的!(我们都很成功,你是更成功和幸福的一位!) 今天,我有幸和你谈论一个话题,“关于保险”...
    潘小芳阅读 246评论 0 0