3

I am adding push notifications with UrbanAirship. I have these two methods in my AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Create Airship options dictionary and add the required UIApplication launchOptions
    NSMutableDictionary *takeOffOptions = [NSMutableDictionary dictionary];
    [takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];

    // Call takeOff (which creates the UAirship singleton), passing in the launch options so the
    // library can properly record when the app is launched from a push notification. This call is
    // required.
    //
    // Populate AirshipConfig.plist with your app's info from https://go.urbanairship.com
    [UAirship takeOff:takeOffOptions];

    // Set the icon badge to zero on startup (optional)
    [[UAPush shared] resetBadge];

    // Register for remote notfications with the UA Library. This call is required.
    [[UAPush shared] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                         UIRemoteNotificationTypeSound |
                                                         UIRemoteNotificationTypeAlert)];

    // Handle any incoming incoming push notifications.
    // This will invoke `handleBackgroundNotification` on your UAPushNotificationDelegate.
    [[UAPush shared] handleNotification:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
                       applicationState:application.applicationState];

    // Override point for customization after application launch.
    return YES;
}

and this method:

// Implement the iOS device token registration callback
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    UALOG(@"APN device token: %@", deviceToken);

    // Updates the device token and registers the token with UA. This won't occur until
    // push is enabled if the outlined process is followed.
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *alias = [defaults objectForKey:@"user_id"];
    [[UAPush shared] setAlias:@"test-alias"];
    [[UAPush shared] registerDeviceToken:deviceToken];
}

I got them from the UrbanAirship instructions here: https://docs.urbanairship.com/display/DOCS/Getting+Started:+iOS:+Push

But I am confused how I can specify the screen which I want the push notification to land on. Where is that done? And also, my server sends some JSON along with the push. Where and how do I extract the data from that JSON?

Thanks!

Eran
  • 387,369
  • 54
  • 702
  • 768
Genadinik
  • 18,153
  • 63
  • 185
  • 284

3 Answers3

1

You are passing the JSON to the handleNotification method. Is that a method you wrote, or is it part of the urbanairship code? (I'm not sure whether they supply client side code in addition to the server code). If it's a method you wrote, you can access the data from the JSON in that method. If not, you can write your own method and pass it the same data.

You can access the notification JSON this way only if the application was not running when the notification arrived, and the user tapped the notification to open the app. If the user taps the app launch icon, that data will be lost.

You can use a custom property in the JSON to specify the landing screen. It would be your responsibility to parse that property and determine which view to display.

You should also implement application:didReceiveRemoteNotification: in order to handle the notification data if the notification arrives when the application is running in the foreground.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

You have to handle remote push notifications in your application delegate:

- (void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo

You can access your root view controller from here and trigger the display of other view controllers.

From the docs:

If the app is running and receives a remote notification, the app calls this method to process the notification. Your implementation of this method should use the notification to take an appropriate course of action. For example, you could use it as a signal to connect to a server and download the data waiting that is waiting for the app

The userInfo dictionary contains your additional data sent over urban airship.

You can find more information in the Reference

  • thanks, just to clarify, so if I want to send the user to a particular screen, I have to first send them to The ViewController and then to that screen from the ViewController? (My ViewController is my root view controller). – Genadinik Mar 27 '13 at 18:07
  • The urban airship SDK doesn't know about the structure of your ios application, it is not able to show a particular screen. Yes, you can forward the call to your root view controller and handle the notification in there. In our app, we have a tabbed view controller as the root view controller and have special methods (e.g. something like 'showMapTab') that we call from the app delegate. – Christoph Wimberger Mar 27 '13 at 19:52
1

The short answer is: you don't (because that functionality is not provided for you.)

If a push is received while your app is running, -[UIApplicationDelegate application:didReceiveRemoteNotification:] will be called. In your implementation of this method, you are responsible for taking appropriate actions (e.g. showing a message, navigating to a particular area of your application.)


From the documentation:

If the app is running and receives a remote notification, the app calls this method to process the notification. Your implementation of this method should use the notification to take an appropriate course of action. For example, you could use it as a signal to connect to a server and download the data waiting that is waiting for the app.

The userInfo dictionary contains the aps key whose value is another dictionary. Although you should not need the information in the aps dictionary, you can retrieve its contents using the following keys:

The userInfo dictionary may also have custom data defined by the provider according to the JSON schema. The properties for custom data should be specified at the same level as the aps dictionary. However, custom-defined properties should not be used for mass data transport because there is a strict size limit per notification (256 bytes) and delivery is not guaranteed.

Nick Dowell
  • 2,030
  • 17
  • 17