標籤:解鎖 顯示 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(本地通知與遠程通知)