Local Notification - ios

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.

Related

iOS how to change push notification title when app is running 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:.

Action From Local Notification Opened (iOS)

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;
}

didReceiveLocalNotification not active in iOS6

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.

Ios Alert on first launch to set up notification

I have created a photography assignment generator app using dreamweaver/phonegap with some finishing touches in xcode.
I have set up a settings bundle where the user can set a daily reminder on on off. it is preset to OFF, as i would rather not annoy people who don't want it.
Because i have done this using dreamweaver, I can't find a way to access the settings bundle, so the user has to go to settings, flick the switch, and reboot the app to have it take effect.
What I would like to do is have the app ask them the first time the app is launched whether or not they would like to set up a daily reminder. If they tap yes, it should set the reminder setting to ON/YES, if no, it should continue on with the default set to no.
it would be even more awesome if I could have a "Maybe Later" button.
I am not great at programming, and it was a lot of work for me to get this working(thanks to help from the great folks on here and other sites on the net)
I have tried using various IF/THEN, but I can't get it to work.
So here is what I have so far...would appreciate it greatly if any of you would be able to help me figure this out.
Thank you
Noel Chenier
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOtions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults =[NSDictionary dictionaryWithObject:#"NO" forKey:#"enableNotifications"];
[defaults registerDefaults:appDefaults];
[defaults synchronize];
UILocalNotification *localNotif= [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(#"Recieved Notification %#",localNotif);
}
/*NSArray *keyArray = [launchOptions allKeys];
if ([launchOptions objectForKey:[keyArray objectAtIndex:0]]!=[nil)
{
NSURL *url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
self.invokeString = [url absoluteString];
}*/
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
This is a pretty simple task, especially considering you're already using NSUserDefaults. All you need to do is store a BOOL in your defaults every time the app launches. For example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOtions:(NSDictionary *)launchOptions {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:#"firstLaunch"]) {
//this key has never been set - this is the first launch
[defaults setBool:YES forKey:#"firstLaunch"];
//show your alert here that you only want to show on the
//first application launch
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Some Title"
message:#"Some message" delegate:self cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Some Button", #"Another Button", #"One More Button",
nil];
[alert show];
}
}

Apns-alert customization

I have successfully used APNS in iphone app and still have a problem the alert customization.Below is my question:
1 I can't custom the Alert view,like title and button title.I custom the alert like:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *apsDic = [userInfo valueForKey:#"aps"];
NSString *alertStr = [apsDic valueForKey:#"alert"];
NSNumber *badgeNum = [apsDic valueForKey:#"badge"];
NSString *soundStr = [apsDic valueForKey:#"sound"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[arr objectAtIndex:1]
message:msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:#"view",nil];
[alert show];
[alert release];
}
In my App, the title of the alert is my app's title;and the button titles are "Close" and "View".
2 when I click the "View", is shows the launch view of my app and then it crashes.Why?
So if the alert is provided by the system which can't be customized, the view action is also under control of system. It seems there's contradiction between 1 and 2.
Any help is appreciated!
thanks
I find: if your app does not start, the apns-alert is provided by the iOS which you can't customize.

Resources