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

不问归期 提交于 2019-12-12 23:28:46

问题


I am new in iOS and I am facing problem regarding to show local notification when app is running. 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!


回答1:


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.

The UNUserNotificationCenterDelegate protocol defines methods for responding to actionable notifications and receiving notifications while your app is in the foreground. You assign your delegate object to the delegate property of the shared UNUserNotificationCenter object. The user notification center object calls methods of your delegate at appropriate times to deliver information.

Then when notification arrives it calls 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. If your delegate does not implement this method, the system silences alerts as if you had passed the UNNotificationPresentationOptionNone option to the completionHandler block. 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.

How can you show the notification?

For achieving this we have 3 options

Sound
Badge
Alert 

UNNotificationPresentationOptions help us to implement this

Above is the good one to understand and do.

I preferred some answers also. They are

Show iOS notification when app is running

Displaying a stock iOS notification banner when your app is open and in the foreground?

Getting local notifications to show while app is in foreground

Notification while app is in foreground

Notification in iOS 10

Local Notification

didReceiveRemoteNotification not called , iOS 10




回答2:


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

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




回答3:


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


来源:https://stackoverflow.com/questions/43694163/how-to-get-local-notification-when-app-is-running-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!