Presenting UILocalNotification not showing banner immediately - ios

Hi I am pushing 3 local notification one by one in a 3 second gap. I am getting 1st notification immediately after push it but rest of the notifications came after few minutes. I went through few documents and blogs but all are pushing the same way in which I am doing in my application. Please help me to resolve this issue. Any help would be appreciated.
Here my code to push the notification
UILocalNotification *notification = [UILocalNotification new];
// Notification details
notification.alertBody = message;
notification.alertAction = NSLocalizedString(#"View", nil);
notification.soundName = UILocalNotificationDefaultSoundName;
notification.userInfo = info;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];

Related

didReceiveLocalNotification triggered when presentLocalNotificationNow is called

When I create my local notification callback didReceiveLocalNotificationgets triggered. The same callback gets triggered when I click on the local notification. Currently I was dividing those two cases by checking
if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
//this means notification is clicked
}
But the main problem here is that when you are in the foreground and you slide your notification menu, and then receive your local notification, this callback didReceiveLocalNotification gets called. And in this case my app goes into this if. Because of this, I can't really distinguish from clicking the notification and creating a local notification while app is in the inactive state. Any ideas on how can I fix this?
This is the code for scheduling a local notification:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"aaaa";
localNotification.alertTitle = #"title";
localNotification.userInfo = myUserInfo;
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
After calling this, I get didReceiveLocalNotification delegate triggered.
I had to work around the exact same issue in my app. I couldn't find an offical way in the API to tell the difference, but here is a workaround.
Pass the current date in the userInfo when the local notification is created:
localNotification.userInfo = ["alertDate": NSDate()]
And when you handle didReceiveLocalNotification, check against current date again, to make sure it didn't just fire just a moment ago:
if application.applicationState == .Inactive {
if let alertDate = notification.userInfo?["alertDate"] as? NSDate
where (NSDate()).timeIntervalSinceDate(alertDate) > 0.1 {
// this means notification was initiated by user
}
}
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = #"image inserted";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[self dismissViewControllerAnimated:YES completion:nil];

UILocalNotification with repeat-once behaviour like in Messages App

I have simple UILocalNotification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Message";
notification.alertAction = #"Action";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.category = kCategoryIdentifier;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Is it possible, to repeat notification once, for example after two minutes? I want behaviour exacly, like in Messages app.
I have tried to set repeatInterval property of notification object, but:
Notification will be presented to user every two minutes, not repeated only once
System shows to user new notification, not repeat the old one. User see two notifications, one with timestamp 2 minutes after another.
Which is not what I've expected.
Also, because of second reason, I don't want to schedule two separate notifications.
Edit: In my app time when something happend is very important. Because of that, in lock screen, when notification is repeated, I want user to know that is something that happend earlier, not in time when notification arrives. So repeated notification should have timestamp of first notification.
Yes, you can set repeatInterval.
See documentation here
The calendar interval at which to reschedule the notification.
Declaration SWIFT var repeatInterval: NSCalendarUnit OBJECTIVE-C
#property(nonatomic) NSCalendarUnit repeatInterval
try this code
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertBody = #"Message";
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber=1;
NSLog(#"LocalNotif.soundName %#",localNotif.soundName);
for (int i=0; i<20; i++)
{
localNotif.fireDate = [repeatAlarm dateByAddingTimeInterval:120*i];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

Receive Local Notification In watchkit

I have setup Local Notification as below :
-(void)startLocalNotification
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:7];
notification.alertBody = #"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 10;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"Hello! This is Local Notification!" forKey:#"Notification"];
notification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
I received notification on iPhone Simulator. But it will not fired notification of watchkit app. I am using below method in NotificationController.m in watchkit extension -
- (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler
{
NSLog(#"Notification Received ..");
completionHandler(WKUserNotificationInterfaceTypeCustom);
}
Can anyone tell me why I am not receiving local notification in watchkit App.
Thanks in advance.
See Apple Watch Programming Guide HERE
When one of your app’s local or remote notifications arrives on the user’s iPhone, iOS decides whether to display that notification on the iPhone or on the Apple Watch.
So you don't need to worry about to send notification on Watch, iOS will do it for you.
Anyhow if you want to send notification on watch only you can use Darwin Notification Concepts, but keep in mind there are some limitations, you can check in the given link.
You can also use shared container for communication between App and extension.
Hope it will help you.

Display UILocalNOtification Alert in iOS Till VOIP call is valid

I am developing iPhone App Using PJSIP. When i enters background mode,if i receive any call.
I am displaying UILocalNotification alert as:
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSString *alertBody = [NSString stringWithFormat:#"NEW VOIP CALL"];
notification.alertBody =alertBody;
notification.alertAction = #"Answer";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
But this alert dismiss after some 4 or 5 seconds.
how to make it repeat for some more time?
The best way in your circumstances is to specify a sound to play - this can be up to 30 seconds long. This seems to keep the notification displayed for the length of the sound:
UILocalNotification *notification =[[UILocalNotification alloc] init];
NSString *alertBody = [NSString stringWithFormat:#"NEW VOIP CALL"];
notification.alertBody =alertBody;
notification.alertAction = #"Answer";
notification.soundName = #"56 Alarm Bell.mp3";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Solution 1:You need to Execute UIlaocationNotification Using Timer.
Solution 2: You Start Execute Notification when finish Previous notification.

How to cancel Notification from notification center which is push notification

I am developing iOS app with both Push notification and Local Notification, I know how to remove 1 and all notification from center,
using
[[UIApplication sharedApplication]cancelAllLocalNotifications];
But my problem is if i have scheduled some local notifications ,
and a push notification is arrived so in didReceiveRemoteNotification
I am write
[[UIApplication sharedApplication]cancelAllLocalNotifications]; for clear notification center,
but it is cleared all my LocalNotification also...
EDIT
if there are total 3 notifiction in NC i.e. 1 is come from local notification and two from push( from server) in this case how can i handle it,? i am tap on 1st notification( comes from server) in NC. in this case what should do, my app badge should be 2.
then what should i do?
The cancelAllLocalNotifications will only cancel the local notifications, its even in the name! Not the push notifications as you can read in the documentation:
Cancels the delivery of all scheduled local notifications.
Since push notifications are server side there is noting to cancel in your app. To remove the push notification from the notification center just set the applicationBadegNumber to 0.
There is an option ,each notification contains a dictionary inside it, so when you creates any local notification add any key in dictionary which specifies that this notification is for local notification.so you can check that if it is not my local notification then i will remove it.
-(void)scheduleLocalNotification{
[self cancelAlarm]; //clear any previous alarms
UILocalNotification *alarm = [[UILocalNotification alloc] init];
alarm.alertBody = #"alert msg";
alarm.fireDate = [NSDate dateWithTimeInterval:alarmDuration sinceDate:startTime];
alarm.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:#"localNotification" forKey:#"localNotification"];
alarm.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
-(void)cancelNotification{
for (UILocalNotification *notification in [[[UIApplication sharedApplication] scheduledLocalNotifications] copy]){
NSDictionary *userInfo = notification.userInfo;
if (![self.key isEqualToString:[userInfo objectForKey:localNotification]]){
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
}
The applicationbadgenumber is managed by you. Deleting scheduled notifications will not change that number. You must manage that yourself. I gave a presentation a while back that might help. http://www.youtube.com/watch?v=ixQqZWtn0pg Start watching at 11:50.

Resources