如何在iPhone中使用动态数据在状态栏中显示通知?
在我的应用程序中,我使用一些网络连接获取数据 我想在iPhone的通知栏(状态栏)中显示这些数据 那个么,当我向下拖动iPhone时,如何添加可以在状态栏中看到的数据呢 我搜索了很多教程,但没有找到好的,请帮助我 请告诉我一些想法,我可以在通知或任何好的教程中管理我的数据 请推荐任何好的教程,以便我可以在通知栏中管理我的动态数据 获得数据后,感谢您:如何在iPhone中使用动态数据在状态栏中显示通知?,iphone,ios,objective-c,xcode,statusbar,Iphone,Ios,Objective C,Xcode,Statusbar,在我的应用程序中,我使用一些网络连接获取数据 我想在iPhone的通知栏(状态栏)中显示这些数据 那个么,当我向下拖动iPhone时,如何添加可以在状态栏中看到的数据呢 我搜索了很多教程,但没有找到好的,请帮助我 请告诉我一些想法,我可以在通知或任何好的教程中管理我的数据 请推荐任何好的教程,以便我可以在通知栏中管理我的动态数据 获得数据后,感谢您: UILocalNotification *localNotif = [[UILocalNotification alloc] init]; loc
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);
}
获取数据后:
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);
}
我认为,UILocalNotifications是不受欢迎的。现在您可以使用UNUserNotificationCenter。我们可以向其添加图像,如下所示: UNUserNotificationCenter*中心=[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
{
}
}];
我认为,UILocalNotifications是不受欢迎的。现在您可以使用UNUserNotificationCenter。我们可以向其添加图像,如下所示: UNUserNotificationCenter*中心=[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
{
}
}];
感谢Imran bhai的工作,现在我想在我收到的通知上绑定事件,我想通过单击我收到的通知来做一些工作,我想对它做一些操作,所以请您进一步告诉我如何进一步,谢谢:)检查回答中添加的此实现感谢Imran bhai正在工作,现在我想在我收到的通知上绑定事件。我想通过单击我收到的通知来对此进行一些工作。我想对此进行一些操作。因此,请您进一步告诉我如何进一步操作。谢谢:)检查在回答中添加的此实现