2012年8月31日 星期五

[iOS] 發送 Local Notification 方法小記

最近剛研究完 Apple Push Notification Service (APNS) 來發送訊息給 user device。
有空再來整理整個設定流程,今天同事討論到也有直接本機發送的方式,上網找了一下寫個函式記錄以方便使用。

- (void)pushLocalNotification:(NSDate *)date
{
    //直接抓取目前時間後1分鐘

    //NSDate *after = [NSDate dateWithTimeIntervalSinceNow:(1 * 60)];

    //若有舊訊息則把它刪除
    NSArray *oldNotificatoins = [application scheduledLocalNotifications];
    if([oldNotificatoins count] > 0)
        [application cancelAllLocalNotifications];

    //建立一個訊息來發送
    UILocalNotification *alarm = [[[UILocalNotification alloc] init] autorelease];
    if(alarm)
    {
        alarm.fireDate = date;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"beep-beep.caf";
        alarm.alertBody = @"test local notification message!";
        [application scheduleLocalNotification:alarm];
    }

}

沒有留言:

張貼留言