Receive Local Notification In watchkit - ios

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.

Related

Save Local Notification in ios

How to save the local notification? I want to use the local notification fire date again to schedule local notification.
You should use UILocalNotification
Please check out documentation: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/
It look something like:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"Alert Fired at %#", dateTime];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
About notification re-schedule cycle:
Firing the notification is not enough to show up another one. Your app won't be launch except if user tap the notification.
You could pre-schedule a notification for every day or week for the next month and unschedule them if user open one. Depending of your need you could also fire a remote notification with a content-available key which will launch your app in background and allow you to reschedule a new local notification.

Cancel Banner Notification in iOS8 Not Working

My title might not accurately depict my question, so I apologize. I have looked for a solution to creating this level of functionality but I am unable to find it.
I am creating a VoIP application for iOS 8. When a user receives a call I am displaying a notification with a 12 second ringtone. While this notification is in progress if the call disconnects I want the Incoming Call notification to disappear and display a Missed Call notification immediately. This level of functionality is possible because Viber does it.
Currently, I am sending a silent push notification when a Incoming Call is available. This is my payload...
aps = {
"content-available" = 1;
};
category = INCOMING;
from = "+15555554220";
Upon receiving the silent push I am creating a Local Notification like this ...
if ([userInfo[#"category"] isEqualToString:#"INCOMING"]) {
NSLog(#"application: didReceiveRemoteNotification: fetchCompletionHandler: Incoming Call Notification Received");
NSLog(#"application: didReceiveRemoteNotification: fetchCompletionHandler: Sending Local Notification For Incoming Call");
// Get Caller Contact Info
NSDictionary *contact = [self findContactInfoForNumber:userInfo[#"from"]];
NSString *message = [NSString stringWithFormat:#"Incoming Call: %#",userInfo[#"from"]];
if (contact != nil) {
message = [NSString stringWithFormat:#"Incoming Call: %# %#",contact[#"firstName"],contact[#"lastName"]];
}
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithObject:#"Incoming Call" forKey:#"type"];
notification.userInfo = infoDict;
notification.category = #"INCOMING_CALL_CATEGORY";
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
notification.alertBody = message;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = #"ring.m4a";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Then, once the call is disconnected I am sending another silent push notification for a missed call...
aps = {
"content-available" = 1;
};
category = MISSED;
Once received I am canceling all local notifications like this...
if ([userInfo[#"category"] isEqualToString:#"MISSED"]) {
NSLog(#"application: didReceiveRemoteNotification: fetchCompletionHandler: Missed Call Notification Received");
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
The issue I am encountering is that on the lock screen it behaves exactly the way I want it to. I receive the incoming call notification and when the caller hangs up that notification immediately disappears from notification center and a missed call notification is there in the place of it. However when the phone is on the home screen. A banner is shown and then it plays the whole ringtone then displays the missed call. Does anyone know the reason why this is happening? Does anyone have any solutions to achieve this level of functionality? Like I said before the app Vider is a prime example of what I want my app to do.
Thanks you in advance.

How to register Local Notifications when app is in background or closed IOS

I am working on a Contact manager app in which i have to schedule birthday notifications of contacts that have birthday on same day or one day before the current date. Can i do it in background or when my app is not running.
Is it possible? If Yes then give some helpful links or blogs.
Thanks
You cannot register for local notifications when you are in background.
Use the below code to register for local notification.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"Your text";
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:120.0]; // your date
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
When your are in foreground, your application:didReceiveLocalNotification will be called.
When you are in background or your app is terminated, you will get the local notification in Notification panel of the device

Presenting UILocalNotification not showing banner immediately

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];

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