user717452
user717452

Reputation: 111

Open View When Notification Tapped

It should be a simple feature, but I'm stuck with no luck. I just want to be able to tap a notification and have it open a basic ViewController with a label showing the notification. However, nothing is pushed on the view when I tap the notification.

#import "AppDelegate.h"
#import "NotificationsPop.h"
@import Parse;
@import UserNotifications;

@interface AppDelegate () <UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [Parse initializeWithConfiguration:[ParseClientConfiguration configurationWithBlock:^(id<ParseMutableClientConfiguration> configuration) {
        configuration.applicationId = @"";
        configuration.clientKey = @"";
        configuration.server = @"https://parseapi.back4app.com";
    }]];
    
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Notification authorization request succeeded!");
        }
    }];
    
    [application registerForRemoteNotifications];
    
    NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        [self handleRemoteNotificationWithPayload:userInfo];
    }
   
    return YES;
}


- (void)handleRemoteNotificationWithPayload:(NSDictionary *)payload {
    NSString *description = payload[@"description"];
    
    if (description) {
        NotificationsPop *dvController8 = [[NotificationsPop alloc] initWithNibName:@"NotificationsPop" bundle:nil];
        dvController8.descriptionText = description;
        
        UINavigationController *nav = (UINavigationController *)self.window.rootViewController;
        [nav pushViewController:dvController8 animated:YES];
    }
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    PFInstallation *currentInstallation = [PFInstallation currentInstallation];
    [currentInstallation setDeviceTokenFromData:deviceToken];
    [currentInstallation saveInBackground];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Failed to register for remote notifications: %@", error);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // Handle the notification when the app is in the background or not running.
    NSLog(@"Received remote notification while in background: %@", userInfo);
    completionHandler(UIBackgroundFetchResultNewData);
}



@end

The app uses storyboard, but the NotificationsPop view controller has a xib, so there's nothing linked there.

My storyboard looks like this below. Any ideas what I'm doing incorrect?

enter image description here

Upvotes: 0

Views: 23

Answers (0)

Related Questions