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];


    

}

最後展示結果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章