通知

作者: 偏执_cbbe | 来源:发表于2017-07-15 08:07 被阅读0次

AppDelegate.m

-------------------

@implementation AppDelegate

//只有当应用在前台时,该方法才会被调用

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

//如果应用程序在前台,将应用程序图标上红色数字设为0

application.applicationIconBadgeNumber = 0;

//使用UIAlertView显示本地通知的信息

[[[UIAlertView alloc]initWithTitle:@"收到通知" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

return YES;

}

-----------------------------------

#import "ViewController.h"

@interface ViewController ()

{

UIApplication *app;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

app = [UIApplication sharedApplication];

}

- (IBAction)changed:(id)sender{

UISwitch *sw = (UISwitch *)sender;

if (sw.on) {

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

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

}

//创建一个本地通知

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

//设置通知的触发时间

notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];

//设置通知的时区

notification.timeZone = [NSTimeZone defaultTimeZone];

//设置通知的重复发送的时间间隔

notification.repeatInterval = kCFCalendarUnitMinute;

//设置通知的声音

notification.soundName = @"";

//通知标题

notification.alertTitle=@"啦啦啦";

// 设置当设备处于锁屏状态时,显示通知的警告框下方的title

notification.alertAction = @"OPEN";

// 设置通知是否可显示Action

notification.hasAction = YES;

// 设置通过通知加载应用时显示的图片

notification.alertLaunchImage = @"";

// 设置通知内容

notification.alertBody = @"主人,妞妞想你了!";

// 设置显示在应用程序上红色徽标中的数字

notification.applicationIconBadgeNumber = 1;

// 设置userinfo,用于携带额外的附加信息。

NSDictionary *info = @{@"bys": @"key"};

notification.userInfo = info;

// 调度通知

[app scheduleLocalNotification:notification];  // ①

}

else

{

// 获取所有处于调度中本地通知数组

NSArray *localArray = [app scheduledLocalNotifications];

if (localArray)

{

for (UILocalNotification *noti in localArray)

{

NSDictionary *dict = noti.userInfo;

if (dict)

{

// 如果找到要取消的通知

NSString *inKey = [dict objectForKey:@"bys"];

if ([inKey isEqualToString:@"key"])

{

// 取消调度该通知

[app cancelLocalNotification:noti];  // ②

}

}

}

}

}

}

相关文章

  • 通知!通知!

    我到现在已有三位粉丝,现在我要开始发福利啦! 我下一篇文章开始写小说啦!希望大家多多支持!谢谢!

  • 通知!通知!

    小仙近日找了一个编辑(啥也不是),有事请找她昂, @小编辑染染 她负责我的更新日常,而且此人闲的要死,如有评论,都...

  • 通知--通知

    因为事情开始多起来了,很难保证下午开始更了,所以决定了,之后每天早上更。 假期结束,很多事情需要去处理,现在不...

  • 通知-通知

    昨天下午,写了一篇文章,可有敏感词,没有成功通过审核,今天晚上,努力搞一篇出来,换另外一个故事,昨天的那个故事,我...

  • 通知! 通知 ! 通知书 !

  • 重要通知!重要通知!重要通知!

    非常感谢小伙伴的关注和支持,以后摄影相关内容将不再这里更新。 喜欢毒家摄影指南内容的小伙伴欢迎关注微信公众号:sm...

  • 推送通知-后台通知/静默通知

    后台远程推送iOS7开始允许应用收到通知后直接在后台(background)状态下运行一段代码,而无需用户点击,可...

  • 通知:本地通知和远程通知

    通知中心(NotificationCenter)和通知(UILocalNotification)是雷锋和雷峰塔的关...

  • Spring 返回通知 异常通知 环绕通知

    返回通知 在方法正常返回结果后的通知。返回通知可以访问到方法的返回值。 示例(附带返回结果的返回通知): 异常通知...

  • 通知与移除通知

    [[NSNotificationCenterdefaultCenter]postNotificationName:...

网友评论

      本文标题:通知

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