我試圖根據notification
點擊打開不同的視圖控制器,但當收到push notification
時,它會自動在後臺運行控制器代碼,所以當我通過應用程序圖標/通知打開應用程序時notification centre
它立即加載view controller
,但是當收到多個通知時,它會加載第一個通知控制器,而不管通知哪個被點擊。如何處理IOS中的多個遠程通知單擊
比方說,我已經得到了與標題「晚報」,「晨報」和 「夜」的通知,應該打開「晚報 - 視圖 - 控制器」時,「晚報」 通知被竊聽,但它加載「夜景控制器「當我去 回加載」早晨視圖控制器「,最後它加載」晚上 視圖控制器「。在發出通知之前,我在主控制器 上,然後我將應用程序移到了後臺。
當我點擊「早晨」notification
它現在不做任何事情。
早些時候,我嘗試使用addObserver,但結果是相同的,所以我轉移到應用程序委託。
這裏是應用程序委託代碼
@interface AppDelegate()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication]setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
// Override point for customization after application launch.
// Register for Push Notitications, if running iOS 8 or More
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
// Register for Push Notifications before iOS 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}
[application setStatusBarHidden:YES];
return YES;
}
// Handle remote notification registration.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"Device Token %@",[self stringWithDeviceToken:deviceToken]);
[[NSUserDefaults standardUserDefaults]setObject:[self stringWithDeviceToken:deviceToken] forKey:@"registration_id"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSLog(@"Error in registration. Error: %@", err);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSDictionary *alertInfo = (NSDictionary *) userInfo[@"aps"][@"alert"];
NSLog(@"%@",alertInfo);
NSString *alertID = [userInfo valueForKey:@"alertid"];
if (application.applicationState == UIApplicationStateBackground) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
NSLog(@"Background Mode");
NSString *title = alertInfo[@"title"];
[self openViewController:title];
}
if(application.applicationState == UIApplicationStateActive){
NSLog(@"Active Mode");
}
completionHandler(UIBackgroundFetchResultNewData);
[self performSelector:@selector(sendAckRequest:) withObject:alertID];
}
- (void)openViewController:(NSString *)notificationTitle{
NSDictionary * userDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginUser"];
if([notificationTitle isEqualToString:@"Exercise"]){
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
Excercise *excerciseController = (Excercise*)[mainStoryboard instantiateViewControllerWithIdentifier: @"Excercise"];
[navigationController pushViewController:excerciseController animated:YES];
//[navigationController presentViewController:excerciseController animated:YES completion:nil];
}else if([notificationTitle isEqualToString:@"Weight"]){
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
Weight *weightController = (Weight*)[mainStoryboard instantiateViewControllerWithIdentifier: @"Weight"];
[navigationController pushViewController:weightController animated:YES];
//[navigationController presentViewController:weightController animated:YES completion:nil];
}else if([notificationTitle isEqualToString:@"MCQ"]){
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
QuestionOfTheDay *questionController = (QuestionOfTheDay*)[mainStoryboard instantiateViewControllerWithIdentifier: @"QuestionOfTheDay"];
questionController.self.dictUser = userDict;
[navigationController pushViewController:questionController animated:YES]
}
}
-(void)sendAckRequest:(NSString *)alertID {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userID =[[defaults objectForKey:@"loginUser"]objectForKey:@"UserId"];
NSString *serverRegistrationID = [defaults objectForKey:@"server_registration_id"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSString *currentDateString = [dateFormatter stringFromDate:[NSDate date]];
//NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
//NSDictionary *parameter = @{@"ReminderDateTime":currentDateString};
NSString *url = [NSString stringWithFormat:@"%@%@/%@/%@/?ReminderDateTime=%@",sendNotificationAck,userID,serverRegistrationID,alertID,currentDateString];
NSLog(@"url: %@",url);
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
if(operation.response.statusCode == 200)
NSLog(@"Notification Acknowledged");
else
NSLog(@"Notification failed to acknowledge");
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
NSLog(@"error: %@",[error localizedDescription]);
}];
}
- (NSString *)stringWithDeviceToken:(NSData *)deviceToken {
const char *data = [deviceToken bytes];
NSMutableString *token = [NSMutableString string];
for (int i = 0; i < [deviceToken length]; i++) {
[token appendFormat:@"%02.2hhX", data[i]];
}
return token;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
if([UIApplication sharedApplication].applicationIconBadgeNumber!=0)
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber - 1;
}
@end
您可以使用布爾值來處理它。 – Santo