Ritu Tyagi
Ritu Tyagi

Reputation: 224

How can I show a custom Local notification View or an Custom Alert view when app is in Background?

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.

Upvotes: 3

Views: 1046

Answers (2)

Nishant Gupta
Nishant Gupta

Reputation: 457

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 notificationenter image description here

Upvotes: 3

Wain
Wain

Reputation: 119041

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.

Upvotes: 2

Related Questions