I am trying to send a local notification with a action button.
The notification is ok. But the action button is not displayed.
I am using Xcode 6.3.2 iPhone 6 iOS 8.2
Sending notification
UIApplication *myapp = [UIApplication sharedApplication];
UILocalNotification *test = [[UILocalNotification alloc] init];
if(test) {
NSLog(#"Sending notification");
test.alertBody = #"jothi test alert";
test.alertTitle = #"kottapa";
test.soundName = UILocalNotificationDefaultSoundName;
test.alertAction = NSLocalizedString(#"View Details", nil);
test.category = #"MY_CATEGORY";
[myapp presentLocalNotificationNow:test];
}
in app delegate didFinishLaunchingWithOptions:
UIMutableUserNotificationAction *myAction =
[[UIMutableUserNotificationAction alloc] init];
myAction.identifier = #"MY_IDENTIFIER";
myAction.title = #"Test Action";
myAction.activationMode = UIUserNotificationActivationModeBackground;
myAction.destructive = NO;
myAction.authenticationRequired = NO;
//------------
// First create the category
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
// Identifier to include in your push payload and local notification
inviteCategory.identifier = #"MY_CATEGORY";
// Add the actions to the category and set the action context
[inviteCategory setActions:#[myAction]
forContext:UIUserNotificationActionContextDefault];
// Set the actions to present in a minimal context
[inviteCategory setActions:#[myAction]
forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObject:inviteCategory];
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
Your code works, you just needs to swipe the notification to the left in order to see the action button (or pull it down, if it's presented in a form of a banner from the top, when the device isn't locked).
Also, I'm guessing you already did that but just didn't include the code, in order to handle the action the user chose, you need to implement this method in your app delegate:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
Related
I have configured an action for a remote notification when it arrives to my iOs app. I want two different actions whether one key passed in the payload. The code seems to be executed, but the app doesn't open, nor the safari url. Here is my AppDelegate.m:
NSString *const NotificationCategoryOpenView = #"openView";
NSString *const NotificationActionOpenView = #"View";
- (void)registerForNotification {
UIApplication *application = [UIApplication sharedApplication];
// iOs 8 or greater:
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
UIMutableUserNotificationAction *open;
open = [[UIMutableUserNotificationAction alloc] init];
[open setActivationMode:UIUserNotificationActivationModeBackground];
[open setTitle:NSLocalizedString(#"View", nil)];
[open setIdentifier:NotificationActionOpenView];
[open setDestructive:NO];
[open setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryOpenView];
[actionCategory setActions:#[open]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else if ([application respondsToSelector:#selector(registerForRemoteNotificationTypes:)]) {
// iOs 7 or lesser:
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:NotificationActionOpenView]) {
NSDictionary *aps = userInfo[#"aps"];
if ([[aps allKeys] containsObject:#"viewToOpen"]){
NSString *webString = [[NSString alloc] initWithFormat:#"%#", aps[#"viewToOpen"]];
NSURL *webURL = [[NSURL alloc] initWithString:webString];
// This line doesn't work:
[[UIApplication sharedApplication] openURL:webURL];
} else {
// These two lines doesn't work:
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController popToRootViewControllerAnimated:NO];
}
}
completionHandler();
}
Thanks!
Your code should be in this method
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if(userInfo) {
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
NSUInteger notificationType = [[userInfo valueForKey:API_NOTIFICATION_TYPE] integerValue];
if (state == UIApplicationStateActive) {
// Write code which should work when notification comes and app is active
}
else {
// Write code which should work when notification comes and app is in background or inactive or terminated
}
}
}
}
This method will get called only
when app is ACTIVE and notification comes
when app is in background or inactive or terminated and notifications comes and user taps on it
Implement
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
And
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
Methods.
Notification activation mode is set to UIUserNotificationActivationModeBackground,which Activate the app and put it in the background. If the app is already in the foreground, it remains in the foreground.So if you want to application in foreground/launch then use UIUserNotificationActivationModeForeground
[open setActivationMode:UIUserNotificationActivationModeBackground];
Use this activation option:
[open setActivationMode: UIUserNotificationActivationModeForeground];
Check weather webUrl is proper and working as expected in normal flow.
For Xcode 8, u will have to migrate push notification using UserNotification framework.
Actually I want to show a local notification in my app. But i want to show this notification with some custom design like ok and cancel button on it.And on the click of Ok and Cancel i want to perform some actions.Please suggest something.
Thanks In advance.
i am impliment this code for custom local notification may be helping you
where Accept is identifier where you get in delegates of local notification in appDelegate.m
NSString *idetnt = #"Accept";
NSString *idetnt1 = #"Reject";
NSString *idetnt2 = #"Local Notification";
UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = idetnt;
notificationAction1.title = #"Snooze";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO;
UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = idetnt1;
notificationAction2.title = #"Call";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = YES;
notificationAction2.authenticationRequired = YES;
UIMutableUserNotificationAction *notificationAction3 = [[UIMutableUserNotificationAction alloc] init];
notificationAction3.identifier = #"Reply";
notificationAction3.title = #"Reply";
notificationAction3.activationMode = UIUserNotificationActivationModeForeground;
notificationAction3.destructive = NO;
notificationAction3.authenticationRequired = YES;
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = #"Email";
[notificationCategory setActions:#[notificationAction1,notificationAction2,notificationAction3] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:#[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];
UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:600];
localNotification.alertBody = idetnt2;
localNotification.category = #"Email";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
when ur app in background then this methods call
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
if ([identifier isEqualToString:#"Accept"])
{
}
else if ([identifier isEqualToString:#"Reject"])
{
}
else if ([identifier isEqualToString:#"Reply"])
{
}
if(completionHandler != nil)
completionHandler();
}
when ur app in foreground then this methods call
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)localNotification
{
}
Go to capability option and click on remote notification
You can't add full customisation, but your notifications can set the category property to specify which notification settings should be used and those notification settings can specify which buttons to show and what those buttons to when the user selects them.
I'm building an app that allows its users to send each other messages. When a new message is received, it appears in the current user's Table view. I want my app to send the current user a notification when a new message arrives. Does anyone know how I might go about doing this?
I have notifications set up in my AppDelegate already:
appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
and I know the below code will allow me to fire a notification at a specified time (e.g. set by a picker):
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = self.itemText.text;
localNotification.alertAction = #"You are being notified!";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
// Dismiss the view controller
[self dismissViewControllerAnimated:YES completion:nil];
How do I make a notification occur however when a new message is posted to the server (and appears in the tableview) for the logged in user?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];}
If your system is iOS8, you need to register permission.
UILocalNotification * notification = [[UILocalNotification alloc] init];
NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow:Time];
if (notification!=nil) {
notification.fireDate= pushDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = kCFCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.alertBody = #"Time O.K!";
NSLog(#"Ready");
notification.applicationIconBadgeNumber = 1;
//notification.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count]+1;
NSDictionary * inforDic = [NSDictionary dictionaryWithObject:#"name" forKey:#"key"];
notification.userInfo =inforDic;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
You can have a try,this code I can successfully use on my device.
I am using UILocalNitification to send notifications when the app is running in the background, my code is:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// ask fo permission to send alert
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]];
}
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
// Define an ID string to be passed back to your app when you handle the action
acceptAction.identifier = #"ACCEPT_IDENTIFIER";
// Localized string displayed in the action button
acceptAction.title = #"Take Photos";
// If you need to show UI, choose foreground
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
// Destructive actions display in red
acceptAction.destructive = NO;
// Set whether the action requires the user to authenticate
acceptAction.authenticationRequired = NO;
// First create the category
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
// Identifier to include in your push payload and local notification
inviteCategory.identifier = #"PHOTO";
// Add the actions to the category and set the action context
[inviteCategory setActions:#[acceptAction]
forContext:UIUserNotificationActionContextDefault];
// Set the actions to present in a minimal context
[inviteCategory setActions:#[acceptAction]
forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[self.window makeKeyAndVisible];
return YES;
}
and the code use to handle the button event is:
- (void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler{
if ([identifier isEqualToString:#"ACCEPT_IDENTIFIER"]) {
NSLog(#"Here");
}
completionHandler();
}
My problem is when testing on a simulator, the button event can be captured, but when testing on a real device (iPod with iOS version 8.1.2 and iPhone 5S iOS8.1.X), both devices cannot get the button event.
Can anybody help?
I've got notifications with some actions; now if there's only one action for a notification, it displays the "alert view" with a dismiss and an options button. When pressing the options, I get 'Open', 'My Action' and 'Close'.
How can I achieve to avoid the options menu and get 'Dismiss' and 'My Action' right from the beginning?
I'm talking about the alert that appears e.g. when you're on the iPhone home screen and you have selected alert style "Alerts" for "Alert Style When Unlocked"
I've got a local push notification with just one action defined. Now I would expect to get the notification on the Home Screen with "Close" on the left and my defined action on the right. But instead I get this one:
I would love to avoid the additional step of tapping "Options" and then only get one real option.
The code I'm using:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) {
return;
}
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:NOTIFICATION_DELAY];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = text;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.category = ERROR_CATEGORY;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
registering for push notifications:
UIMutableUserNotificationAction *callSupportAction = [self actionWithIdentifier:#"callSupportAction" title:NSLocalizedString(#"callSupportActionTitle", #"") background:YES destructive:NO authenticationRequired:NO];
UIMutableUserNotificationCategory *errorCategory = [self categoryWithIdentifier:ERROR_CATEGORY actions:#[callSupportAction]];
NSSet *categories = [NSSet setWithObjects:errorCategory, nil];
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
- (UIMutableUserNotificationAction *)actionWithIdentifier:(NSString *)identifier title:(NSString *)title background:(BOOL)background destructive:(BOOL)destructive authenticationRequired:(BOOL)authenticationRequired {
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = identifier;
action.title = title;
action.activationMode = background ? UIUserNotificationActivationModeBackground : UIUserNotificationActivationModeForeground;
action.destructive = destructive;
action.authenticationRequired = authenticationRequired;
return action;
}
- (UIMutableUserNotificationCategory *)categoryWithIdentifier:(NSString *)identifier actions:(NSArray *)actions {
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = identifier;
if (actions.count > 2) {
[category setActions:actions forContext:UIUserNotificationActionContextDefault];
[category setActions:[actions subarrayWithRange:NSMakeRange(0, 2)] forContext:UIUserNotificationActionContextMinimal];
} else {
[category setActions:actions forContext:UIUserNotificationActionContextDefault];
}
return category;
}