I want to have a UILocalNotification firing in my application every minute for a variable number of times and after that, I want to cancel it.
I'm trying to find a parameter that would enable me to do that.
1) Scheduling notification
Note the .repeatInterval property value.
UILocalNotification *reminder = UILocalNotification.new;
reminder.fireDate = fireDate;
reminder.timeZone = [NSTimeZone systemTimeZone];
reminder.alertBody = #"Your alert message";
reminder.alertAction = #"Your alert action";
reminder.soundName = UILocalNotificationDefaultSoundName;
reminder.repeatInterval = NSMinuteCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:reminder];
2) Handling notification (AppDelegate)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// custom handling code
}
Note: in iOS 8 you have to register for local notifications for the delegate methods to be called.
3) Canceling UILocalNnotification
[[UIApplication sharedApplication] cancelLocalNotification:reminder];
4) Notification distinction
To keep track of how many times a notification has been received, you have to uniquely identify your notification(s). You can do that by making use of UILocalNotification instance .userInfo property.
Example
UILocalNotification *reminder = UILocalNotification.new;
...
reminder.userInfo = [NSDictionary dictionaryWithObject:#"custom value" forKey:#"notificationUniqueId"];
...
Then, when you receive your notification in delegate method written in 2), you can check for notification unique id that you stated in userInfo dictionary. Knowing that, you are able to keep track how many times a UILocalNotification has been fired and cancel it when appropriate.
Hope that helps!
Related
I'm sending a silent notification to a user that gets picked up trough "didReceiveRemoteNotification fetchCompletionHsndler" where I want to check som conditions and if it returns "true" for thoese want to make notificaton visible to a user -any idea on how can I accomplish that?
Yes, there are 2 ways for that.
If your condition returns true then:
1) Your app is an active state then use any third party/alert to show that navigation content.
2) Your app is in the background then fire local notification with exact content of the silent notification.
like this:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:1]; // will fire notification after 1 second
notification.alertBody = #""; // pass your body text here
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
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.
I need to present UILocalNotification after removing the same one by cancelLocalNotification:. The problem is removing performs slowly than firing new one and removes just added notification.
How can I determine that UILocalNotification cancelLocalNotification: was completed?
You can wait a little bit after calling UILocalNotification cancelLocalNotification:
and register again for the new UILocalNotification
Something like this
[[UIApplication sharedApplication] cancelLocalNotification:someNotification];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
/* register UILocalNotification again here after delay */
});
If you want to call notification with specific timer interval or call after first one is over then local notification it self has repeatInterval property and value should be NSCalendarUnit. And repeat with specific timer.
Otherwise second option is below delegate method always call
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
// I recieved a notification
}
When your get notification in the block
you can remove all local notification and create new one with fire date should be
localNotification.fireDate = [NSDate date];
Try checking,
[[UIApplication sharedApplication] scheduledLocalNotifications]
This will return array of scheduled notifications.
You can either check count of array or NSLog array and get details if local notification is cancelled or not.
It may be simple but i am looking for a specific solution inside a problem.
I would like to let user set a reminder ,but when the time is come, to check some condition in my server before i show him the notification message .
Also, i would like to have the ability to cancel all reminders in one line.
Right now i am using the notification alarm like this :
UILocalNotification * notification = [[UILocalNotification alloc] init];
notification.fireDate = newDate;
notification.repeatInterval = NSCalendarUnitDay;
notification.alertTitle=#"Reminder";
notification.alertBody = message;
notification.soundName=#"shake.mp3";
notification.userInfo=med;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
and cancel all of them with this :
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If the user hit the message i have a callback function. but i would like to check before i show the message-for a condition.
How do you set a reminder that fires a callback function,so you can also control the reminders,the specific messages,the condition of the app when user hit the message ( background/foreground etc) ?
In my app i am scheduling two UILocalNotification,one is fired depending on the time selected in UIDatePicker and other on repeat Interval basis.The problem here is i have to fire two different alert for both when user tap on the notification.How can i distinguish these two notification and fire corresponding alert .
Part of the UILocalNotification object you will get there is a userInfo property. Set a key like "type" to values like "main" and "repeat" to differentiate the notification.
What do you mean by repeat interval basic. Are you talking about the notification for the snooze functionality. If that is the case then you dont have to set the second notification for repeat interval at the time of setting the original notification. You can set the repeat interval notification only when the original notification is fired.
Besides you can set the setUserInfo attribute of the notification object.
[localNotification setUserInfo:[[NSDictionary alloc] initWithObjectsAndKeys:#"Snoozed", kSnoozedAlarm, nil]];
You can give your notification a unique name in order to distinguish it from the others.
You can then iterate through the scheduledLocalNotifications to search for the specific notification.
NSArray *arrayNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (int i = 0; i < [arrayNotifications count]; i++)
{
UILocalNotification *snoozedNotification = [arrayNotifications objectAtIndex:i];
NSDictionary *userInfo = snoozedNotification.userInfo;
if ([[userInfo objectForKey:kSnoozedAlarm] isEqualToString:#"Snoozed"])
{
[[UIApplication sharedApplication] cancelLocalNotification:snoozedNotification];
}
}
You can use the below code in order to repeat the alert.
[localNotification setRepeatInterval:NSWeekCalendarUnit];