User taps on UILocalNotification: Can it transfer data to the application?

I create a local UILocalNotification and display it to the user as a banner. Is it possible to configure it so that when the user deletes it and returns to the application, the application will receive some data on a certain type of notification? I want to open a specific view controller in an application. I think the best way is to essentially send the URL to the application, or is there a way to access the UILocalNotification so that I can check what type was and take the right action?

+4
source share
3 answers

To get data from the local NSUserNotification passed to the iOS application, all you have to do is implement the following method: - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification .

The problem is that this is called when a local notification is sent when the application is in the background (i.e. when the user enters the notification and then returns to the application), and also if the application is in the foreground at a time when local notification is triggered (after all, this is a timer). So, how do you determine if a notification was sent when the application was in the background or foreground? It is pretty simple. Here is my code:

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { UIApplicationState state = [application applicationState]; if (state == UIApplicationStateInactive) { // Application was in the background when notification was delivered. } else { // App was running in the foreground. Perhaps // show a UIAlertView to ask them what they want to do? } } 

At this point, you can process a notification based on notification.userInfo that contains NSDictionary.

+6
source

ust implement NSUserNotificationCenterDelegate and define this method:

 - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification 

Example:

This is what I did in the notifier application.

 - (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification { NSRunAlertPanel([notification title], [notification informativeText], @"Ok", nil, nil); } - (void) userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification { notifications=nil; [tableView reloadData]; [center removeDeliveredNotification: notification]; } 

When the notification is activated (click the user), I just tell the user the panel (I could use the hud window). In this case, I immediately delete the delivered notification, but this is not what usually happens. the notification may remain there for a while and be deleted after half an hour (it depends on the application you are developing).

+1
source

1 - Define a class in your project to implement the NSUserNoficationCenterDelegate protocol (registered here )

 @interface someObject : NSObject <NSUserNotificationCenterDelegate> { - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification; } 

2 - Install the instance of the object defined by # 1 as the default notification center delegate.

 [[NSUserNotificationCenter defaultNotificationCenter] setDelegate: someObject]; 

Now you will be called on didActivateNotification each time the user clicks / clicks on the notification. You will have the original notice that you created. Therefore, any information that you need should be available to you.

If you need / need special information (except for the title of notifications, messages, etc.), you will probably need to specify additional information for a specific application in your notification before planning to send it. For instance:

 NSUserNotification* notification = [[NSUserNotification alloc] init]; NSDictionary* specialInformation = [NSDictionary dictionaryWithObjectsAndKeys: @"specialValue", @"specialKey", nil]; [notification setUserInfo:specialInformation]; 
0
source

Source: https://habr.com/ru/post/1447981/


All Articles