iOS关于本地推送通知的简单用法

iOSApp中经常会有弹出通知消息,也是现在比较常用的推送通知

本地推送通知是iOS系统自带的。

iOS8之后推送通知有了变化。下面展示一下本地通知。


AppDelegate.m:


#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    

    //版本判断

    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0f) {

        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound |UIUserNotificationTypeAlert categories:nil]];

        [[UIApplication sharedApplication] registerForRemoteNotifications];

        //注册通知

        [[UIApplication sharedApplication] registerForRemoteNotifications];

    }else {

        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];

        [self addLocationNotification];

    }

    

    

    

    return YES;

}

//ios8系统下得提示通知

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

{

    [self addLocationNotification];

}

//清除角标

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}



//添加本地通知

- (void)addLocationNotification

{

    //创建一个本地通知的对象

    UILocalNotification *location = [[UILocalNotification alloc]init];

    //设置触发时间

    location.fireDate = [NSDate dateWithTimeIntervalSinceNow:2.0];

    //设置重复的次数

    location.repeatInterval = kCFCalendarUnitSecond;

    

    //设置本地通知的属性

    location.alertBody = @"该减肥了,你都胖死了";

    //小红点提醒

    location.applicationIconBadgeNumber = 1;

    //向右滑动,打开

    location.alertAction = @"走,去减肥";

    //提示声音

    location.soundName = @"msg.caf";

    

    //调用

    [[UIApplication sharedApplication]scheduleLocalNotification:location];


    

}

最后展示结果:



你可能感兴趣的:(iOS)