系統服務之本地通知

開發中不單單有遠程推送還有本地通知,這樣能夠減輕服務器的壓力。服務器

1、在IOS8中須要先註冊app

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    return YES;
}

 2、在-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification方法中接收通知ide

//這個方法只有在程序啓動以後纔會執行,所以當程序處於後臺時,該方法不會執行
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"%@",notification);
    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"通知" message:@"下班了" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
    [alertView show];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}

 3、建立本地通知ui

//
//  ViewController.m
//  UILocalNotification
//
//  Created by City--Online on 15/5/15.
//  Copyright (c) 2015年 XQB. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILocalNotification *notification=[[UILocalNotification alloc]init];
    if (notification) {
        //設置推送時間
        NSDate *currentDate=[NSDate date];
        notification.timeZone=[NSTimeZone defaultTimeZone];
        notification.fireDate=[currentDate dateByAddingTimeInterval:10];
        
        //設置重複間隔  NSCalendarUnit類型 0表示不重複
        notification.repeatInterval=kCFCalendarUnitMinute;
        //設置提醒的文字
        //設備收到本地通知時橫額或鎖屏時的主要文字內容
        notification.alertBody=@"內容";
        notification.alertTitle=@"標題";
        //鎖屏時顯示的slide to後面的文字內容
        notification.alertAction=NSLocalizedString(@"我是誰?", nil);
        //提示音 默認
        notification.soundName=UILocalNotificationDefaultSoundName;
        //設置應用程序右上角的提醒個數
        notification.applicationIconBadgeNumber++;
        
        //設置通知的userInfo
        NSMutableDictionary *userInfo=[[NSMutableDictionary alloc]init];
        [userInfo setObject:@"kLocalNotificationID" forKey:@"kLocalNotificationID"];
        [userInfo setObject:@"cuiyanwei" forKey:@"name"];
        notification.userInfo=userInfo;
        //將通知添加到系統中
        //若是咱們的應用程序給系統發送的本地通知是週期性的,那麼即便把程序刪了重裝,以前的本地通知在重裝時依然存在(沒有從系統中移除)(註冊到系統服務裏並非在APP中)
        [[UIApplication sharedApplication]scheduleLocalNotification:notification];
//        [self delete];
        
    }
}
//刪除本地通知
-(void)delete
{
    //往系統服務裏面註冊以後有時會須要刪除
    //1.暴力刪除,取消全部的通知
    //[[UIApplication sharedApplication] cancelAllLocalNotifications];
    //2.刪除制定的通知
    for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        NSString *kLocalNotificationID=[notification.userInfo objectForKey:@"kLocalNotificationID"];
        NSLog(@"%@",kLocalNotificationID);
        if ([kLocalNotificationID isEqualToString:@"kLocalNotificationID"]) {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"取消通知" message:@"已經取消通知" delegate:self cancelButtonTitle:@"YES" otherButtonTitles:nil, nil];
            [alertView show];
            break;
        
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 4、運行結果3d

相關文章
相關標籤/搜索