zeeshan shaikh
zeeshan shaikh

Reputation: 819

How to display notifications in status bar using dynamic data in iPhone?

In my app i am getting data using some network connection

i want to show that data in notification bar (status bar) in iPhone

so how to add data that i can see in status bar of iPhone when i drag it down

i search many tutorials but i did not find any good one please help me

Please tell me some ideas that i can manage my data in notification or any good tutorial

Please suggest any good tutorial so i can manage my dynamic data in notification bar

Thanks

Upvotes: 1

Views: 5044

Answers (2)

Ajjjjjjjj
Ajjjjjjjj

Reputation: 675

i think, uilocalnotificatios are deprecated. now you can use, UNUserNotificationCenter. we can add image to it as well as shown below:

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

        UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;

        [center requestAuthorizationWithOptions:options
                              completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                  if (!granted) {
                                      //NSLog(@"Something went wrong");
                                  }
                              }];

        int dayCounter =5;

        int minute = 48;


       {
            NSDateComponents *components = [[NSDateComponents alloc] init];
            components.weekday = dayCounter;


             dayCounter++;

            components.hour = 12;
            components.minute = minute;

            UNCalendarNotificationTrigger *trigger =  [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

            UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
            objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];

            objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"We made a surprise Edit for You" arguments:nil];

            objNotificationContent.sound = [UNNotificationSound defaultSound];

            objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);


            UNNotificationAttachment *attachment = nil;

            NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:filePath];
            NSError *attachmentError = nil;

            attachment = [UNNotificationAttachment attachmentWithIdentifier:@"image"
                                                                        URL: outputURL
                                                                    options:nil
                                                                      error:&attachmentError];
            if (attachmentError) {

                return;
            }

            objNotificationContent.attachments=@[attachment];

            NSString *identifier = @"UYLLocalNotification";
            UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                                  content:objNotificationContent trigger: trigger];

            [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                if (error != nil) {
                    NSLog(@"Something went wrong: %@",error);
                }
                else
                {
                }
            }];

Upvotes: 1

Imran Ahmed
Imran Ahmed

Reputation: 1033

After getting data:

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = date;  // date after 10 sec from now
localNotif.timeZone = [NSTimeZone defaultTimeZone];

// Notification details
localNotif.alertBody =  text; // text of you that you have fetched
// Set the action button
localNotif.alertAction = @"View";

localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;

// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;

// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

To handle onclick of Noification:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

// Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    application.applicationIconBadgeNumber = 0;

// Handle launching from a notification

    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNotif) {
        NSLog(@"Recieved Notification %@",localNotif);
    }

    return YES;
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification  *)notif {
// Handle the notificaton when the app is running

    NSLog(@"Recieved Notification %@",notif);
}

Upvotes: 7

Related Questions