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];
Related
Any possibility to reset/clear app badge without any app interactions such as opening the app or any push/local notifications. My need is, I want to reset my app badge starting off every day without even opening the app or with any notifications.
You can schedule local notification for end of day with badge count equal 0. Check this out Updating iOS badge without push notifications
I solved this problem with the help of this solution. The working code is:
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
NSCalendarUnit unitDay = NSCalendarUnitDay;
localNotification.repeatInterval = unitDay;
localNotification.fireDate = fireDate;
localNotification.applicationIconBadgeNumber = -1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Local notification has no alertBody, so local notification will not display upfront.
If need to show alertBody then add localNotification.alertBody = #"Your notification alertBody";.
Thanks for the answer.
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.
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) ?
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!
In my app, I am trying to create an Alarm but the only problem is with ios6 UILocalnotification is been converted to badge instead of UIAlertView.
Here's the code i am using:
UILocalNotification * notif = [[UILocalNotification alloc]init];
notif.fireDate = date;
notif.alertAction = #"Snooze";
notif.repeatInterval = NSDayCalendarUnit;
notif.alertBody = #"Time to wake up";
notif.soundName = #"client_song.mp3";
[[UIApplication sharedApplication]scheduleLocalNotification:notif];
Sorry, you don't get any choice about that. The user is allowed to specify (in the Settings app) that a local notification from your app should appear as an alert, or as a banner, or not appear at all - and you can't do anything about it. If you don't like that, don't use a local notification...