iOS(本地通知與遠程通知)

來源:互聯網
上載者:User

標籤:解鎖   顯示   color   format   end   官方   erro   dev   form   

iOS 推播通知有兩種:本地推送、遠程推送.

本地推送 :  在不需要連網的情況下,由APP發出推送,常用於某一時刻的通知,如鬧鐘。本地通送有局限性在於當APP處於後台或者退出時就無法發出通知。

遠程推送:   APNs和第三方推送,第三方推送最終也需要APNs轉寄,

 

本地推送實現
  註冊通知: 

    

float sysVer = [[UIDevice currentDevice].systemVersion floatValue];    if (sysVer < 10) {        //設定通知類型 彈框、腳標、聲音        UIUserNotificationSettings* setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];        [[UIApplication sharedApplication] registerUserNotificationSettings:setting];    }else{        UNUserNotificationCenter* center =[UNUserNotificationCenter currentNotificationCenter];        [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) {            if (granted) {                NSLog(@"通知註冊成功");            }        }];    }

  

 

  發送通知:
    

      

float sysVer = [[UIDevice currentDevice].systemVersion floatValue];    if(sysVer < 10){        UILocalNotification* local = [[UILocalNotification alloc] init];        //給這些屬性賦值才能讓通知有特定的內容                [email protected]"航海王:One Piece";                //特定的時間讓顯示出來()        NSString* dateStr = @"2016-12-01 16:30:00";        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];        dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm:ss";                local.fireDate=[NSDate dateWithTimeIntervalSinceNow:60];                //滑動解鎖的文字(在推播通知資訊的下面一小行字)                local.alertAction [email protected]" ";                //有聲音給聲音,沒聲音用預設的                [email protected]"UILocalNotificationDefaultSoundName";                //設定表徵圖右上方數字                local.applicationIconBadgeNumber=1;                //使用者資訊                [email protected]{@"name":@"航海王",@"content":@"One Piece",@"time":@"20161201"};                //3:定製一個通知                 [[UIApplication sharedApplication]scheduleLocalNotification:local];            }else{        UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];        content.title = [NSString localizedUserNotificationStringForKey:@"Hello Title" arguments:nil];        content.body = [NSString localizedUserNotificationStringForKey:@"Hello Body" arguments:nil];        content.sound = [UNNotificationSound defaultSound];                //設定通知時間        UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];        UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"notificationIdentifier" content:content trigger:trigger];                UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {                                }];    }

 

遠程推送實現

  伺服器向用戶端發送推送的過程:

                  

 

   每個請求中都有裝置令牌,APNs通過裝置令牌去識別裝置和APP,每次運行APP,都會向伺服器發送裝置令牌,伺服器再使用裝置令牌發送推送,只有當推送設定改變時裝置令牌才會被改變 ,只有APNs 可以解碼裝置令牌.

    

  伺服器與APNS 串連過程:

    

                            

 

頭暈,這個還是看官方的文檔:https://developer.apple.com/library/prerelease/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1

信鴿 使用流程:

1、配置真機調試認證, 推送測試認證 (度娘·······)

2、註冊iOS推送

 float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];    if(sysVer < 8){        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];    }else{#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];        UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert                                                                                     categories:[NSSet setWithObject:categorys]];        [[UIApplication sharedApplication] registerUserNotificationSettings:userSettings];        [[UIApplication sharedApplication] registerForRemoteNotifications];#endif    }

3、註冊裝置資訊(deviceToken)

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {    //註冊裝置NSString * deviceTokenStr = [XGPush registerDevice: deviceToken];        //列印擷取的deviceToken的字串NSLog(@"deviceTokenStr is %@",deviceTokenStr);}

3、註冊信鴿

[XGPush startApp:@“10086” appKey:@"key"];

//設定賬戶
[XGPush setAccount:@"123456"];

//推送統計

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{     [XGPush handleReceiveNotification:userInfo]; }


 

 

 

 

iOS(本地通知與遠程通知)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.