1. 程式人生 > >IOS 本地訊息推送

IOS 本地訊息推送

1、第一步先註冊推送

在AppDelegate.m編寫一個推送註冊方法如下:

- (void)registerLocalPush

{

UILocalNotification *localNotifi = [UILocalNotificationnew];

    [[UIApplicationsharedApplication] scheduleLocalNotification:localNotifi];

/*

     UIUserNotificationTypeBadge   = 1 << 0, // the application may badge its icon upon a notification being received

     UIUserNotificationTypeSound   = 1 << 1, // the application may play a sound upon a notification being received

     UIUserNotificationTypeAlert   = 1 << 2, // the application may display an alert upon a notification being

     */

UIUserNotificationSettings *settings = [UIUserNotificationSettings

settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlertcategories:nil];

    [[UIApplicationsharedApplication] registerUserNotificationSettings:settings];

}

2、在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函式中呼叫如下:

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

// Override point for customization after application launch.

    [selfregisterLocalPush];

returnYES;

}

3、設定推送時間

- (void)pushLocalNotifi

{

// 1.建立通知

UILocalNotification *localNotification = [[UILocalNotificationalloc] init];

// 2.設定通知的必選引數

// 設定通知顯示的內容

    localNotification.alertBody = @"推送顯示的資訊";

// 設定通知的傳送時間,單位秒,在多少秒之後推送

    localNotification.fireDate = [NSDatedateWithTimeIntervalSinceNow:10];

//解鎖滑動時的事件

    localNotification.alertAction = @"XXOOXX";

//收到通知時App icon的角標

    localNotification.applicationIconBadgeNumber = 0;

//推送是帶的聲音提醒,設定預設的欄位為UILocalNotificationDefaultSoundName

    localNotification.soundName = UILocalNotificationDefaultSoundName;

//設定推送自定義聲音格式

    //localNotification.soundName = @"檔名.副檔名";

//迴圈次數

    localNotification.repeatInterval = kCFCalendarUnitDay;

// 3.傳送通知(根據專案需要使用)

// 方式一: 根據通知的傳送時間(fireDate)傳送通知

    [[UIApplicationsharedApplication] scheduleLocalNotification:localNotification];

}

4、呼叫推送時間設定

- (IBAction)clickLocalPush:(id)sender

{

    [selfpushLocalNotifi];

}