Search code examples
iosobjective-cnotificationsuilocalnotificationpushkit

Get Local notification text on tap when App is minimize ios


Example:- I have generated 4 local notifications with 4 types in minimize State.

Type of notifications below.

  1. Message

  2. Friend Request

  3. Video call

  4. Audio call

notifications are shown in notification center.

Noow..I have clicked on 2nd notification,

  1. How can i get which number of notification clicked?

  2. How can i get 2nd notification body alert text(content)?

this mehod is not working for minimize state

like whatsApp.. I need to go on specific screen according to Notification type.

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
NSString *Notitype=@"";
if ([[payload.dictionaryPayload valueForKey:@"type"]isEqualToString:@"video"]) {
    Notitype=@"video";
}
else if ([[payload.dictionaryPayload valueForKey:@"type"]isEqualToString:@"friendRequest"]) {
     Notitype=@"friend Request";
}
else if([[payload.dictionaryPayload valueForKey:@"type"] isEqualToString:@"message"] )
{
    Notitype=@"message";
}
else{
    Notitype=@"audio";
}

I'm creating UILocalNotification like this :

UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.repeatInterval = NSDayCalendarUnit;
[notification setAlertBody:Notitype];
[notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[notification setTimeZone:[NSTimeZone  defaultTimeZone]];
[[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];

Solution

  • For Set local notification as:

    // Schedule the notification
        UILocalNotification *notification = [[UILocalNotification alloc]init];
        notification.repeatInterval = NSDayCalendarUnit;
        [notification setAlertBody:Notitype];
        [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
        [notification setTimeZone:[NSTimeZone  defaultTimeZone]];
    
        //Create user info for local notification.
        NSDictionary* dict = @{@"Type": @"message", @"ID": @"set your unique ID", @"body": notification.alertBody,};
        notification.userInfo = dict;
    
        //You can Set Type as: message, friend Request, video call, Audio call.
    
        [[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
    

    for get which type of local notifications is click than you can use didReceiveLocalNotification delegate.

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
        //[super application:application didReceiveLocalNotification:notification]; // In most case, you don't need this line
    
        //Get notification type
        NSString *notificationType = [notification.userInfo valueForKey:@"Type"];
        //notificationType as: message, friend Request, video call, Audio call.
    
        NSString *notificationBody = [notification.userInfo valueForKey:@"Notification_body"];
    
        UIApplicationState state = [application applicationState];
        if (state == UIApplicationStateActive) {
            // Application in foreground when notification is delivered.
    
            if ([notificationType isEqual:@"message"]) {
                //Handle message
            } else if ([notificationType isEqual:@"friend Request"]) {
                //Handle friend Request
    
            } else if ([notificationType isEqual:@"video call"]) {
                //Handle video call
    
            } else if ([notificationType isEqual:@"Audio call"]) {
                //Handle Audio call
    
            }
    
        } else if (state == UIApplicationStateBackground) {
            // Application was in the background when notification was delivered.
            if ([notificationType isEqual:@"message"]) {
                //Handle message
            } else if ([notificationType isEqual:@"friend Request"]) {
                //Handle friend Request
    
            } else if ([notificationType isEqual:@"video call"]) {
                //Handle video call
    
            } else if ([notificationType isEqual:@"Audio call"]) {
                //Handle Audio call
    
            }
    
        } else {
    
        }
    }
    

    Update

    Also check this same things in DidFinishLaunchingWithOptions, possible user don't react on notification, but tap on App icon to open app. So in case of video call or anything urgent then it must be handled through DidFinishLaunchingWithOptions