Simple reminders for uilocalnotification local notifications in iOS development

Source: Internet
Author: User

This period of time the project asked to do a similar alarm clock alert, I am not familiar with the notice, decided to use the local notification of Xcode to try, the ultimate success of the implementation of the function, special finishing sharing.

It's performance features:

Notifications can also be received and displayed when the app is closed.

Notifications can be displayed when the app is in the background.

The app can be received at the front desk, but it can't be displayed, but it will go through the application delegate method

Specific ways to create:

-"Create a local notification object uilocalnotification

-"Set Firedate,alertbody,alertaction,soundname,applicationbadgenumber,repeatinterval,alertlanuchimage property

-"Configure notification parameters, UserInfo." and the contents of the notice. We can get the object in the method that receives the notification.

-"Invoke notifications, use UIApplication's Singleton object Schedulelocalnotificaiton to start notifications as scheduled

It is important to note that the user's notification needs to be solicited since IOS8 , and if so, create uiuernotificationsettings and registerusernotificationsettings. Limits on the number of local notifications, iOS allows up to 64 recent local notifications, and local notifications exceeding the limit will be ignored by iOS.

Here is the detailed code:

1. Registration notice, also applicable to iOS10

In Appdelegate's application:didfinishlaunchingwithoptions: Call the following method

 

2. Definition and use of local notifications

In the case of a controller definition that requires a local notification, here for a simple and straightforward definition of a 5s alarm clock, it can be changed to any point in time, converted to nsdate type replacement [NSDate Datewithtimeintervalsincenow:5].

  

To differentiate between local notifications, you can define the following properties at the same time as defined

Set up information about the notification, this is important, you can add some of the marked content, easy to distinguish and get the notification information later
nsdictionary *infodic = [nsdictionary dictionarywithobjectsandkeys:local_notify_schedule_id,@ "ID", nil];
Localnotification.userinfo = Infodic;

3. Cancel Local Notifications

  Note: You must cancel all local notifications before you do not need them or refresh all local notifications, or there will be duplicate notifications.

//Cancel a notification  
Nsarray *notificaitons = [[UIApplication sharedapplication] scheduledlocalnotifications];
//Get all current local notifications
if (!notificaitons | | notificaitons.count <= 0) {
return;
}
For (uilocalnotification *notify in notificaitons) {
if ([[[Notify.userinfo objectforkey:@] id "] isequaltostring:local_notify_schedule_id]) {
//Cancel a specific notification
[[UIApplication sharedapplication] cancellocalnotification:notify];
Break ;
        }
    }
    
//Cancel all local notifications
[[UIApplication sharedapplication] cancelalllocalnotifications];

4. Responses to local notifications

If a local notification has been registered, when the client responds to the notification:

A, the application in the background, the local notification will send the device to a remote notification of the same reminder

b, the application is running, the device will not receive a reminder, but will walk the method in application delegate:

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) Notification {
}

If you want to implement the program in the background when the kind of reminder, you can add the relevant code in the above method

if ([[[Notification.userinfo objectforkey:@] id "] isequaltostring:@" Affair.schedule "]) {
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "test" Message:notification.alertBody Delegate:nil cancelbuttontitle:@ "Off" otherButtonTitles:notification.alertAction, nil Nil];
[Alert show];
}

It is important to note that in case a, if the user clicks on the reminder to enter the application, the callback method that receives the local notification will also be executed, in which case if you add the above code, there will be two consecutive prompts, in order to solve the problem, the code is as follows:

if ([[Notification.userinfo objectforkey:@ "id"] isequaltostring:@ " Affair.schedule "]) {  
      // Determine the current running state of the application, and if it is active, make a reminder, or do not alert   
        if (application.applicationstate = = uiapplicationstateactive) {  
           UIAlertView *alert = [[UIAlertView Alloc] initwithtitle:@ "Prompt" Message:notification.alertBody delegate:nil cancelbuttontitle:@ "Off" Otherbuttontitles: Nil, nil];  
             [alert show];  
       }  
  } 

Simple reminders for uilocalnotification local notifications in iOS development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.