Remove open button form uilocalnotification - ios

I want to remove the "open" app button from the alert notification. When user set the notification and notification appears when app is in background I want to remove the open button.
Please see the image. This is the by default alert appears when notification fire. I want to remove that open button.
This is the Code when user schedule the notification
-(void) scheduleLocalNotificationOnlyOneTime:(NSDate *)fireDate
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.timeZone = [NSTimeZone localTimeZone];
localNotif.fireDate = fireDate;
if(!category){
localNotif.category = #"ACTIONABLECATEGORY";
}
else{
localNotif.category = #"ACTIONABLE";
}
localNotif.alertBody = #"Time Up";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
and this is the code in which I create my alert
UIMutableUserNotificationAction *accept=[[UIMutableUserNotificationAction alloc] init];
[snooze setActivationMode:UIUserNotificationActivationModeBackground];
[snooze setTitle:#"Accept"];
[snooze setIdentifier:NotificationActionOneIdent];
[snooze setDestructive:NO];
[snooze setAuthenticationRequired:YES];
UIMutableUserNotificationAction *maybe=[[UIMutableUserNotificationAction alloc] init];
[watch setTitle:#"Maybe"];
[watch setActivationMode:UIUserNotificationActivationModeForeground];
[watch setIdentifier:NotificationActionTwoIdent];
[watch setDestructive:NO];
[watch setAuthenticationRequired:YES];
UIMutableUserNotificationAction *decline=[[UIMutableUserNotificationAction alloc] init];
[watch setTitle:#"Decline"];
[watch setActivationMode:UIUserNotificationActivationModeForeground];
[watch setIdentifier:NotificationActionTwoIdent];
[watch setDestructive:NO];
[watch setAuthenticationRequired:YES];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:#[decline, accept,maybe]
forContext:UIUserNotificationActionContextDefault];
Thanks

Related

Interactive notification in Objective C

I want to implement interactive notification with two buttons with actions. Where I have to create buttons and their actions?
I have done this but from where I have to call this method?
- (void)registerForNotification
{
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:#"Action 1"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:#"Action 2"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:#[action1, action2]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
func showInterActiveNotification() {
let category = UIMutableUserNotificationCategory()
let restartAction = UIMutableUserNotificationAction()
restartAction.identifier = "xx"
restartAction.destructive = false
restartAction.title = "Restart"
restartAction.activationMode = .Background
restartAction.authenticationRequired = false
let categoryIdentifier = "category.identifier"
category.identifier = categoryIdentifier
category.setActions([restartAction], forContext: .Minimal)
category.setActions([restartAction], forContext: .Default)
let categories = Set(arrayLiteral: category)
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: categories)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let localNotif = UILocalNotification()
localNotif.alertBody = "testBody"
localNotif.category = categoryIdentifier
// Notification will be shown after 10 second (IMPORTANT: if you want to see notification you have to close or put app into background)
localNotif.fireDate = NSDate().dateByAddingTimeInterval(10)
UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
}
Handle Notification
func application(application: UIApplication, handleActionWithIdentifier identifier: String?,
forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
completionHandler()
}
Call this in didFinishLaunchingWithOptions method in AppDelegate. Nothing else you need to do.
To handle the actions call the handler.
if you want to check whith local notification plz use this
-(void)localnotificationGenrated
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.soundName = #"Default";
localNotification.alertBody = #"my notification;)";
localNotification.fireDate = [NSDate date];
localNotification.category = NotificationActionOneIdent;
localNotification.repeatInterval = kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
and if you want with push notification create payload with category.
like this
{"aps":{"alert":"Hello Testing","badge":1,"sound":"default","category":"your_category_key"}}
and call your method
- (void)registerForNotification
in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Local notification override Remote notification (Actionable notification) setting in iOS 9?

I used following code for remote notification
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:#"REJECT"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];////UIUserNotificationActivationModeBackground
[action2 setTitle:#"ACCEPT"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:#[action1, action2]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
//Right, that is the point
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
//register to receive notifications
[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
This works fine show's correct (Accept/Reject buttons). For some condition i want to wake up app to Foreground so i am using following local notification code in
(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void
(^)())completionHandler method.
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeForeground];
[action1 setTitle:#"LAUNCH"];
[action1 setIdentifier:#"OPEN_ACTION"];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:#"LOCAL_NOTIFICATIOn"];
[actionCategory setActions:#[action1]
forContext:UIUserNotificationActionContextDefault];
[actionCategory setActions:#[action1]
forContext:UIUserNotificationActionContextMinimal];
NSSet *categories1 = [NSSet setWithObject:actionCategory];
UIUserNotificationSettings *settings2 = [UIUserNotificationSettings settingsForTypes:
UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:categories1];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings2];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date];
localNotification.alertTitle= #"Security settings enabled,";
localNotification.alertBody = #"tap the Launch button to start the application";
localNotification.category = #"LOCAL_NOTIFICATIOn";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Problem: First time Remote notification show's Accept/Reject button correctly but after registering Local notification Remote notification doesn't shows action buttons(Accept/Reject). I can't seen buttons in alerts?
Your remote notification setting overridden by local notification setting.
// Registering UIUserNotificationSettings more than once results in previous settings being overwritten.
- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;
comment this code:
[[UIApplication sharedApplication] registerUserNotificationSettings:settings2];

Implement the delegates for receiving push notification actions

I've created a push local notification for my alarm project.
The problem is that when I click an action button (snooze and okay) the delegates that I implemented doesn't get called.
Here is my code for push local notification:
UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = Snooze;
notificationAction1.title = #"Snooze";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO;
UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = Okay;
notificationAction2.title = #"Okay";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = NO;
notificationAction2.authenticationRequired = NO;
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = #"Alarm";
[notificationCategory setActions:#[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:#[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];
Here is the code for implementing the delegate:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {
if ([identifier isEqualToString:Snooze]) {
NSLog(#"You chose Snooze");
}
else if ([identifier isEqualToString:Okay]) {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSLog(#"You chose Okay");
}
if (completionHandler) {
completionHandler();
}
}
I don't know yet how to make the snooze and okay button to work so I just log something but nothing is being log when I click snooze/okay. Am I missing something?
Please check below code, It may help you..
NSString * const NotificationCategoryIdent = #"ACTIONABLE";
NSString * const NotificationActionOneIdent = #"ACTION_ONE";
NSString * const NotificationActionTwoIdent = #"ACTION_TWO";
- (void)registerForNotification {
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:#"Action 1"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:#"Action 2"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:#[action1, action2]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
There are 2 new methods on the UIApplicationDelegate protocol.
1) application:handleActionWithIdentifier:forLocalNotification:completionHandler:
2) application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
These methods will get called, in the background, when the user selects an action from your push notification.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:NotificationActionOneIdent]) {
NSLog(#"You chose action 1.");
}
else if ([identifier isEqualToString:NotificationActionTwoIdent]) {
NSLog(#"You chose action 2.");
}
if (completionHandler) {
completionHandler();
}
}
Source Link

Setting dynamic button titles in iOS8 local notifications

I have followed the examples here and elsewhere for setting up 1 or 2 button local notifications and have that working just fine. The examples are just fine if you need canned buttons like Accept/Delete/etc, but I need need to set the button titles for what is happening in the app at the time. This is for a game and needs a generic engine. I need to be able to set the button titles to match the needs at the time. I have two categories so I can do one button or two button notification. It seems like the actions and categories need to be setup when you register for the notification. The title on the UIMutableUserNotificationCategory is shown as "read only". I know this can be done. For example the game "Spy Watch" can be played 90% of the time from the notifications.
Is there anyway set the titles when scheduling the notification? I have passed dictionaries with the data I need to process the response.
This is the AppDelegate side
NSString * const NotificationCategoryIdent1 = #"1BUTTON";
NSString * const NotificationCategoryIdent2 = #"2BUTTON";
NSString * const NotificationActionOneIdent = #"ACTION_ONE";
NSString * const NotificationActionTwoIdent = #"ACTION_TWO";
- (void)registerForNotification {
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:#"Button 1"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:#"Button 2"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory2;
actionCategory2 = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory2 setIdentifier:NotificationCategoryIdent2];
[actionCategory2 setActions:#[action1, action2]
forContext:UIUserNotificationActionContextDefault];
UIMutableUserNotificationCategory *actionCategory1;
actionCategory1 = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory1 setIdentifier:NotificationCategoryIdent1];
[actionCategory1 setActions:#[action1]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObjects:actionCategory1, actionCategory2, nil];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
Here is how I set up the notification in my view controller
UILocalNotification* localNotification2 = [[UILocalNotification alloc] init];
localNotification2.fireDate = [NSDate dateWithTimeIntervalSinceNow:30];
localNotification2.alertBody = #"Alert Body2";
localNotification2.timeZone = [NSTimeZone defaultTimeZone];
localNotification2.soundName = UILocalNotificationDefaultSoundName;
localNotification2.category = #"2BUTTON";
//localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];

ios 8 interactive notifications not showing actions

This is my code to register Interactive Notifications for ios8 :
+ (void)registerInteractiveNotifications
{
UIMutableUserNotificationCategory *corideInviteCategory = [self corideInviteCategory];
UIMutableUserNotificationCategory *riderInviteCategory = [self riderInviteCategory];
NSSet *categories = [NSSet setWithObjects:corideInviteCategory, riderInviteCategory, nil];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
+ (UIMutableUserNotificationCategory *)riderInviteCategory
{
UIMutableUserNotificationAction *accept;
accept = [[UIMutableUserNotificationAction alloc] init];
[accept setActivationMode:UIUserNotificationActivationModeForeground];
[accept setTitle:#"Accept"];
[accept setIdentifier:RiderInviteAccept];
[accept setDestructive:NO];
[accept setAuthenticationRequired:NO];
UIMutableUserNotificationAction *decline;
decline = [[UIMutableUserNotificationAction alloc] init];
[decline setActivationMode:UIUserNotificationActivationModeForeground];
[decline setTitle:#"Decline"];
[decline setIdentifier:RiderInviteDecline];
[decline setDestructive:YES];
[decline setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:RiderInviteCategory];
[actionCategory setActions:#[decline, accept]
forContext:UIUserNotificationActionContextDefault];
[actionCategory setActions:#[decline, accept] forContext:UIUserNotificationActionContextMinimal];
return actionCategory;
}
+ (UIMutableUserNotificationCategory *)corideInviteCategory
{
UIMutableUserNotificationAction *accept;
accept = [[UIMutableUserNotificationAction alloc] init];
[accept setActivationMode:UIUserNotificationActivationModeForeground];
[accept setTitle:#"Accept"];
[accept setIdentifier:CorideInviteAccept];
[accept setDestructive:NO];
[accept setAuthenticationRequired:NO];
UIMutableUserNotificationAction *decline;
decline = [[UIMutableUserNotificationAction alloc] init];
[decline setActivationMode:UIUserNotificationActivationModeForeground];
[decline setTitle:#"Decline"];
[decline setIdentifier:CorideInviteDecline];
[decline setDestructive:YES];
[decline setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:CorideInviteCategory];
[actionCategory setActions:#[decline, accept]
forContext:UIUserNotificationActionContextDefault];
[actionCategory setActions:#[decline, accept] forContext:UIUserNotificationActionContextMinimal];
return actionCategory;
}
What happens is : when i delete the app and install again, the 2 action buttons (when I pull down the notification banner, or swipe left in notification center) appears. But after a while ( i'm not really sure what cause it ), they stop appearing although i keep sending the same notification. This is my notification content:
{"aps":{"alert":"test","category":"coride_invite"},"journey_id":100}
Can anyone shed some light please ? Thanks
Check for the following in you code if it is anywhere else as well:
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
Since, UIUserNotificationSettings is singleton, whenever you call this, it overwrites the old settings. So if new settings is registered without any buttons it will not show any buttons.
Better way of registering new setting is explained here:
Interactive push notifications - Hide/Show buttons

Resources