码迷,mamicode.com
首页 > 移动开发 > 详细

ios本地推送互动

时间:2016-03-07 13:40:59      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

?    小编只是一个小白,每个app都需要推送功能,个人觉得是基础中的基础,所以自学了下推送,并且写了点小demo,也是第一次发原创博客,还请各路大神批评指导。

    信息推送可以让App尚未运行的状态下可以接受一些信息,通常的推送有苹果自带的APNS推送,这个方面的知识还请自行查询学习,本文只是针对本地推送互动方面,后续会单独写APNS推送的博文,还请持续关注!

    推送信息文字主要出现四个位置:1.屏幕锁定画面;2.屏幕上方下拉的“通知区域”;3.解锁后屏幕最上方的横幅的位置;4解锁后的提示位置;解锁模式下推送信息的位置需要在自己去调整 在系统--->通知--->要调整的App下面调整

   技术分享

    推送信息的处理有互动按钮(本文使用四个),可以把这四个按钮放在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中,封装在UIMutableUserNOtificationCategory类中,一个App可以有很多组Category,以供推送信息时选 择,最后将category封装到NSSet中,注册到UIUserNotificationSettings中。

代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UIMutableUserNotificationAction *action1=[UIMutableUserNotificationAction new];
    action1.identifier=@"ACTION_1";
    action1.title=@"title";
    action1.activationMode=UIUserNotificationActivationModeForeground;
    action1.authenticationRequired=YES;
    action1.destructive=NO;
    
    UIMutableUserNotificationAction *action2=[UIMutableUserNotificationAction new];
    action2.identifier=@"ACTION_2";
    action2.title=@"title2";
    action2.authenticationRequired=NO;
    action2.activationMode=UIUserNotificationActivationModeBackground;
    action2.destructive=YES;
    
    UIMutableUserNotificationAction *action3=[UIMutableUserNotificationAction new];
    action3.identifier=@"ACTION_3";
    action3.title=@"title3";
    action3.activationMode=UIUserNotificationActivationModeBackground;
    action3.authenticationRequired=NO;
    action3.destructive=NO;;
    
    UIMutableUserNotificationAction *action4=[UIMutableUserNotificationAction new];
    action4.identifier=@"ACTION_4";
    action4.title=@"titile4";
    action4.activationMode=UIUserNotificationActivationModeBackground;
    action4.authenticationRequired=NO;
    action4.destructive=NO;
    
    UIMutableUserNotificationCategory *category=[UIMutableUserNotificationCategory new];
    category.identifier=@"CATEGORY";
    [category setActions:@[action1,action2,action3,action4] forContext:UIUserNotificationActionContextDefault];
    NSSet *categories=[NSSet  setWithObject:category];
    UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:categories];
    [application registerUserNotificationSettings:setting];
    return YES;
}

 

这 其中:identifier是用来识别用户点击了哪个按钮的;title是按钮的文字;activationMode是指这个按钮按下去是不是要把app 调到前台运行,设置为前台运行就需要赋值为YES;authenticationRequired是只当设备属于锁定状态时候,是否要求用户先解 锁;destructive是标识特别重要的按钮,操作他可能带来一定的影响,例如删除数据。

    互动操作需要在appdelega.m的一个方法中写互动操作方法代码如下:

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler
{
    if ([notification.category isEqualToString:@"CATEGORY"]) {
        if ([identifier isEqualToString:@"ACTION_1"]) {
            NSLog(@"单击了Category1,Action1");
        }
        if ([identifier isEqualToString:@"ACTION_2"]) {
            [UIApplication sharedApplication].applicationIconBadgeNumber=0;
            NSLog(@"单击了Category2,Action2");
        }
        if ([identifier isEqualToString:@"ACTION_3"]) {
            NSLog(@"单击了Category3,Action3");
        }
        if ([identifier isEqualToString:@"AACTION_4"]) {
            NSLog(@"单击了Category4,Action4");
        }
    }
    completionHandler();//必写
}

 

最后在viewDidLoad写(图方便)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UILocalNotification *note=[UILocalNotification new];
    note.alertBody=@"自定义推送通知";
    note.category=@"CATEGORY";
    note.applicationIconBadgeNumber=1;
    note.soundName=UILocalNotificationDefaultSoundName;
    note.fireDate=[NSDate dateWithTimeIntervalSinceNow:10];
    [[UIApplication sharedApplication] scheduleLocalNotification:note];
}

 

谢谢!初次写,不足之处请谅解!!!

ios本地推送互动

标签:

原文地址:http://www.cnblogs.com/liucongkun/p/5249963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!