(進度:未完,停留在沒有實機無法測PUSH跟LOCAL NOTIFICATION)
稍微看了一下Session 713(Notification),這次主要在IOS8新增了一些功能 分別是:
- User Notifications
- Notification Actions
- Remote Notifications
- Location Notifications
首先,不管是使用 Remote 或者是 Local notification都必須要註冊,並且徵求使用者同意。
Notification 的 type 也跟之前的 Push 一樣分為 Badge, Sound, Alert
Notification 在不同的地方,顯示的Action數量也會不同
地方 | 手勢 | Action數量 |
---|---|---|
lock screen | 向左滑 | 2 actions |
notification center | 向左滑 | 2 actions |
banners | 往下拉 | 2 actions |
alertview | 無 | 4 actions |
Notification 主要有三大步驟
- Register Actions(註冊Actions)
-
Action
(自定義Acation,包含他的Identifier, title, activationMode, destructive, authenticationRequired共五個屬性)
-
Category
(把Actions group in Category裡,例如:郵件Category可以包含寫郵件,讀郵件,刪除郵件三種Action,如下面Table)
-
Settings
(你的APP當然不是只有郵件Category,可能還有廣告Category,吃飯Category。用NSSet把這三個Category打包起來)
- Send push Notification or schedule local notification(發出通知)
- Handle Action
Category | Actions |
---|---|
Invite | Accept, Maybe, Decline |
New mail | Mark as Read, Trash |
Tagged | Like, Comment, Untag |
IOS 7 | IOS 8 |
---|---|
User notifications (sound, badge, banner, alertiview, notification center,lock screen) Silent notifications |
User Notifications Notification Actions Remote Notifications Location Notifications |
- Must register to use
- Require user approval
- (必須註冊才能使用,並且徵求使用者同意)
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings
settingsForTypes:types categories:nil];
[[UIApplication sharedApplication]
registerUserNotificationSettings:mySettings];
//UIApplicationDelegate Callback
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:
(UIUserNotificationSettings *)notificationSettings {
// user has allowed receiving user notifications of the following types
UIUserNotificationType allowedTypes = [notificationSettings types];
}
//Getting Settings
- (void)getReadyForNotification {
// ...
// check to make sure we still need to show notification
UIUserNotificationSettings *currentSettings = [[UIApplication
sharedApplication] currentUserNotificationSettings];
[self checkSettings:currentSettings];
}
- lock screen (left) 2 actions
- notification center (left) 2 actions
- banners (pull down) 2 actions
- alertview 4 actions
- Register Actions
- Action
- Category
- Setting
- Send push Notification or schedule local notification
- Handle Action
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"ACCEPT_IDENTIFIER";
acceptAction.title = @"Accept";
// yume
// you will have on the order of seconds, not minutes, to run
// Given seconds, not minutes, to run in the background
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
// If YES requires passcode, but does not unlock the device
acceptAction.authenticationRequired = NO;
Category | Actions |
---|---|
Invite | Accept, Maybe, Decline |
New mail | Mark as Read, Trash |
Tagged | Like, Comment, Untag |
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
// yume
// You'll include this identifier in your push payload and local notification
inviteCategory.identifier = @"INVITE_CATEGORY";
[inviteCategory setActions:@[acceptAction, maybeAction, declineAction]
forContext:UIUserNotificationActionContextDefault];
// yume
// 2 action context
// Default action context supports up to four actions (show first 2,只會顯示前2個)
// minimal action context is used when there's only room for two actions
NSSet *categories = [NSSet setWithObjects:inviteCategory, alarmCategory, ...
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
// yume
// You need to include the category identifier in your push payload
// push payload has now been increased to 2KB
{
"aps" : {
"alert" : "You’re invited!",
"category" : "INVITE_CATEGORY",
}
}
UILocalNotification *notification = [[UILocalNotification alloc] init]; ...
notification.category = @"INVITE_CATEGORY";
[[UIApplication sharedApplication] scheduleLocalNotification:notification]
...
...
define core location region objects and attach them to this notififcaion, so that the notification will fire when the user happens to come near, enter or exit a region.
只顯示第一次或always顯示
//Core Location registration
CLLocationManager *locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
// request authorization to track the user’s location
[locMan requestWhenInUseAuthorization];
// Plist Information Property List
NSLocationWhenInUseUsageDescription "Your description"
// Core Location registration callbacks
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
// check status to see if we’re authorized
BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
if (canUseLocationNotifications) {
[self startShowingLocationNotifications];
}
}
- (void)startShowingNotifications {
UILocalNotification *locNotification = [[UILocalNotification alloc]
init];
locNotification.alertBody = @“You have arrived!”;
locNotification.regionTriggersOnce = YES;
locNotification.region = [[CLCircularRegion alloc]
initWithCenter:LOC_COORDINATE
radius:LOC_RADIUS
identifier:LOC_IDENTIFIER];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}