您好,登錄后才能下訂單哦!
目前分為四個推送:用戶推送,本地推送,遠程推送,地理位置推送。
我們先開始講這個用戶推送,我們要使用之前必須先注冊這個推送,用戶要允許這個程序進行推送
注冊過程:
[objc] view plaincopy
if (IS_IOS8) {
//1.創建消息上面要添加的動作(按鈕的形式顯示出來)
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = @"action";//按鈕的標示
action.title=@"Accept";//按鈕的標題
action.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啟動程序
// action.authenticationRequired = YES;
// action.destructive = YES;
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
action2.identifier = @"action2";
action2.title=@"Reject";
action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啟動程序,在后臺處理
action.authenticationRequired = YES;//需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略;
action.destructive = YES;
//2.創建動作(按鈕)的類別集合
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";//這組動作的唯一標示,推送通知的時候也是根據這個來區分
[categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
//3.創建UIUserNotificationSettings,并設置消息的顯示類類型
UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
[application registerUserNotificationSettings:notiSettings];
}else{
[application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
}
[objc] view plaincopy
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
// UIUserNotificationSettings *settings = [application currentUserNotificationSettings];
// UIUserNotificationType types = [settings types];
// //只有5跟7的時候包含了 UIUserNotificationTypeBadge
// if (types == 5 || types == 7) {
// application.applicationIconBadgeNumber = 0;
// }
//注冊遠程通知
[application registerForRemoteNotifications];
}
我們現在僅僅是注冊了通知的設置,還要注冊推送通知的行為,在iOS8中,行為能直接在推送消息進行,如回復消息,拒絕消息等總結就是三個方法進行注冊
我們如何能進行這些行為,首先我們需注冊這些行為。
Actions
[objc] view plaincopy
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"RickAction";
acceptAction.title = @"Accept";
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO;
UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
inviteCategory.identifier = @"INVITE_CATEGORY";
[inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
//Location Notification
CLLocationManager *locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
[locMan requestWhenInUseAuthorization];
#pragma mark - CLLocationManager
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse);
if (canUseLocationNotifications) {
[self startShowLocationNotification];
}
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
CLRegion *region = notification.region;
if (region) {
}
}
- (void)startShowLocationNotification
{
CLLocationCoordinate2D local2D ;
local2D.latitude = 123.0;
local2D.longitude = 223.0;
UILocalNotification *locNotification = [[UILocalNotification alloc] init];
locNotification.alertBody = @"你接收到了";
locNotification.regionTriggersOnce = YES;
locNotification.region = [[CLCircularRegion alloc] initWithCenter:local2D radius:45 identifier:@"local-identity"];
[[UIApplication sharedApplication] scheduleLocalNotification:locNotification];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
{
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
{
if ([identifier isEqualToString:@"RickAction"]) {
[self handleAcceptActionWithNotification:notification];
}
completionHandler();
}
- (void)handleAcceptActionWithNotification:(UILocalNotification*)notification
{
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token=[NSString stringWithFormat:@"%@",deviceToken];
token=[token stringByReplacingOccurrencesOfString:@"<" withString:@""];
token=[token stringByReplacingOccurrencesOfString:@">" withString:@""];
token=[token stringByReplacingOccurrencesOfString:@" " withString:@""];
}
<span style="font-family: Helvetica, Arial, Geneva, sans-serif;">[[UIApplication sharedApplication] registerForRemoteNotifications];</span>
{
“aps”: {
"content-available": 1,
"alert": "This is the alert text",
"badge": 1,
"sound": "default"
}
}
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil nil];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert ;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。