ios8 本地通知

本文详细介绍了如何在iOS8环境下实现并管理本地通知,包括创建通知、取消通知、通知内容设置及通知类别配置等关键步骤。文章还涵盖了如何在程序启动时处理通知情况,以及如何在不同场景下响应本地推送消息。
//
//  ViewController.m
//  2014_11_07_本地通知 _ios8
//
//  Created by Mac10.9 on 14-11-7.
//  Copyright (c) 2014年 xiaoxiaobing. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createLocalNotification];
}



/**
 *  建立一个本地通知
 */
- (void)createLocalNotification
{
   
    //1.创建消息上面要添加的动作(按钮的形式显示出来)
    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    action.identifier = @"action";//按钮的标示
    action.title=@"马上处理";//按钮的标题
    action.activationMode = UIUserNotificationActivationModeForeground;
    //    action.authenticationRequired = YES;
    //    action.destructive = YES;
   
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
    action2.identifier = @"action2";
    action2.title=@"稍后处理";
    action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
   
    //开启的画则为进入程序 否则为取消通知
    action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
    action.destructive = YES;
   
    //2.创建动作(按钮)的类别集合
    UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
    categorys.identifier = @"alert";//这组动作的唯一标示
    [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
   
    //3.创建UIUserNotificationSettings,并设置消息的显示类类型
    UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
   
    //4.注册推送
   
    [[UIApplication sharedApplication] registerUserNotificationSettings:uns];
    /**
     ios8 需要注册通知
     */
    [[UIApplication sharedApplication] registerForRemoteNotifications];
   
    // 本地推送
    //一个本地通知就是一个任务
    // 1.创建 本地通知对象
    UILocalNotification *localNoti = [[UILocalNotification alloc] init];
    // 2.通知提醒的时间 多少时间以后提醒
    localNoti.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    //设置时区 默认时区就好跟 defaultTimeZone
    localNoti.timeZone = [NSTimeZone defaultTimeZone];
    // 3.设置通知的内容
    localNoti.alertBody = @"通知来了";
    // 4.设置锁屏的时显示消息
    localNoti.alertAction = @"xxxx";
    // 5.设置 点通知后 启动程序时的图片 默认情况下没有 应该设置成和程序的启动界面一样
    localNoti.alertLaunchImage = @"LaunchImage";
    // 6.设置图标右上角数字 马上就有 只会执行一次 不管通知多烧毁都只会执行一次
    localNoti.applicationIconBadgeNumber = 100;
    // 7.设置通知重复的间隔 最好不要重复 不会
    localNoti.repeatInterval = NSCalendarUnitSecond;
    // 8.添加额外的信息 可以在userInfo中找到
    localNoti.userInfo = @{@"detail": @"中午送花到公司 晚上烛光晚餐"};
    // 执行本地通知
   
    localNoti.category = @"alert";
    [[UIApplication sharedApplication] scheduleLocalNotification:localNoti];
}
/**
 *  取消所有的通知
 */
- (void)removeLocalNotification
{
    NSLog(@"取消通知");
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

@end


程序启动的时候的相关的通知情况处理跟ios7中一样 具体的可以参看我的博客

#import "AppDelegate.h"

@interface AppDelegate ()


@end

@implementation AppDelegate
           

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
   
   
//[[UIApplication sharedApplication] setApplicationIconBadgeNumber:10];
   
//1.创建消息上面要添加的动作(按钮的形式显示出来)
    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    action.identifier = @"action";//按钮的标示
    action.title=@"Accept";//按钮的标题
    action.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
    //    action.authenticationRequired = YES;
    //    action.destructive = YES;
   
    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
    action2.identifier = @"action2";
    action2.title=@"Reject";
    action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
    action.authenticationRequired = YES;//需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
    action.destructive = YES;

//2.创建动作(按钮)的类别集合
    UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
    categorys.identifier = @"alert";//这组动作的唯一标示
    [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];

//3.创建UIUserNotificationSettings,并设置消息的显示类类型
    UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];

//4.注册推送
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    [[UIApplication sharedApplication] registerUserNotificationSettings:uns];

//5.发起本地推送消息
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];
    notification.timeZone=[NSTimeZone defaultTimeZone];
    notification.alertBody=@"测试推送的快捷回复";
    notification.category = @"alert";
    [[UIApplication sharedApplication]  scheduleLocalNotification:notification];
   
    //用这两个方法判断是否注册成功
     NSLog(@"currentUserNotificationSettings = %@",[[UIApplication sharedApplication] currentUserNotificationSettings]);
    [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

   
    return YES;
}

//本地推送通知
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    //成功注册registerUserNotificationSettings:后,回调的方法
    NSLog(@"%@",notificationSettings);
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    //收到本地推送消息后调用的方法
    NSLog(@"%@",notification);
}

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
    //在非本App界面时收到本地消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮,notification为消息内容
    NSLog(@"%@----%@",identifier,notification);
   
    completionHandler();//处理完消息,最后一定要调用这个代码块
   
}

//远程推送通知
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    //向APNS注册成功,收到返回的deviceToken
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    //向APNS注册失败,返回错误信息error
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    //收到远程推送通知消息
}

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
    //在没有启动本App时,收到服务器推送消息,下拉消息会有快捷回复的按钮,点击按钮后调用的方法,根据identifier来判断点击的哪个按钮
}


@end



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值