美文网首页
ios, 通知消息定义

ios, 通知消息定义

作者: pony23 | 来源:发表于2017-05-04 13:40 被阅读0次

在iOS8和iOS9下,notification增加了一些新的特性,使之更加强大: - iOS8增加了下拉时的Action按钮,像微信一样; - iOS9增加了像信息一样的可以下拉直接输入;iOS8为了实现action按钮,增加了4个类,一起完成这个功能: - UIUserNotificationSettings; - UIUserNotificationType:alert/sound/badge - UIUserNotificationCategory; - UIUserNotificationAction;闲话少说,还是代码讲的清楚:    //初始化action    UIMutableUserNotificationAction* action1 =        [[UIMutableUserNotificationAction alloc] init];    //设置action的identifier    [action1 setIdentifier:@"action1"];    //title就是按钮上的文字    [action1 setTitle:@"title1"];    //设置点击后在后台处理,还是打开APP    [action1 setActivationMode:UIUserNotificationActivationModeBackground];    //是不是像UIActionSheet那样的Destructive样式    [action1 setDestructive:NO];    //在锁屏界面操作时,是否需要解锁    [action1 setAuthenticationRequired:NO];    UIMutableUserNotificationAction* action2 = [[UIMutableUserNotificationAction alloc] init];    [action2 setIdentifier:@"action2"];    [action2 setTitle:@"title2"];    [action2 setActivationMode:UIUserNotificationActivationModeForeground];    [action2 setDestructive:NO];    [action2 setAuthenticationRequired:NO];    //一个category包含一组action,作为一种显示样式    UIMutableUserNotificationCategory* category = [[UIMutableUserNotificationCategory alloc] init];    [category setIdentifier:@"category1"];    //minimal作为banner样式时使用,最多只能有2个actions;default最多可以有4个actions    [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextMinimal];    NSSet* set = [NSSet setWithObject:category];    UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound                                                                            categories:set];    //注册notification设置    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];    //添加一个notification,10秒后触发    UILocalNotification* notification = [[UILocalNotification alloc] init];    //设置notifiction的样式为"category1"    [notification setCategory:@"category1"];    [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];    [notification setAlertBody:@"this is an alert"];    [[UIApplication sharedApplication] scheduleLocalNotification:notification];

结果是这个样子的: 这时候点击action1,alert就直接消失了,因为action1的setActivationMode是background的;而点击action2,app就会被打开; 无论哪个action,系统打开的时候都会调用AppDelegate的-(void)application: handleActionWithIdentifier: forLocalNotification:(nonnull UILocalNotification *)completionHandler:方法;如果直接点击alert的话,会调用application: didFinishLaunchingWithOptions:方法,而options参数里就包含了alert的信息;几个要注意的地方:push notification的时候,如何指定alert的样式,即category? 只需要在push的内容里添加category的Identifier:"aps":{    "alert":"This is a notification",    "category":}

APP正在运行的时候,收到了notification,如何接收?

此时会调用application:didReceiveLocalNotification:方法;而系统不会有任何操作,不会发出声音、添加badge icon、也不会弹出alert;

alert可以设置一个alertLaunchImage,可以替代系统默认的launch image;

APP的badge number如何自动+1:

苹果没有提供这个方法,因此我们要在服务器上保存某个用户的badge number,在推送时+1;

能不能像信息那样,下拉直接回复?

iOS8没有这个功能,最新的iOS9可以:

action.behavior = UIUserNotificationBehaviorTextInput;

iOS8时,增加了在指定地点弹出的功能:[notification setRegion:];

如何确定用户有没有禁止发送notifications:

设置notificationCategory后,检查AppDelegate有没有调用下面的方法:

- (void) application: didRegisterUserNotificationSettings:

相关文章

  • ios, 通知消息定义

    在iOS8和iOS9下,notification增加了一些新的特性,使之更加强大: - iOS8增加了下拉时的Ac...

  • ios 消息通知

    苹果的通知分为本地通知和远程通知 若用户直接启动,lauchOptions内无数据; 若由其他应用程序通过open...

  • iOS模式之二:代理模式

    iOS中消息传递方式 在iOS中有很多种消息传递方式,这里先简单介绍一下各种消息传递方式。 通知:在iOS中由通知...

  • iOS消息通知产品设计

    ios消息类型有本地通知、推送消息以及系统消息。 消息类型: 本地通知:是由ios设备生成并发布的,无论应用程序是...

  • iOS 远程推送通知

    iOS 远程推送通知 分分钟搞定IOS远程消息推送 iOS推送通知的实现步骤 推送通知iOS客户端编写实现及推送服...

  • Swift JPush极光推送通知和自定义消息

    在开始之前,首先了解下自定义消息和通知的几点区别。 自定义消息和通知的区别 收到推送自定义消息时推送通知栏不显示 ...

  • iOS 消息通知(NSNotification)

    消息通知传递信息的方法有好多种,消息通知便是其中的一种消息通知的优点是可以一对多进行信息传递,可以隔层传递1、观察...

  • iOS 本地消息通知

    本地消息通知的流程 1.注册 通过调用requestAuthorization这个方法,通知中心会向用户发送通知许...

  • iOS问题解决(三):模拟器收不到UILocalNotifica

    通知功能是iOS应用开发经常会碰到的需求,iOS应用的通知分为本地通知和远程通知(即消息推送),Apple dev...

  • 推送MobPush-API说明

    1. 消息监听接口 MobPushReceiver:消息监听接口(包含接收自定义消息、通知消息、通知栏点击事件、别...

网友评论

      本文标题:ios, 通知消息定义

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