//
// 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