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.
Related
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];
I am having an aiff file of 5.0 seconds. I want to run it in loop up to 30 seconds. Please guide how could I do that.
Right now I am setting the notification in the normal way by the following code-
//Function to schedule local notification
-(void)schedulelocalnotification:(NSDate *)particularfiredate ringtone: (NSString *)particularringtone name:(NSString *)alarmname info:(NSDictionary *)dicttext
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = particularfiredate;
notification.soundName = [arrayAIFFFiles objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]];
notification.alertBody = alarmname;
notification.userInfo = dicttext;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Please note that I don't want the notification banner and the notifications to appear 6 times, Just the sound should repeat up to 30 seconds.
If I am not doing this, And adding a sound file of 30 seconds, then the sound is continuous till 30 seconds, If the user closes the notification in between, and opens the app, The sound continues to play.That issue has been stated here : The UILocalNotification sound does not stop playing
I fixed it by creating multiple notifications at regular intervals which will be the duration of the track. Following is my code :
-(void)schedulelocalnotification:(NSDate *)particularfiredate ringtone: (NSString *)particularringtone name:(NSString *)alarmname info:(NSDictionary *)dicttext
{//post one single notification here with alert body, so that there are no multiple banners for each notification.
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = particularfiredate;
notification.soundName = [arrayAIFFFiles objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]];
notification.alertBody = alarmname;
notification.userInfo = dicttext;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
totalDuration = totalDuration + [[arraySoundDuration objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]]floatValue];
while (totalDuration<30) {//while time is less than 30 secs, which is the desired time.
NSDate *newDate1 = [particularfiredate dateByAddingTimeInterval:totalDuration];
UILocalNotification *notification1 = [[UILocalNotification alloc] init];
notification1.fireDate = newDate1;
notification1.soundName = [arrayAIFFFiles objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]];
// notification1.alertBody = alarmname;//create notification without alert body, just with the sound file, so there are no multiple banners.
notification1.userInfo = dicttext;
[[UIApplication sharedApplication] scheduleLocalNotification:notification1];
totalDuration = totalDuration + [[arraySoundDuration objectAtIndex:[arraysoundfilesnames indexOfObject:particularringtone]]floatValue];
}
totalDuration = 0;
}
-(void)notifyMe
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.1];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"Alert";
localNotification.alertAction = #"Local notification";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertLaunchImage = nil;
localNotification.userInfo = nil;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
This is the method I am calling for local notification. This is working fine with other applications. But with my working one, notification is coming but with no sound.
I guess there is some problem with app setting, but I am unable to find it out.
I found your question as I had run into the same problem. Turns out it was a simple fix on my part, my code was fine all I had to do was enable push notifications for my app (which I had already done but was getting no sound or badge), and then switch Badge App Icon and Sound to ON.
I'm surprised there have been no other answers to this question in 6 months.
I need to send a text message when the current time equals the time selected in the UIDatePicker. How might I do this? You don't need to include the code to send the message, I already have that coded. I've tried all sorts of things with NSTimer and if - then statements but none have worked.
Edit: Since I wrote this question I've found a better way to do things. I just need to set a local notification and when received execute my code with -(void)didRevieveLocalNotification. Here is what I have so that any googlers can hopefully be helped.
NSDate *pickerDate = [self.datePicker date];
//Set Local Notification
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = pickerDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
//----------------------------------------------------------------------
notif.alertBody = #"Tap to send your text message!";
notif.alertAction = #"send message...";
notif.soundName = #"sms_alert_nova.caf";
notif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
well i would use a local notification... something like this
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = theDate //The date that your picker has selected
notification.alertBody = #"Hey, the time just expired!"
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Then in your AppDelegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//Code to manage the notification logic
}
Hope this helps, the user will get the alert even if on background.. if on background the user must click the alert to let your application know that the local notification triggered, if he does (or he is on your app already, then the app delegate method will trigger letting your app know that the notification fired...
Hope this helps!
I know how to schedule a localNotification and set the applicationIconBadgeNumber when the localNotification is pushed:
- (void) scheduleNotificationOn:(NSDate*) fireDate
text:(NSString*) alertText
action:(NSString*) alertAction
sound:(NSString*) soundfileName
launchImage:(NSString*) launchImage
andInfo:(NSDictionary*) userInfo
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
....
self.badgeCount ++;
localNotification.applicationIconBadgeNumber = self.badgeCount;
// Schedule it with the app
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
As the code shows, applicationIconBadgeNumber is set before the localNotification is called. I want to know is it possible to calculate the applicationIconBadgeNumber when the localNotification is called and display it nearby the app icon?
No, you can't. Unless your app is running in foreground that you can catch the fire of local notification.