iOS8 中的通知:Notification 草稿,正在整理中。。。。

在ios8中,有了关于本地通知和远程通知的新改动,下面来看看。

先看一段网上抄来的代码

  UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
    notificationAction1.identifier = @"Accept";
    notificationAction1.title = @"Accept";
    notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
    notificationAction1.destructive = NO;
    notificationAction1.authenticationRequired = NO;

    UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
    notificationAction2.identifier = @"Reject";
    notificationAction2.title = @"Reject";
    notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
    notificationAction2.destructive = YES;
    notificationAction2.authenticationRequired = YES;

    UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init];
    notificationAction3.identifier = @"Reply";
    notificationAction3.title = @"Reply";
    notificationAction3.activationMode = UIUserNotificationActivationModeForeground;
    notificationAction3.destructive = NO;
    notificationAction3.authenticationRequired = YES;

    UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
    notificationCategory.identifier = @"Email";

   // [notificationCategory setActions:@[notificationAction3] forContext:UIUserNotificationActionContextDefault];
    [notificationCategory setActions:@[notificationAction1] forContext:UIUserNotificationActionContextMinimal];

    NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];

    UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    localNotification.alertBody = @"Testing";
    localNotification.category = @"Email"; //  Same as category identifier
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

这是一段发送本地通知的代码,其中涉及到了这次更新的几个类,

UIMutableUserNotificationAction,UIMutableUserNotificationCategory,UIUserNotificationSettings

我们需要依次生成这几个对象,最后使用
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];把程序的通知设定注册到系统中去。

注意,当应用程序在前台时,通知到来,系统不会为程序弹出alert,而是会调用相应的

didReceiveLocalNotification 和

didReceiveRemoteNotification 这2个代理函数,把通知内容交给程序自身处理。而当程序不在前台时,系统才会为用户弹出alert,提示用户进行相应操作。 这里

UIMutableUserNotificationAction就代表了一个操作,在界面上的表示形式就是一个按钮,当点击按钮时,就会调用

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 或者

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0);

不会再调用

didReceiveLocalNotification 和

didReceiveRemoteNotification 这2个代理函数了。

UIMutableUserNotificationAction 中有一个属性,
activationMode,当它是
UIUserNotificationActivationModeBackground时,系统会选在在后台执行代码,执行的时间是有限的,我测试的是30s,如果超过30s你的程序还在后台执行,那么系统会终止调你的程序,并抛出异常。异常如下
<Warning>: <BKNewProcess: 0x17e685f0; -.bbbb; pid: 922; hostpid: -1> has active assertions beyond permitted time:
    {(
        <BKProcessAssertion: 0x17d6b4f0> id: 42-2B9D290F-7F21-4DCB-955B-9D80DE693382 name: Notification action process: <BKNewProcess: 0x17e685f0; -.bbbb; pid: 922; hostpid: -1> permittedBackgroundDuration: 30.000000 reason: notificationAction owner pid:42 preventSuspend  preventThrottleDownUI  preventIdleSleep  preventSuspendOnSleep ,
        <BKProcessAssertion: 0x17d6de50> id: 42-707C3B54-51BC-47DA-B779-B11888416FE4 name: Deliver Message process: <BKNewProcess: 0x17e685f0; -.bbbb; pid: 922; hostpid: -1> permittedBackgroundDuration: 10.000000 reason: suspend owner pid:42 preventSuspend  preventThrottleDownCPU  preventThrottleDownUI  preventSuspendOnSleep
    )}

注意,如果你没有点击action按钮,而是通过点击通知内容本身,那么系统扔会像ios7一样,调用

didReceiveLocalNotification 和

didReceiveRemoteNotification 这2个代理函数。

如果程序没有启动,用户点击action按钮后,系统会先调用

didFinishLaunchingWithOptions启动程序,再调用

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 或者

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0);

来处理action,这一点和以前点击通知内容本身的效果一致,都会先启动程序,再调用相应函数。

 
 
时间: 2024-08-29 20:53:49

iOS8 中的通知:Notification 草稿,正在整理中。。。。的相关文章

[译] C# 5.0 中的 Async 和 Await (整理中...)

C# 5.0 中的 Async 和 Await [博主]反骨仔 [本文]http://www.cnblogs.com/liqingwen/p/6069062.html 伴随着 .NET 4.5 和 Visual Studio 2012 的 C# 5.0 ,我们可以使用的新的异步模式,这里涉及到 async 和 await 关键字.有许多不同点的观点,比起之前我们所看到的代码,它的可读性和实用性是否更加突出.我们将通过一个例子,看看它与当前的做法有何“与众不同”. 线性代码与非线性代码 大部分的软

iOS开发中通知(Notification)快速入门及推送通知实现教程

iOS开发中通知(Notification)快速入门及推送通知实现教程 标签: NSNotificationCenterNSNotification 2016-11-14 00:18 232人阅读 评论(0) 收藏 举报  分类: iOS(400)  转载自:http://www.111cn.NET/sj/ios8/90190.htm 通知(Notification)是开发框架中观察者模式的一种实现方式,内部的实现机制由Cocoa框架支持,通常用于试图控制器和数据模型的交互.通过通知,可以向一个

iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)

iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了-但是也无所谓,有新东西不怕,学会使用了就行.接下来会探讨一下这些个新的Controller. - (void)showOkayCancelAlert { NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);

IOS开发之 ---- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)

群号:49690168 转:http://blog.csdn.net/liangliang103377/article/details/40078015 iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了-但是也无所谓,有新东西不怕,学会使用了就行.接下来会探讨一下这些个新的Controller. - (void)showOkayCancelAle

iOS8中的UIAlertController

转:      iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了-但是也无所谓,有新东西不怕,学会使用了就行.接下来会探讨一下这些个新的Controller. - (void)showOkayCancelAlert { NSString *title = NSLocalizedString(@"A Short Title Is Best"

安卓学习之通知(Notification)

安卓中创建通知需要借助NotificationManager来对通知进行管理.可以调用Context的getsSystemService()方法来获得. getsSystemService()方法接收一个参数,这个参数是字符串,用于指定哪一个服务.Context.NOTIFICATION_SERVICE 就是指定通知服务. 这个方法返回一个Object对象,所欲需要进行强制转换. NotificationManager manager = (NotificationManager) getSys

iOS中的通知(NSNotification)

iOS中的通知(NSNotification) 前言 通知中心是一个单例.通知在iOS中是一种设计模式.每一个应用程序都有一个通知中心NSNotificationCenter实例, 专门负责协助不同对象之间的消息通信. 任何一个对象都可以向通知中心发布NSNotification, 描述自己在做什么,而任何注册了该通知的对象该特定通知发布的时候会收到这个通知. 获取通知中心对象 通过下面的方式来获取通知中心对象: 1 2 3 NSNotificationCenter *center = [NSN

通知 Notification 详解

效果 通知栏-刚收到通知时 通知栏-收到通知几秒后 标准视图 大视图-下滑前是标准视图 大视图-下滑后显示大视图 自定义通知 讲解 Notification,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容. 注意:因为一些Android版本的兼容性问题,对于Notification而言,Android3.0是一个分水岭,在其之前构建Notification推荐使用Notification.Builder构建,而在An

IOS中的通知NSNotification

类似于Android中的广播接收者发送者 1.一般完整的通知包含三个属性 1 -(NSString *)name ;//通知的名称 2 -(id)object ;//通知发布者(是谁要发布通知) 3 -(NSDictionary *)userInfo;//一些额外的信息(通知发布者传递给通知接收者的信息内容) 2.初始化一个通知(NSnotification)对象 1 +(instancetype)notificationWithName:(NSString *)aName object:(id