2016-06-20 97 views
0

我已經搜索了2天的答案,仍然沒有得到答案。 需求是我們的服務器用一些數據向我的APP發送一個APNS,並且我應該將這些數據轉換爲userDefaults以供進一步使用。如何處理後臺IOS 8的遠程通知?

我到目前爲止所做的工作就是製作didReceiveRemoteNotification工作。所以這意味着當APP在後臺時,我只能在用戶點擊提醒時完成保存過程。

我想使用didReceiveRemoteNotification:fetchCompletionHandler。 但我真的不明白它是如何工作的。這個代表永遠不會被叫到? 我看了蘋果開發者文檔仍然沒有幫助。請有人給我一個示例代碼。特別是告訴我APNS的內容應該是什麼。 非常感謝

+0

即使您使用了didReceiveRemoteNotification:fetchCompletionHandler,您仍然需要用戶點擊通知才能調用該方法。 –

回答

0

我已經找到了解決辦法,這裏是代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
//push notification 
UIUserNotificationSettings *settings=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]; 
[application registerUserNotificationSettings:settings]; 
[application registerForRemoteNotifications]; 
return YES; 
} 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
//because our sever guy only wants a string, so i send him a string without brackets 
NSString * token = [[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""] stringByReplacingOccurrencesOfString: @">" withString: @""]; 
//because they only want the token to be uploaded after user login, so i save it in a variable. 
VAR_MANAGER.APNSToken = token; 
} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{ 
NSLog(@"userInfo: %@", userInfo); 
if (userInfo != nil) { 
    [self updateNotificationData:userInfo]; 
} 
completionHandler(UIBackgroundFetchResultNoData); 
} 

- (void)updateNotificationData: (NSDictionary *)userInfo { 
NSMutableArray *notificationDataArray = [NSMutableArray arrayWithArray:VAR_MANAGER.notificationDataArray]; 
//the app will add the userInfo into the array silently, but when user clicked on the notification alert, the delegate will run again, so I check if the previous added entry is the same as current one. 
for (int i = 0; i < notificationDataArray.count; i++) { 
    if ([userInfo isEqualToDictionary:notificationDataArray[i]]) return; 
} 
[notificationDataArray addObject:userInfo]; 
VAR_MANAGER.notificationDataArray = notificationDataArray; 
} 

VAR_MANAGER是NSObject的我用來存放所有的全局變量,它採用國際志願者組織,當值的變化,它會在userDefault存儲。

//here is the userInfo i obtained from push notification, remember the content-available = 1, that is the most important part 
{ 
    aps =   { 
     alert = success; 
     badge = 1; 
     "content-available" = 1; 
     sound = default; 
    }; 
    status = 1; 
    "time_stamp" = "1466407030.006493"; 
} 

最後感謝所有答案提供者。

0

當應用程序在後臺,你不能做推送通知任何東西。當用戶點擊它時,你可以做更多的操作。 我也面臨同樣的問題,但我不得不把API考慮到這一點,我們所做的事情

  • 我們創造了一個API
  • 這將調用每一次當你的應用涉及到前臺
  • 然後,它會檢查特定的通知是READ或UNREAD,然後就該響應而言,我更改了標記NSUserDefaults

我們可以有多種方案來處理這種情況。

+0

謝謝Pushkraj,與Ajharudeen的答案和你的,我完成了這個功能。 –

+0

問候!!!歡迎好友!如果它對你有用,你可以提高我的答案;) – Pushkraj