本章描述ios系統中計劃local notification,註冊 remote notification,以及處理 local和remote notification的步驟。本文中客戶端API中notification推送指的就是remote notfication的推送 html
第一個知識點:準備我的定製音頻做爲提示音, ios
請注意下面四個小問題----- 網絡
1,
系統能播放的四種音頻數據格式
Linear PCM
MA4 (IMA/ADPCM)
µLaw
aLaw
對應的後綴名能夠是aiff, wav, or caf file. Then,
2, 能夠用afconvert來轉換音頻,例如把16位的線性PCM系統音頻格式文件 Submarine.aiff 轉換成IMA4音頻,存爲.CAF文件。
在終端執行便可
afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v
3, 如何肯定音頻格式呢。
打開QuickTime Player-> Movie menu->Show Movie Inspector
4, 我的定製音頻必須是30s如下。不然就播放默認的聲音了。 app
第二個知識點:預約一個Local Notification
須要瞭解下面5個步驟
1, Allocate 和 initialize 一個 UILocalNotification對象。
2, fireDate屬性賦值
3, 設置別的幾個體型要素 提示框,提示音,圖標上的提示數字。也能夠帶別的個性化數據:經過userInfo帶出去。
4, 而後Schedule這個通知。經過UIApplication.scheduleLocalNotification來預約執行或者立馬執行presentLocalNotificationNow:
5, 也能夠取消這個通知。用這個方法:cancelLocalNotification和cancelAllLocalNotifications這個方法
看下代碼
//如何建立設定一個Notification
- (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:item.day];
[dateComps setMonth:item.month];
[dateComps setYear:item.year];
[dateComps setHour:item.hour];
[dateComps setMinute:item.minute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
item.eventName, minutesBefore];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
//程序運行在後臺時候如何提交一個UILocalNotification。
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Application entered background state.");
// bgTask is instance variable
NSAssert(self->bgTask == UIInvalidBackgroundTask, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self->bgTask];
self->bgTask = UIInvalidBackgroundTask;
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
NSString *friend = [self checkForIncomingChat];
if (friend) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:
NSLocalizedString(@"%@ has a message for you.", nil), friend];
localNotif.alertAction = NSLocalizedString(@"Read Message", nil);
localNotif.soundName = @"alarmsound.caf";
localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
[localNotif release];
friend = nil;
break;
}
}
}
[application endBackgroundTask:self->bgTask];
self->bgTask = UIInvalidBackgroundTask;
});
} async
第三個知識點:註冊 Remote Notifications ide
} ui
第四個知識點:處理 Local and Remote Notifications
通常通知來時,程序有兩種狀態。
1,後臺運行發送,須要窗體,聲音和數字
點擊窗體,啓動程序。程序啓動了在application:didFinishLaunchingWithOptions: 方法裏面獲取傳遞的數據
notification payload (for remote notifications) or
local-notification object (for local notifications).
2,程序在前臺跑着呢。
程序直接就調用application:didReceiveRemoteNotification: (for remote notifications)這個方法了
application:didReceiveLocalNotification: method (for local notifications)
總之要實現UIApplicationDelegate協議
實現application:didFinishLaunchingWithOptions:方法
實現application:didReceiveRemoteNotification:方法
或者實現application:didReceiveLocalNotification:方法
3,那如何判斷區別前臺仍是後臺這兩種情況呢。
用這個屬性applicationState來判斷。
若爲UIApplicationStateInactive就是用戶點擊通知框按鈕進來的。
若爲UIApplicationStateActive,就是前臺正跑着呢。
看下代碼
//Handling a local notification when an application is launched
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
[viewController displayItem:itemName]; // custom method
application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
這裏能夠經過UIApplicationLaunchOptionsRemoteNotificationKey來獲取通知傳遞過來的自定義數據
以後便可從provider方下數據了。
- (void)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)opts {
// check launchOptions for notification payload and custom data, set UI context
[self startDownloadingDataFromProvider]; // custom method
app.applicationIconBadgeNumber = 0;
// other setup tasks here....
}
Listing 2-6 Handling a local notification when an application is already running
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
NSString *itemName = [notif.userInfo objectForKey:ToDoItemKey]
[viewController displayItem:itemName]; // custom method
application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}
spa
本文爲意譯,原文地址:https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1 orm