简体   繁体   English

如何在Objective-C中运行应用程序时获取本地通知

[英]How to get local notification when app is running in Objective-C

I am new in iOS and I am facing problem regarding to show local notification when app is running. 我是iOS的新手,我正面临着在应用运行时显示本地通知的问题。 My code is like this 我的代码是这样的

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.
localNotification.alertBody = @"Message hear";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

I write this code on the button click but it is not showing me notification. 我在点击按钮上编写此代码,但它没有显示通知。 Thanks in Advance! 提前致谢!

No it does not show if you use above code.First of all I want to ask some questions. 如果您使用上述代码,请不要显示。首先,我想问一些问题。

Does your above code work if you use iOS 10?

Where do you call above lines of code in your class or in which method do you apply?

iOS 10 has new framework called UNUserNotification .We have to import the UNUserNotificationCenterDelegate for showing UILocalNotification when app is running. iOS 10有一个名为UNUserNotification新框架。我们必须导入UNUserNotificationCenterDelegate ,以便在应用程序运行时显示UILocalNotification。

The UNUserNotificationCenterDelegate protocol defines methods for responding to actionable notifications and receiving notifications while your app is in the foreground . UNUserNotificationCenterDelegate协议定义了在应用程序位于前台时响应可操作通知和接收通知的方法。 You assign your delegate object to the delegate property of the shared UNUserNotificationCenter object. 您将委托对象分配给共享UNUserNotificationCenter对象的委托属性。 The user notification center object calls methods of your delegate at appropriate times to deliver information. 用户通知中心对象在适当的时间调用您的代理方法来传递信息。

Then when notification arrives it calls willPresentNotification 然后当通知到达时,它会调用willPresentNotification

If your app is in the foreground when a notification arrives, the notification center calls below method 如果您的应用在通知到达时位于前台,则通知中心会调用以下方法

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
   willPresentNotification:(UNNotification *)notification 
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;

Above method delivers the notification directly to your app. 上述方法会将通知直接发送到您的应用。 If you implement this method, you can take whatever actions are necessary to process the notification and update your app. 如果您实施此方法,则可以采取处理通知和更新应用所需的任何操作。 When you finish, execute the completionHandler block and specify how you want the system to alert the user, if at all. 完成后,执行completionHandler块并指定系统如何提醒用户(如果有的话)。 If your delegate does not implement this method, the system silences alerts as if you had passed the UNNotificationPresentationOptionNone option to the completionHandler block. 如果您的代理没有实现此方法,系统会将警报静音,就好像您已将UNNotificationPresentationOptionNone选项传递给completionHandler块一样。 If you do not provide a delegate at all for the UNUserNotificationCenter object, the system uses the notification's original options to alert the user. 如果您没有为UNUserNotificationCenter对象提供委托,系统将使用通知的原始选项来提醒用户。

How can you show the notification?

For achieving this we have 3 options 为实现这一目标,我们有3个选择

Sound
Badge
Alert 

UNNotificationPresentationOptions help us to implement this UNNotificationPresentationOptions帮助我们实现这一点

Above is the good one to understand and do. 以上是理解和做的好人。

I preferred some answers also. 我也喜欢一些答案。 They are 他们是

Show iOS notification when app is running 在应用运行时显示iOS通知

Displaying a stock iOS notification banner when your app is open and in the foreground? 当您的应用程序处于打开状态且位于前台时显示库存iOS通知横幅?

Getting local notifications to show while app is in foreground 在应用程序处于前台时显示本地通知

Notification while app is in foreground 应用程序处于前台时通知

Notification in iOS 10 iOS 10中的通知

Local Notification 本地通知

didReceiveRemoteNotification not called , iOS 10 didReceiveRemoteNotification未调用,iOS 10

you have to add this code 你必须添加这个代码

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeNewsstandContentAvailability| UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

in didFinishLaunchingWithOptions 在didFinishLaunchingWithOptions中

if you set this then you have to change this line 如果你设置了这个,那么你必须改变这一行

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0]; //Enter the time here in seconds.

when you tap on button set notification but you cannot show because you app in active state and replace 0 to 60 time interval and put your app background and check 当你点击按钮设置通知但你无法显示,因为你的应用程序处于活动状态并替换0到60时间间隔并将你的应用程序背景和检查

try like this 试试这样

NSTimeInterval interval;
interval = 60* 60* 12;//12 hours from now
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];//Enter the time here in seconds.
localNotification.alertBody= @"This is message Users will see";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval= NSCalendarUnitMinute;//NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName= UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Objective-C IOS 7推送通知在应用程序未运行时保存数据? - Objective-C IOS 7 push notification save data when app not running? 如何检查 iOS 应用程序是否在模拟器上运行。 (目标-c) - How to check if an iOS app is running on the simulator. (objective-c) 如何在Objective-C上发送带有参数的通知? - How to send a notification with parameters on Objective-C? 本地通知横幅和声音在前景Objective-C中不起作用 - Local Notification Banner and sound not working in Foreground Objective-C 如何根据objective-c中的uiswitch ON / OFF情况实现删除和重新安排本地通知 - how to implement delete and reschedule local notification based on uiswitch ON/OFF case in objective-c 当应用程序未运行(即完全终止)时,如何在用户点击推送通知时打开特定控制器 Objective-C - How to open specific controller when user taps on Push Notifications when App is NOT running (i.e Totally Killed) Objective-C 当在后台收到通知时,Objective-c徽章会依靠应用程序图标吗? - Objective-c badge count on app icon when notification received in background? 在iOS目标C中应用处于活动状态时如何获取推送通知? - How to get push notification when app is in active state in ios objective c? 如何取消目标 c 中的本地单个通知 - how cancel local single notification in objective c 当应用程序后台运行时,Objective-c蓝牙的长期运行后台服务 - Objective-c long-running background service for bluetooth, when app backgrounded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM