I am working with UILocalNotifications. A problem I am having is that after I restart my app in XCode the data associated with a notification seems to be deleted. Here are the steps I am following.
-start the app in XCode
-receive the notification
-stop the app in XCode
-start the app in XCode
-go to notification center and tap on one of the notifications for my app
-break in application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
At this point if I inspect notification I find that it is nil.
Any ideas what is going wrong? Thanks.
If a local notification is tapped in notification center then app is started using theapplication:didFinishLaunchingWithOptions: method and you can get access to the UILocalNotification object from the options dictionary by using the key UIApplicationLaunchOptionsLocalNotificationKey.
application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification is only called if a notification fires and the app is in the foreground.
Related
I created a application. That receives push notifications from server.
This all works for me.
But the only thing is that i want that my old push notification in notification center will be removed when i receive a new one.
What do i need to do in this function
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo {
This doesn't solve my problem
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
doesn't work
It is for Background(when you are home screen or locked your phone).
Or is it possible to replace your old push notification for new one
If application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo is triggered in my running app, but I don't want to handle it right now, is it possible to leave it posted in the Notification Center so the user can swipe down later and click on it?
No, active apps get the push notification directly and the notification will not be added to the notification center.
You will have to handle it directly or store it your self for later use.
in method
didReceiveLocalNotification:(UILocalNotification *)notification i need to detect if it is invoked because user click on a notification in Notification Center or not to have appropriate actions.
Is there anyway?
using didReceiveLocalNotification:(UILocalNotification *)notification you cannot check if app is opened from local notification push notification or by directly clicking on the app icon.
But,
what you can do is in method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions
write this code
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil) {
// App opened from push notification but app was not in background
}else if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]!=nil){
// App opened from local notification and app was in background
}
and in method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
you can detect if app is opened from local notification when app was in background
and finally one last method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
In this method you can check if app is opened from push notification when app was either running or it was in backround
I guess this will help u in knowing if app is opened from notification centre or from somewhere else
When launching the app from a local notification you can use the application:didFinishLaunchingWithOptions: method of ApplicationDelegate to handle the notification, but this method is not called when you select the notification in the notifications panel and the app is in background.
Is there a way to handle the notification in that case?
I want to show a special viewcontroller when the app becomes active by this cause.
Thanks in advance!
Did you check the below delegate method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
my app receives remote notification from Apple server.
Is there a way to remove a single remote notification from notification center (the drop-down menu available from iOs 5.0+), when the user taps on it?
Thanks!
There is no way to remove a specific notification as of iOS SDK 5.0. The way to remove all the notifications from your app so they don't show in the Notification Center when the user opens the app from one of them, is to set the app badge to 0, like this:
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
EDIT: on iOS 8, SpringBoard seems to be automatically dismissing a notification when you tap on it on the Notification Center to open the app.
Here is a suggestion, though it does have its flaws, and I haven't tried it myself:
Push a silent notification (contentAvailable:true), don't include a "alert" inside the push, place the alert text in a custom property of the push
Handle the incoming push and trigger a local notification, display it immediately
If the user clicks the local notification, use the [UIApplication cancelLocalNotification:] which should remove the notification from the notification center.
When you call the method:
[application cancelAllLocalNotifications];
inside the AppDelegate methods:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
and
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
All Local and Push Notifications will be remove on that for the particular app.