I want to know when a UINotification has gone off when the user doesn't launch the app via touching on the notification itself but by fast app switching or homescreen icon.
I've checked [[UIApplication sharedApplication] scheduledLocalNotifications] but after the notifications have fired this array is empty.
Once localnotification is fire that will show in notification center ,it will not show it in [[UIApplication sharedApplication] scheduledLocalNotifications].
if your app is in active state that will call didreceivelocalnotification but when your app is not in active state it will show in notification center.
So it will remain in notification center untill you clear it.
if You want to get you can check it in appdelegate method with didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
NSLog(#"lcoal:%#",[localNotif userInfo]);
UIAlertView *al=[[UIAlertView alloc]initWithTitle:#"Challenge gehaald!" message:#"Gefeliciteerd, je hebt deze bonus challenge succesvol afgerond." delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[al show];
}
Related
I'm using Cordova and the plugin phonegap-plugin-push,
when my app is in foreground I don't see the notification alert.
What I have to do?
(I know the way to do it on Android platform (setting "forceShow": true in the method init of PushNotification) but this works only for Android)
Thank you in advance for your replies.
If u don't want to touch ios native things, then you can display your own custmize html/css Banner (like iOS push Baner) in following method :
push.on('notification', function(data) {
If(platform == "iOS" && data.additionalData.foreground){
// Show your Baner here
// U can also define call back on clicking of that banner
//Like navigation to respective page or close that banner
});
Denise,
Notification doesn't work in the foreground. It only works in the background. Do handle it in the foreground you need to implement the logic in the delegate method.
Below is the Objective C code snippet to show the notification in the foreground.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
application.applicationIconBadgeNumber = 0;
//self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.
if (application.applicationState == UIApplicationStateActive)
{
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Did receive a Remote Notification" message:[NSString stringWithFormat:#"Your App name received this notification while it was running:\n%#",[[userInfo objectForKey:#"aps"] objectForKey:#"alert"]]delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
It will help you for your problem.
In iOS if your app is in foreground, push notification alert will not show. To achieve it, you should write code for alert in push notification receive delegate if your application is in foreground. To know application state in iOS this is the code.
+(BOOL) runningInForeground
{
UIApplicationState state = [UIApplication sharedApplication].applicationState;
return state == UIApplicationStateActive;
}
I am new to iOS development. I want to show an alertview on receiving a push notification while app is inactive.
Apple says that push notification can be presented in form of alert message or they can badge the application icon.
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html
I am using PushSharp to send push notifications. But notifications are seen in notification center only. What should I do to show it on alertview?
I know we can write a code to show alertview like
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"test title" message:#"test message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert show];
}
But this code is executed after I tap notification and app launches. I want to show alertview as soon as device receives a push notification.
One more question, does Apple support any way to execute a code on receiving push notification though app is not launched?
Any ideas?
Not Possible with the UIAlertView . You can go through the UILocalNotification to achieve your task .
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
notifaication = [[UILocalNotification alloc] init];
notifaication.timeZone = [NSTimeZone systemTimeZone];
notifaication.alertBody = // Set the Body
notifaication.alertAction =// #"Show me";
backupAlarm.soundName = UILocalNotificationDefaultSoundName;
}
For More info over_Here
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];'
I added the above code to didfinishLaunchingWithOptions but when a user taps a notification in his notification center and enters my app the notification does not gets cleared.
Edit:
I also tried adding this to my code:
You Also need to increment then decrement the badge in your
application:didReceiveRemoteNotification: method if you are trying
to clear the message from the message centre so that when a user
enters you app from pressing a notification the message centre will
also clear, ie:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
as describes here: iOS application: how to clear notifications? but the notification still won't clear from the notification center
I just Added a Badge number manually to my application and pasted
- (void)applicationDidBecomeActive:(UIApplication *)application
{
application.applicationIconBadgeNumber = 0;
}
To my AppDelegate. For me this works like a charm.
Note that didfinishLaunchingWithOptions and applicationDidBecomeActive are not the same as Mouhammad Lamaa explained. If you paste this to your AppDelegate and tap the notification in notification center it should disapper. If it does not your App maybe creates a new Notification after becoming active?
add this code
- (void)applicationDidBecomeActive:(UIApplication *)application
{
application.applicationIconBadgeNumber = 0;
}
the didfinishlaunchingwithoptions launched at the initial launch of your app. if your app the running in the background, didfinishlaunchingwithoptions will not be launched.
When the user open application from notification action - it launches with
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *remoteNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
//handle remote notification
}
....
}
But when the app was in background it calls
- application:didReceiveRemoteNotification:
Also method [[UIApplication sharedApplication] cancelAllLocalNotifications]; cancel registered LOCAL notifications only. Push notifications can't be canceled - they delivered immediately and executed only once.
I want my app to do specific things when the app is launched by a click on a notification. I want to do these specific things when the app is already running into background BUT ALSO when the app is started FROM SCRATCH (not running into background) by a click on the notification.
When the app is started from background by a click on the notification, I get the notification via:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
=> NO PROBLEM !
When the app is started from scratch by a click on the notification, I would like to get the notification via:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
}
But launchOptions is always nil !! It is nil when the app is started from scratch via a click on the app icon (normal) but also when the app is started from scratch via a click on a notification (not normal).
Anybody knows how to solve this issue ?
Thanks !!!
EDIT 1
Here is how my notifications are created (Joe question):
NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:notifText,latitudeString,longitudeString,nil]
forKeys:[NSArray arrayWithObjects:#"notifText",#"latitude",#"longitude", nil]];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody =msg;
localNotif.alertAction = #"Ok";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
EDIT 2 (ANSWER TO MY QUESTION! :))
Here is the procedure I used to debug my app but...this procedure is wrong!!
I set up my debugger with the "Wait for MyApp.app to launch" option in the "Edit Scheme" menu
I launched my app with XCode a first time (launch from scratch) XCode displays the "Waiting for my MyApp to launch" => I CLICKED ON MY APP ICON to launch the app
The app is launched => I clicked on the home button => the notification is displayed
I clicked on the stop button in XCode to close the app I relaunched it with XCode => XCode displays again the "Waiting for my MyApp to launch" message => I CLICKED ON THE
NOTIFICATION in the status bar to launch the app
=> launchOptions is nil !
launchOptions equal to nil is due to the fact that relaunching the app with XCode (in this case with the "Waiting for my MyApp to launch" option) deletes the notifications even if it is still displayed in the status bar...
To be able to debug check what is the content of launchOptions after a relaunch of the app from scratch by a click on a notification, it seems that the only way is to display this content in a UIAlert as mentioned in answer by Tammo Freese. So, use the following to debug in this specific case:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
return YES;
}
Thanks all for your help !!!!
I could not find an error in the code you shared. Normally this should work both in the simulator, and on the device. Here is an example that worked for me: First generate a new Single View iPhone app (ARC and Storyboards on). Then change two methods in the AppDelegate as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeInterval:10.0 sinceDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Just some text";
localNotif.alertAction = #"OK";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = #{#"test": #YES};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Then start this app, press the home button, then stop the app. If you tap on the local notification which comes in 10 seconds after pressing the home button, you should see something like this, which shows the local notification has been passed to -application:didFinishLaunchingWithOptions::
My advice is: First get the example I posted above to work to make sure nothing has gone awry with your setup, then check what you are doing differently in your code.
Edit
This applies to Edit 2 of the question: Local notifications also seem to work for me when waiting for the app to launch (in the simulator, not on the device). Try this:
Install the sample app described above and launch it with "Wait for MyApp.app to launch" disabled.
Click on the home button, then stop the app via Xcode or via the task bar.
Enable "Wait for MyApp.app to launch".
If you now tap the notification in the notification center, it is shown in the alert view.
Dont know if this is what your looking for, but are you missing 'return YES;'?
Because i use this to perform a segue to a new view from a notification and it works fine
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigation = (UINavigationController *) self.window.rootViewController;
[navigation.visibleViewController performSegueWithIdentifier:#"Ident" sender:nil];
return YES;
}
My conclusion and suggestion if it can hep anyone, refer below
Every time you have new build to test your app, you must test the notification click actions with the notifications generated by latest app. If you keep on testing of click actions with old notifications generated by older build then It will behave unexpectedly (means somehow its able to launch the app but it will not return you any valid info in didFinishLaunchingWithOptions:)
Please forgive me, I am still new when it comes to Apple's Push Notification service, but I did some reading through their documentation and my guess is that there may be something in the creation of your local notification that is causing the problem.
I would double check how you are creating your local notification with Apple's Example as shown here in Listing 2-1 just to rule out that possibility. Since some of the code that is used for creating your local notification isn't displayed to us in this thread, it makes it difficult to assess if the instantiation of your local notification is indeed correct. It could end up being as simple as something being wrong with the fire date or something else in the notification not being set up correctly.
Is it possible to let a remote iOS5 notification show in the notification center even if my app is running, something like "I don't care about the remote notification at the moment even though I am running, let it show in the notification bar just as if I wasn't running"?
You show the UIAlertView to the user, instead of that.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"userInfo :%#",userInfo);
NSString* msg = [userInfo valueForKey:#"aps"];
if (self._VCObj.isViewLoaded && self._VCObj.view.window) {
// viewController is visible don't show.
}
else { // viewController is not visible
[[[UIAlertView alloc]initWithTitle:#"Title" message:msg delegate:self cancelButtonTitle:#"ok" otherButtonTitles: nil] show];
}
}
}
Tutorial
This is not possible. The only workaround I can think of is store notification somehow in app, and then on applicationWillTerminate(or some other method) schedule same notification as a LocalNotification and made it appear after second or two. This however could annoy me as a user if I get a notification right after I close some app :)