Support

Can I send a user to a particular part of my app via a Layer push notification?

You can easily navigate someone to a particular part of your app (similar to a deep link) by adding code inside didReceiveRemoteNotification.  Here's some example code:

 


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    // Get Message from Metadata
    __block LYRMessage *message = [self messageFromRemoteNotification:userInfo];
    
    if (application.applicationState == UIApplicationStateInactive && message) {
         //Navigate user to right part of the app here
    }

    NSError *error;
     BOOL success = [self.layerClient synchronizeWithRemoteNotification:userInfo completion:^(NSArray *changes, NSError *error) {
        if (changes)
        {
            if ([changes count])
            {
                // Try navigating once the synchronization completed
                if (application.applicationState == UIApplicationStateInactive && !message) {
                    message = [self messageFromRemoteNotification:userInfo];
                    //Navigate user to right part of the app here
                }
completionHandler(UIBackgroundFetchResultNewData);  } else { completionHandler(UIBackgroundFetchResultNoData); } } else { completionHandler(UIBackgroundFetchResultFailed); } }]; if (success) { NSLog(@"Application did complete remote notification sync"); } else { NSLog(@"Failed processing push notification with error: %@", error); completionHandler(UIBackgroundFetchResultNoData); } } - (LYRMessage *)messageFromRemoteNotification:(NSDictionary *)remoteNotification { static NSString *const LQSPushMessageIdentifierKeyPath = @"layer.message_identifier"; // Retrieve message URL from Push Notification NSURL *messageURL = [NSURL URLWithString:[remoteNotification valueForKeyPath:LQSPushMessageIdentifierKeyPath]]; // Retrieve LYRMessage from Message URL LYRQuery *query = [LYRQuery queryWithClass:[LYRMessage class]]; query.predicate = [LYRPredicate predicateWithProperty:@"identifier" operator:LYRPredicateOperatorIsIn value:[NSSet setWithObject:messageURL]]; NSError *error; NSOrderedSet *messages = [self.layerClient executeQuery:query error:&error]; if (!error) { NSLog(@"Query contains %lu messages", (unsigned long)messages.count); LYRMessage *message= messages.firstObject; LYRMessagePart *messagePart = message.parts[0]; NSLog(@"Pushed Message Contents: %@",[[NSString alloc] initWithData:messagePart.data encoding:NSUTF8StringEncoding]); } else { NSLog(@"Query failed with error %@", error); } return [messages firstObject]; }
Have more questions? Submit a request