【问题标题】:How to display notifications in status bar using dynamic data in iPhone?如何使用 iPhone 中的动态数据在状态栏中显示通知?
【发布时间】:2013-10-14 09:23:00
【问题描述】:

在我的应用中,我正在使用一些网络连接获取数据

我想在 iPhone 的通知栏(状态栏)中显示该数据

那么当我向下拖动它时,如何添加我可以在 iPhone 的状态栏中看到的数据

我搜索了很多教程,但我没有找到任何好的,请帮助我

请告诉我一些想法,我可以在通知或任何好的教程中管理我的数据

请推荐任何好的教程,以便我可以在通知栏中管理我的动态数据

谢谢

【问题讨论】:

    标签: iphone ios objective-c xcode statusbar


    【解决方案1】:

    获取数据后:

    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];
    

    处理通知的点击:

    - (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);
    }
    

    【讨论】:

    • 感谢 Imran bhai 的工作,现在我想在收到的通知上绑定事件 我想通过单击收到的通知来做一些工作那你能告诉我更多我怎么能走得更远谢谢:)
    • 检查答案中添加的这个实现
    【解决方案2】:

    我认为,uilocalnotificatios 已被弃用。现在您可以使用 UNUserNotificationCenter。我们可以向它添加图像,如下所示:

    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
                    {
                    }
                }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多