Recently I am trying to create an alarm clock, and when I use UILocalNotification, the problem occur. It will show a banner when the app is in background, but when the app is active, even thought I have used didReceiveLocalNotification, there is no reaction at all.
Why?
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = application.applicationState;
if (state == UIApplicationStateActive) {
NSLog(#"RingRingRing~~~~~");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Remind" message:notification.alertBody delegate:self cancelButtonTitle:#"Sure" otherButtonTitles:nil, nil];
[alert show];
}
}
Neither Remote or local notifications does not wakeup the application in iOS. it takes a user's action on the notification to launch the app.
Related
Im trying to implement the local notification to my app and I implemented the local notification but the problem is .... I'm not getting the Notification BANNER and SOUND when my app is in the foreground. But it is working good when my app is in background.
How to bring the notification banner and sound in foreground.. Is that possible?
this is my piece of code...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle launching from a notification
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
if (application.applicationState == UIApplicationStateActive ) {
NSLog(#"it entered active push");
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = userInfo[#"aps"][#"alert"][#"body"];
localNotification.alertLaunchImage= userInfo[#"acme1"];
localNotification.fireDate = [NSDate date];
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Remove the badge number
application.applicationIconBadgeNumber = 0;
}
-(void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification *)notification{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Reminder"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
If the app is active you will be notified by application:didReceiveLocalNotification: in app delegate only. There you can display custom banner like view on the top viewcontroller presented on the view hierarchy . Just look at the whatsapp notification when the app is open
If the application is active then you will not recieve any sound, badge or alert, however the application delegate application:didReceiveLocalNotification: will be called
From apple docs
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.
If your app is currently running and active (i.e. visible), you will not see the alert message. Instead iOS will deliver the notification to your app directly through the following method
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
No it will not play a sound or show a banner. However in your apps delegate , did receive notification will still be called where you can then show an alert view if you wish to notify a user.
You can however add audio manually.
- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
SystemSoundID systemSoundID;
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"blip"
withExtension:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &systemSoundID);
AudioServicesPlaySystemSound(systemSoundID);
}
Local notification in Foreground
I'm using Urban airship in my app to receive push notifications and all works as expected.
The problem is that when my app is running in foreground and a push received it shows Notification as title along with info in payload.But i want to show title as Notis instead Notification i tried this-
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if ( application.applicationState == UIApplicationStateActive )
{
NSDictionary* aps = [userInfo valueForKey:#"aps"];
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Notis" message:[aps valueForKey:#"alert"] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex=2;
[self loadStatisticsInBackground];
[[UAPush shared] handleNotification:userInfo applicationState:application.applicationState];
}
[[UAPush shared] resetBadge];
}
But it shows two alerts one that i defined and another is system defined.
Any help would be appreciated.
Edit: Ok.. if it is not possible to change title of push notification then Is there any way that we can prevent push alert when app is running in foreground?I don't want to show when my app is running in foreground?
You can just detect if your app is running in foreground and if it does, you don't pass the notification to UAPush in -application:didReceiveRemoteNotification:.
Is there a way to present a alert like the alert presented by twitter SDK if no account is signed-in.
I want to display that type of alert if user has disabled Settings->Notification Center for my app.
Just want to present the alert, mentioned twitter as ref. Its not about twitter.
NOTE: I don't want to display / use social media, its for reference, question is can we display a custom alert which can navigate the user to settings app of iOS.
There is no way to know the status of the switch in Notification Center > Your app. The only thing you can access is what type of notifications he will get ([[UIApplication sharedApplication] enabledRemoteNotificationTypes];).
You can know notification setting of your app and can show alert when it is disable as below
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (!(types & UIRemoteNotificationTypeAlert)) {
UIAlertView *al = [[UIAlertView alloc] initWithTitle:#"TITLE" message:#"YOUR MESSAGE" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"Settings",nil];
[al show];
[al release];
}
For more reference, you may check Determine on iPhone if user has enabled push notifications
The only settings you can access are your app's settings, which can be within your app or in Settings app. Your app's settings are the ones you set/get with [NSUserDefaults standardUserDefaults]. However for your case you could schedule a notification which fires after one second, and see if you get called from - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification. Here is an example code.
-(void) createLocalNofication{
UILocalNotification *local = [[UILocalNotification alloc] init];
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0f];
local.alertBody = #"Some Alert Body";
local.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:local];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
//Here you can set some flag
}
To gain more insight on the topic, you can check out this apple documentation link.
I've searched other questions similar to this here and I can't find any that seem to actually work. What I'm trying to do is when the user opens the app from a local notification, I need it to execute some code (such as opening the UIMessageComposer or displaying a UIAlertView). Anyone have any idea on how I would do this? Just as a note it is a local notification not a push notification.
You need to implement this method in you AppDelegate.m file
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
You can do want you want in this.
Here's a good tutorial on how to work with Local notifications. http://www.appcoda.com/ios-programming-local-notification-tutorial/
use this
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Reminder"
message:notification.alertBody
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
I implemented a local notification in my game app that fires up once a day for a daily bonus. It all works fine when I tap the notification banner, however, when i get in to the app from the app icon, the local notification doesn't work like it should.
Here is my code:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{
.......
.......
application.applicationIconBadgeNumber = 0;
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif)
{
NSLog(#"recieved notification %#",localNotif);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Daily Bonus"
message:#"You recieved 100 free coins"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"ok", nil];
[alert show];
[alert release];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
float balance = [standardUserDefaults floatForKey:kCurrentScore];
balance +=100.0f;
NSLog(#"%g",balance);
[standardUserDefaults setFloat:balance forKey:kCurrentScore];
[standardUserDefaults synchronize];
}
I would appreciate any help.
That's the way it works.Starting the app from it's icon will not trigger any notifications to your app, only from the banner.You should probably use the same logic as the one triggering your notification if you want to reward your users even though they didn't tap the banner - just calculate how long has it been since the app was last ran and do your stuff there.