iPhone開發基礎學習 在程序里設置Push
iPhone開發基礎學習 在程序里設置Push是本文要介紹的內容,最近做項目有一個需求,要在程序得系統設置里進行push的設置。在網上搜了幾天資料沒找著啥。今天忽然心血來潮跟蹤系統注冊push時得代碼,居然發現有可行得解決方法,思路如下:
1、在iphone得framework里的UIApplication.h中有以下函數:
- @interface UIApplication (UIRemoteNotifications)
- - (void)registerForRemoteNotificationTypes:(UIRemoteNotificationType)types __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- - (void)unregisterForRemoteNotifications __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- // calls -registerForRemoteNotificationTypes with UIRemoteNotificationTypeNone
- // returns the enabled types, also taking into account any systemwide settings; doesn't relate to connectivity
- - (UIRemoteNotificationType)enabledRemoteNotificationTypes __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- @end
2、首先可以用[[UIApplication sharedApplication] enabledRemoteNotificationTypes]獲取到允許得push推送類型。然后再調用registerForRemoteNotificationTypes進行修改。若要關閉程序得push服務,可調用unregisterForRemoteNotifications.
3、補充:以上想法以實現。補充部分代碼。settingsData為tableview的數據源數組
a、獲取系push設置,用于顯示給用戶
- //push設置
- NSMutableArray * pushOptions = [[NSMutableArray alloc] init];
- UIRemoteNotificationType notificationType = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
- NSMutableDictionary * soundNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
- @"聲音", @"name",
- @"0", @"status",
- nil];
- if (notificationType & UIRemoteNotificationTypeSound) {
- [soundNotice setValue:@"1" forKey:@"status"];
- }
- [pushOptions addObject:soundNotice];
- [soundNotice release];
- NSMutableDictionary * alertNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
- @"提醒", @"name",
- @"0", @"status",
- nil];
- if (notificationType & UIRemoteNotificationTypeAlert) {
- [alertNotice setValue:@"1" forKey:@"status"];
- }
- [pushOptions addObject:alertNotice];
- [alertNotice release];
- NSMutableDictionary * badgeNotice = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
- @"標記", @"name",
- @"0", @"status",
- nil];
- if (notificationType & UIRemoteNotificationTypeBadge) {
- [badgeNotice setValue:@"1" forKey:@"status"];
- }
- [pushOptions addObject:badgeNotice];
- [badgeNotice release];
- NSDictionary * pushConfig = [[NSDictionary alloc] initWithObjectsAndKeys:
- @"通知設置", @"groupName",
- pushOptions, @"data",
- nil];
- [self.settingsData addObject:pushConfig];
- [pushOptions release];
- [pushConfig release];
b、獲取用戶設置的數據放入pushdata,然后向系統提交設置
- NSArray * pushData = [[settingsData objectAtIndex:indexPath.section] objectForKey:@"data"];
- NSInteger length = [pushData count];
- UIRemoteNotificationType myType = 0;
- for (NSInteger i =0; i< length; i++) {
- if ([[[pushData objectAtIndex:i] objectForKey:@"status"] intValue] ==1) {
- switch (i) {
- case 0: myTypemyType = myType|UIRemoteNotificationTypeSound; break;
- case 1: myTypemyType = myType|UIRemoteNotificationTypeAlert; break;
- case 2: myTypemyType = myType|UIRemoteNotificationTypeBadge; break;
- default: break;
- }
- }
- }
- if (myType != 0) {
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myType];
- }else {
- [[UIApplication sharedApplication] unregisterForRemoteNotifications];
- }
希望以上思路對有這方面需求得人有幫助。以上方案我暫未用于代碼實現。若有問題。請留言共同商討。
小結:iPhone開發基礎學習 在程序里設置Push的內容介紹完了,希望本文對你有所幫助1更多相關內容請參考編輯推薦。