Inside the method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
How do I extract the alert value from the dictionary UserInfo? I believe the dictionary might look something like
{
"_" = "l0-mgGElEeKbHAAbIbyL6A";
aps = {
alert = "Manual: IOM update 2013-01-17T20:14:42-08:00";
badge = 0;
};
operation = update;
tab = manuals;
}
Is this really always the case? Or does Urban Airship provide a method for doing this? which would really be ideal.
UPDATE
Based on feedback by #Spynet
It seems that by alert what I really mean is the actual text message, which in the case where the alert itself is a dictionary may be called the body of the alert. All the same, does Urban Airship provide a method for extracting such information? Or must I roll out my own? If my own, has anyone done this successfully? Thanks.
Actually urban airship does take care of it for you. Simple implement the protocol UAPushNotificationDelegate and method
- (void)displayNotificationAlert:(NSString *)alertMessage;
Related
I have several notifications on my app. What i want to do is to, when a local notification arrived, it erases another previous already showed one from notification center.
I am using the the following code.
static func unscheduleNotification(userInfoValue: String){
if let notifications = UIApplication.sharedApplication().scheduledLocalNotifications{
for notification in notifications{
if let userInfo = notification.userInfo as? [String:String]{
if userInfo["info"] == userInfoValue{
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
}
Would appreciate if someone point me out the right direction here. If that is even possible.
I found the solution. Or one solution. Adding one repetition interval to the notification allows with the method i was using and posted on the question to delete it.
I don´t know if it is the correct procedure or not but it is indeed working.
If you want to receive 1 notification when getting another 1, then best idea to keep you 1st notification object in NSUserDefault, when getting another 1 notification, fetch already stored notification from NSUserDefault then remove it.
Consider example if your device is restarted after receiving 1st notification. then it will be in notification tray but you would not get object to cancel it.
Also scheduledLocalNotifications will not provide object after restating your device. scheduledLocalNotifications doesn't work properly always with iOS 8.
Refer - iOS 8 [UIApplication sharedApplication].scheduledLocalNotifications empty
While storing in NSUserDefault you have to convert notification object to NSData
let data = NSKeyedArchiver.archivedDataWithRootObject(notificationObject)
At the time of getting from NSUserDefault it will be NSData
So again convert to object
let notificationObject : UILocalNotification = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? UILocalNotification
I'd like to be able to send the viewer to a specific view in the app when they get the push notification, based upon what I send them.
"aps": {
"alert": "look at this stuff",
"view": "wc1"
}
the view 'wc1' is just a tag in a collection view. So what i'd really like to know is, if the user is deep in my app, and they receive a push notification, how do I send them back to the collection view screen.
I've come across the term deep linking, but not been able to find anything on it thus far. Any direction would be really helpful. Thanks!
Try to move your dictionary entry for view outside of the "aps" dictionary.
{
"aps":
{
"alert": "look at this stuff"
}
"view": "wc1"
}
When application is loaded you can detect that in your appdelegate class in that method:
didFinishLaunchingWithOptions
NSDictionary *pushNotification = [options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotification )
{
//Handle remote notification
}
If application is still running in background and notification came, you can detect that also in your AppDelegate class:
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
How can I check if my app has a badge number. When I send a push notification the users, it add a "1" as the badge number to the icon. I would like to check if there is a badge number when the user launches the app and direct them a view controller.
I'd propably make use of the property applicationIconBadgeNumber of the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions's application.
So, to be clear:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
application.applicationIconBadgeNumber = 1; /*some number...*/
//or to read the number
int appIconBadge = application.applicationIconBadgeNumber;
}
Hope that helps.
I think you'll need to keep track of it yourself in the app. Your delegate’s application:didFinishLaunchingWithOptions: will be called upon the notification and the user presses the action button and it receives the notification payload.
If you app is running in the foreground, the delegates application:didReceiveRemoteNotification: will be called. In this case you could have an integer value and increment it to keep track of the number of notifications you have. It also receives the notification payload.
Alternately, without knowing anything about your design, you could have a web service that the app could query to determine whatever number of items you are looking for, but this seems much more difficult, and depending on the design you are working with might be unworkable.
Reference:
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
I want to implement this function on my apps but i cant seem to figure out how to use this line of codes.
- (void)applicationWillResignActive:(UIApplication *)application {
//our app is going to loose focus since there is an incoming call
[self pauseGame];
}
- (void)applicationDidBecomeActive:(UIApplication *)application{
//the user declined the call and is returning to our app
[self resumeGame];
}
I've read that this must be placed in appdelegates but i cant seem to figure out how could i call my pause action when the game is currently in the viewcontroller.
Thank you.
Instead of sending the messages to self (which is the app delegate), you would send them to your view controller.
For example, if your app delegate had a property for your main game view controller named "gameViewController" (where the methods to pause and resume were implemented):
- (void)applicationWillResignActive:(UIApplication *)application {
// our app is going to loose focus since there is an incoming call
[self.gameViewController pauseGame];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// the user declined the call and is returning to our app
[self.gameViewController resumeGame];
}
I know that this was answered a long time ago. But I wanted to add that another (more scalable) solution is to have interested parties (e.g., UIViewControllers) register an interest in UIApplicationDidEnterBackgroundNotification and UIApplicationWillEnterForegroundNotification.
This approach has the advantage that the Application Delegate doesn't need to have direct knowledge of the objects that might need to respond to entering background/foreground.
What's the code to subscribe to an event like applicationWillResignActive in any place in your iphone application?
[UPDATE]
Let me rephrase my question. I don't want to respond to this in my application delegate, but rather listen to this event from another class. Is that possible or I need to pass the event from the application delegate to the concerning class?
Looks like you are looking for this code.
- (void) applicationWillResign {
NSLog(#"About to lose focus");
}
- (void) myMethod {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationWillResign)
name:UIApplicationWillResignActiveNotification
object:NULL];
}
Take a look at the documentation for the method you're talking about:
applicationWillResignActive:
Tells the delegate that the application will become inactive. This method is optional.
- (void)applicationWillResignActive:(UIApplication *)application
[...]
Discussion
[...]
Just before it becomes inactive, the application also posts a UIApplicationWillResignActiveNotification.
Implement the method below in your application delegate:
-(void)applicationWillResignActive:(UIApplication *)application
This allows you to react when the application becomes inactive - when this is the case, it is executing but not dispatching incoming events. This happens, for example, when an overlay window pops up or when the device is locked.
Just before it becomes inactive, the application also posts a UIApplicationWillResignActiveNotification.
Your topic and question are asking slightly different things.
Your application will receive applicationWillResignActive, along with applicationWillTerminate, automatically. No subscription is necessary, just implement the function in your app.
As to how to respond, this is down to the application. While you can choose to do nothing the recommended behavior is that you cease or slow any non-critical functionality. E.g. if you were a game you would stop updating the display and/or pause the game.