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.
Related
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.
I'm making an applikation which is using push notifications, I am currently using pushbots for the push notifications. I was wondering if there is any way to intercept the notifications that is received by the application and check the notification before the notification is shown on the device. And if the data in the notification is not correct, dont show a notification at all? Is this possible with pushbot or do I need to do it all by my self?
Yes, you can achieve this behaviour playing around with Local Notifications.
You can configure your payload without an alert and "content-available": "1" so your application can receive notifications without showing them to the user.
// Payload
{
aps: {
"content-available": 1
},
text: 'my alert message' // your custom info
}
And in your app code, register notifications as
// Register notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
UIRemoteNotificationTypeNewsstandContentAvailability];
Then, the key is to trigger Local Notifications in the application:didReceiveRemoteNotification:fetchCompletionHandler: method based on some condition
- (void)application:(UIApplication *)application didReceiveRemoteNotification: (NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"push data package: %#", userInfo);
// Retrieve your data
NSString *text = [userInfo objectForKey:#"text"];
BOOL mustShow = YES;
// Only show notification if app is background and your custom condition
if ((state == UIApplicationStateInactive || state == UIApplicationStateBackground)
&& mustShow) {
// Raise the local notification a second after received
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = text;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
This way you can "intercept" the notifications before "displaying" them to the user.
You can't prevent the notification from being shown once it reaches the device (assuming it contains an alert field in the aps dictionary - if it doesn't, no notification will be shown anyway).
You should determine in your server which notifications should be sent to which device tokens. You can associate device tokens with users in your DB, if your functionality requires it.
Even if what you request was possible, it would be very inefficient to send notifications to all the devices that installed your app, and then only display the notification in a small subset of them.
I got my push notification successfully in both background and foreground but my problem is when my application is in active state/foreground and push notification is arrived then i show this pushnotification message on alertview using didReceiveRemoteNotification method,
My alert view have two buttons
1) later
2) Ok.
If I press 1) "later" button then I want add this pushnotification message in notification area so after some time user can see and tap on that particular push notification and go with that and that record of push notification will remove from notification area.
This is not possible. There is no API to access notification are of iOS.
Alternative
What near alternative you can try is Local Notification. When user select later set Local Notification for that thing. You can add this Local Notification when user leave your app so that you don't get notification while user is continue with your application.
Better Approach
The most general approach for this problem is Notification screen in app. Your application has one screen which has list of received notification so that user can check that in you app. I suggest you to go with this. Because this is most common and clear idea.
You need to implement core data for Notifications but it can only happen when your Application is is Active state.
1-create a new identity everytime a new notification arrive.
2-save it.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *Notification = userInfo;
NSString *title = [(NSDictionary*)[(NSDictionary*)[Notification valueForKey:#"aps"] valueForKey:#"alert"] valueForKey:#"title"];
NSString *body = [(NSDictionary*)[(NSDictionary*)[Notification valueForKey:#"aps"] valueForKey:#"alert"] valueForKey:#"body"];
XXNotification *objNotification = [XXNotification create];
objNotification.title = title;
objNotification.detail = body;
[XXNotification save:nil];
NSArray *arrNotification =[XXNotification allUnRead:nil];
[UtilityFunctions setApplicationBadgeNumber:[arrNotification count]];//Utility functions is my class for common functions.
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateActive)
{
[UtilityFunctions showAlertView:title message:body delegate:self cancelButtonTitle:#"Ok" otherButtonTitle:#"Cancel" withTag:99 withAccessibilityHint:[NSString stringWithFormat:#"%#:|:%#", title,body]];
}
else if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateInactive || [[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = body;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
if (![IsLocationSaved isEqualToString:#"NO"])
{
[[NSNotificationCenter defaultCenter]postNotificationName:kNotificationForShowingNotification object: nil userInfo:nil];
}
}
}
On showing the UIAlertView, on its click event either delete that notification in DB or make a bool in it as isRead and make it YES. then save it,
On Notifications list Query the notification from DB or only those whose isRead = NO.
That is the way I did it in My Application.
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.
i'm not receiving newsstand notifications when the app is not running, here is what I have done.
The app has the proper plist keys 'UINewsstandApp = YES' and 'UIBackgroundModes = newsstand-content'.
In the app delegate I register for all notification types, and i receive my token from APNS, from the server side ( i am using a gem called Grocer ) i set up the dev certificate and send a regular push and it works.
if i send a newsstand push I receive it if the app is running on 'didReceiveRemoteNotification', but when the app is not running i get nothing in the notification center, which is mainly because 'Grocer' has the following payload
{"aps": {"content-available":1}} and can't add any other keys ( alert, badge, etc )
so I thought that I should not get anything in the notification center, I look for the 'UIApplicationLaunchOptionsRemoteNotificationKey' in the launch options, and then write a file to make sure that the app ran in the background, the file is never written as such
NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(remoteNotif)
{
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [cachePath stringByAppendingPathExtension:#"pushreceived.txt"];
[#"testing" writeToFile:filePath atomically:YES encoding:NSASCIIStringEncoding error:nil];
}
I have NKDontThrottleNewsstandContentNotifications set to true in my user defaults, and then I synchronize to make sure.
when the app is running, no matter how many times i send the push, I always get a callback on " didReceiveRemoteNotification ", with the proper "content-available"
if the app is closed or in the background, nothing happens.
Update:
I managed to change the gem that sends the notification payload, here is the dictionary it sends
{"aps"=>
{
"alert"=>"Hello iPhone!!",
"content-available"=>1,
"badge"=>1,
"sound"=>"default"
}
}
and here the userinfo dictionary I receive on my app ( while running )
{
aps = {
alert = "Hello iPhone!!";
badge = 1;
"content-available" = 1;
sound = default;
};
}
please note the quotation marks around content-available, does this mean that APNS parsed it as a custom key ?
To receive notifications when your application is running in the background, you need to add a applicationDidEnterBackgroud method to your main class. From the Apple documentation:
Applications might also find local notifications useful when they run in the background and some message, data, or other item arrives that might be of interest to the user. In this case, they should present the notification immediately using the UIApplication method presentLocalNotificationNow: (iOS gives an application a limited time to run in the background). Listing 2-2 illustrates how you might do this.
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"Application entered background state.");
// bgTask is instance variable
NSAssert(self->bgTask == UIInvalidBackgroundTask, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self->bgTask];
self->bgTask = UIInvalidBackgroundTask;
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
// Replace this code with your notification handling process
NSString *friend = [self checkForIncomingChat];
if (friend) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithFormat:
NSLocalizedString(#"%# has a message for you.", nil), friend];
localNotif.alertAction = NSLocalizedString(#"Read Message", nil);
localNotif.soundName = #"alarmsound.caf";
localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
[localNotif release];
friend = nil;
break;
}
}
}
[application endBackgroundTask:self->bgTask];
self->bgTask = UIInvalidBackgroundTask;
});
}
Also note:
Because the only notification type supported for non-running applications is icon-badging, simply pass NSRemoteNotificationTypeBadge as the parameter of registerForRemoteNotificationTypes:.
Try these as a sanity check:
In Settings -> Newsstand, you have automatic content download as on for your app.
Make sure that you used [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeNewsstandContentAvailability] somehwere in your app.
Make sure that you have required background mode as 'newsstand-content' in your plist.
Run the app in the device, not in the simulator.
If you have done those correctly you should get a notification in didReceiveRemoteNotification even when your app is not running.