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,这一点和以前点击通知内容本身的效果一致,都会先启动程序,再调用相应函数。

 
 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。