UILocalNotification issue in IOS - ios

I have used UILocalNotification in my code after getting the response from server. I'm sending multiple images to the server , when the response comes 1 it should show a local notification in an app.
I have tried some code it shows a notification in banner style but not one the app screen , when i press command+shift+H it goes to home screen of simulator and shows the notification there instead of in app screen.
How we can show same notification on the home screen of app. I have tested this on real device but its not showing there.
My Code is:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = res;
localNotification.alertBody = #"Image Sent";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
It shows like this,

You need to handle it by yourself. When local notification comes, you can use UIAlertController to show the payload dictionary data associated with that local notification. Inside below method, you can do it. In notification settings, change notification style to alert from banner if needed.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

This method is called when notification fire in foreground for iOS 10:
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(#"User Info : %#",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
here you can handle the local notification and show the alert UI using TSMessage class https://github.com/KrauseFx/TSMessages

Local notifications and remote notifications are ways to inform users when new data becomes available for your app, even when your app is not running in the foreground. It is your responsibility to inform user when notification is arrived.
Use OTNotification for display Notification like push notifications' cube animation With covering statusBar While your app is in foreground.
You can Use Like this.
OTNotificationManager *notificationManager = [OTNotificationManager defaultManager];
OTNotificationMessage *notificationMessage = [[OTNotificationMessage alloc] init];
notificationMessage.title = [self notificationTitle];
notificationMessage.message = #"A notification. Touch me to hide me.";
[notificationManager postNotificationMessage:notificationMessage];

Related

iOS Push Notifications don't adds to Notification Center

I realizing push notification handling on my iOS application on Xamarin.iOS platform but my problem actual for iOS native too.
I need to handle push notification when my application is in Background Mode for some reasons, so I turn on Background Mode and Push Notification using in my project. Also I include the content-available key with a value of 1 into the payload’s aps dictionary. Also I include alert, badge and sound keys because I want to show this push notification for user and add it into Notification Center.
As a result after push notification receiving in Background Mode (when application is not active):
1) I handle push notification receiving using DidReceiveRemoteNotification() method.
2) The user see notification rolls down from the top of the screen as a banner.
3) I change application icon badge counter.
My problem is that push notification don't adds to Notification Center after all this actions.
As I understand after handling the push notification in DidReceiveRemoteNotification() method iOS mark this notification like handled and doesn't add them to Notification Center. As possible solution I can create Local Notification, schedule them and it'll added to Notification Center but the user will again see notification rolls down from the top of the screen as a banner and that's not good (it looks like user gets 2 notifications but only 1 shows in Notification Center).
What's the actual reason for this behaviour and how can I solve this problem?
You can try VoIP Push notification Service.
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type
NSDictionary *payloadInfo = payload.dictionaryPayload;
[self showLocalNotificationWithInfo: payloadInfo];
}
- (void) showLocalNotificationWithInfo:(NSDictionary *)infoDict{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = #"Your title message";
localNotification.soundName = #"YourSoundFileName.mp3";
localNotification.userInfo = userInfo;
localNotification.fireDate = [NSDate date];
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Apple VoIP Push Service
Thank you guys for your answers. I found a reason of this behavior - the problem was with badge key in push notification body. This key had 0 value and thats why iOS marked new notifications as readed and they didn't add to Notification Center.

Why is a banner not shown for my local notification in iOS 7

My iOS 7 app is generating local notifications in a method called within an NSOperationQueue block. The notifications are appearing in the Notification Center, but they are not showing a banner at the top of the screen. The notifications are being generated while the app is in the background.
I've tried everything I can think of, and done considerable Google searching, but I still can't get the banners to display.
Here is the code that builds and schedules the notification:
// In the most recent case, I have verified that
// alertText = Why not work? and alertAction = View
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = alertText;
localNotification.alertAction = alertAction;
localNotification.alertLaunchImage = launchImage;
UIApplication *application = [UIApplication sharedApplication];
application.applicationIconBadgeNumber++;
localNotification.applicationIconBadgeNumber = application.applicationIconBadgeNumber;
[self performSelectorOnMainThread:#selector(scheduleNotification:)
withObject:localNotification waitUntilDone:NO];
}
- (void)scheduleNotification: (id)notification
{
UILocalNotification *localNotification = (UILocalNotification *)notification;
// Schedule it with the app
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
I have checked the notification settings for my app, and they are:
Alert Style: Banners
Badge App Icon: On
Sounds: Off
Show in Notification Center: On
Include: 5 Recent Items
Show on Lock Screen: On
The bug was actually in a different part of my code. I was generating the notification in a background thread, and the thread was canceled before the notification went out.
If your app is running you can't have this banners (unless you create your own).
A solution could be:
When the app is running, Notification are handle by
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
Then you can use this project (that I use and which is really good) : TSMessages to create something similar as your banner.
Hope that will help...

ios - local notification not updating badge number when application is closed

I have noticed that when a local notification is being received in an ios device, the notification appears in the Notification Center but the app badge number is not updated when the app is closed.
I need to touch the notification in the Notification Center for the local push message to be transferred to the app.
Is this the normal behavior? Can this be solved by using remote push notifications?
You can utilize the applicationIconBadgeNumber parameter in a UILocalNotification object.
Basically:
localNotificationObject.applicationIconBadgeNumber++;
Example:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:20];
localNotification.alertBody = #"Some Alert";
//the following line is important to set badge number
localNotification.applicationIconBadgeNumber++;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
But the issue with this is that the badge number doesn't increment on subsequent (multiple) local notifications (there's a scenario here but for simplicity sake, lets just say the badge stays 1 even after 2 or more, back to back, local notifications).
In this case, Yes... Push Notification seems to be the way to go
(but be aware that Push Notifications aren't always reliable... check: link)
Well... to use Push Notifications for proper badge number updates, you should know that you can send a badge count in the Push Notification's payload.
When this push notification is received, the badge count is changed by iOS to the badge count specified in the Push Notification (& the app need not be open for this).
Example (continued):
Set applicationIconBadgeNumber to 0 as it helps in certain scenarios (optional)
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
Extra:
You can also manually set the badge number when you terminate/close or resign the application.
Generally... in any or all of the following methods:
-applicationWillResignActive
-applicationDidEnterBackground
-applicationWillTerminate (set badgeNumber when app closes)
Example:
- (void)applicationWillResignActive:(UIApplication *)application {
//Called when the application is about to move from active to inactive state.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
//...
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[[[UIApplication sharedApplication] scheduledLocalNotifications] count]];
//...
}
iPhone: Incrementing the application badge through a local notification
It is not possible to update dynamically the badge number with local notifications while your app is in the background. so You have to use push notifications. You can only increment badge while application is running in foreground and look for alternative solution you can go with here
iPhone: Incrementing the application badge through a local notification

How to show an alertview on receiving push notifications when app is not launched?

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

Can UILocalNotification be used to wake up a task that is in the background

I would like to know, if it is possible to somehow "wake up" a task that is in the background, to quickly check something on the network.. I think that this could be done with UILocalNotification, however, no matter what I tried, I could not get the didReceiveLocalNotification to do ANYTHING when the app is in the background.. After starting up, I immediately close the app by pressing the Home button (there is a 10 second delay for local notification to fire). This code works PERFECTLY when the app is in the foreground, and just kind of sitting there...
In app delegate header file:
UILocalNotification *localNotif;
For testing, I set up local notification to fire quickly in the appDelegate startup.
localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10]; // the date you want the notification to fire.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"setup the timer for 10 seconds");
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
UIApplicationState state = [application applicationState];
NSLog(#"getting kicked");
if (state == UIApplicationStateInactive) {
// Application was in the background when notification was delivered.
NSLog(#"INACTIVE..");
} else {
NSLog(#"ACTIVE..");
}
}
The user has a couple of choices: #1) Do they want to see a notification for your app. #2) If notifications are enabled for your app, do they want to click on your notification to launch your app. If they do accept notifications and open your notification while your app is in the background, application:didReceiveLocalNotification is called. To be clear, the user has to accept the notification (such as sliding the slider underneath the notification)... otherwise NOTHING is called.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
NSLog(#"%#", notification);
}
If your app has been terminated application:didFinishLaunchingWithOptions: is called -
- (BOOL)application:(UIApplication *)
application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
UILocalNotification *theNotification =
[launchOptions
objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSLog(#"%#", theNotification);
return YES;
}

Resources