iOS push: Local notification uilocalnotification

Source: Internet
Author: User

Notification is a very common mechanism of transmitting information in the application programming of smart phone, and it can save resources very well, without consuming resources to check information status (Pooling), the application in iOS is divided into two different kinds of notification, local and remote. The local notification is managed centrally by iOS under Notificationmanager, simply by adding the encapsulated local notification object to the system notification management mechanism queue. The system will fire the local notification at the specified time, and the application will complete the notification process simply by designing a method to handle notification.

The objects that are used by local notification are uilocalnotification,uilocalnotification properties that cover all the content that is required to process notification. The attributes of Uilocalnotification are Firedate, TimeZone, Repeatinterval, Repeatcalendar, Alertbody, Alertaction, HasAction, Alertlaunchimage, Applicationiconbadgenumber, Soundname and UserInfo.

Dispatch of Uilocalnotification

Where Firedate, TimeZone, Repeatinterval, and Repeatcalendar are scheduled for uilocalnotification. Firedate is the exact time of uilocalnotification excitation. TimeZone is whether the uilocalnotification excitation time is changed according to the time zone change, if set to nil, then the uilocalnotification will be excited after a period of time, not an exact moment is excited. Repeatinterval is the time difference between uilocalnotification being repeated excitation, but the time difference is completely based on the calendar unit (nscalendarunit), such as the units that are fired weekly, Nsweekcalendarunit, If not set, the excitation will not be repeated. Repeatcalendar is the calendar that the calendar units used by the uilocalnotification repeat firing need to refer to, and if not set, the system default calendar will be used as the reference calendar.

Uilocalnotification's reminder content

Alertbody, Alertaction, Hasaction, and Alertlaunchimage are when the application is not running, the system processes

1. Add a local push

Set after 20 seconds

NSDate *date = [NSDate datewithtimeintervalsincenow:20];

/*

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[Formatter setdateformat:@ "HH:mm:ss"];

NSDate *now = [Formatter datefromstring:@ "15:00:00"];//time to trigger notification

*/

Chuagjian

A local push

Uilocalnotification *noti = [[[[Uilocalnotification alloc] init] autorelease];

if (Noti) {

Set push time

Noti.firedate = Date;//=now

Setting the time zone

Noti.timezone = [Nstimezone defaulttimezone];

Set the recurrence interval

Noti.repeatinterval = Nsweekcalendarunit;

Push Sound

Noti.soundname = Uilocalnotificationdefaultsoundname;

Content

Noti.alertbody = @ "Push content";

The number of the red circle displayed on the icon

Noti.applicationiconbadgenumber = 1;

Setup UserInfo is convenient to use when you need to undo later

Nsdictionary *infodic = [nsdictionary dictionarywithobject:@ "name" forkey:@ "Key"];

Noti.userinfo = Infodic;

Add push to UIApplication

UIApplication *app = [UIApplication sharedapplication];

[App Schedulelocalnotification:noti];

}

2. A local push message is received while the program is running

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification*) notification

{

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "receive local alerts in app"

Message:notification.alertBody

Delegate:nil

cancelbuttontitle:@ "OK"

Otherbuttontitles:nil];

[Alert show];

Here, you can go through notification's useinfo and do what you want to do.

Application.applicationiconbadgenumber-= 1;

}

3. Cancel a local push

UIApplication *app = [UIApplication sharedapplication];

Get local push array

Nsarray *localarr = [app scheduledlocalnotifications];

Declaring a local notification object

Uilocalnotification *localnoti;

if (Localarr) {

For (Uilocalnotification *noti in Localarr) {

Nsdictionary *dict = Noti.userinfo;

if (dict) {

NSString *inkey = [dict objectforkey:@ "key"];

if ([Inkey Isequaltostring:key]) {

if (Localnoti) {

[Localnoti release];

Localnoti = nil;

}

Localnoti = [Noti retain];

Break

}

}

}

Determine if a push is found for the same key that already exists

if (!localnoti) {

Initialization not present

Localnoti = [[Uilocalnotification alloc] init];

}

if (Localnoti &&!state) {

Do not push cancel push

[App Cancellocalnotification:localnoti];

[Localnoti release];

Return

}

}

4. Two ways to unregister a local notification, one is to cancel the specified notification, and the second is to cancel all registration notices:

[[UIApplication sharedapplication] cancellocalnotification:localnotification];

[[UIApplication sharedapplication] cancelalllocalnotification];

5.IOS5 Four types of notifications

5.1. Banner (Banner)

Banner notifications are new features that appear in IOS5, which are displayed at the top of the screen and disappear after a few seconds. A banner notification will show the program's small icon (under the low split screen display 29×29 icon, high split screen display 58×58 icon), the name of the program and the contents of the notification. Small icons help users to see clearly which application is reminding them at a glance.

5.2. Alert (Alert)

Reminder notifications do not automatically disappear and require users to interact with them to close. Designers need to design the details of the notification, and sometimes the action button design title. The background style of the entire alert notification, including the style of the buttons inside, is immutable, so designers and developers don't have to be creative here. Android because of its openness, we can use their own design of the control, and iOS do this may be to maintain UI style consistency.

5.3. Mark (Badge)

A tag notification is a red oval marker that appears in the upper-right corner of the program icon, showing the number of notifications that need to be processed by the user. Similarly, the color and shape and size of the marker cannot be changed. The number of applications that are updated in the App store, and the number of unread messages received in mail is notified to the user with a tag.

5.4. Voice (sound)

Voice prompts are also a way of notifying iOS, which supports customization and can be used in conjunction with the previous three notification types.

6. Local notifications and push notifications

The iOS app alerts users by using local or push notifications:

6.1. Local Notifications

The local notification of the application is generated and published by the user's iOS device, regardless of whether the application is running in the foreground. Just like a calendar app, or a to-do list app, you can send a local notification to remind users that a meeting is about to begin.

6.2. Push Notifications

Usually use pigeons to push http://xg.qq.com/xg and Aurora push https://www.jpush.cn/ Two kinds of push third-party, the application's push notification is published like this: The application's remote server ( Provider) sends a notification to Apple's push notification service (Apple push Notification service, APNS), and Apple's notification server pushes the notification to all iOS devices that have the app installed.

If you want to use push notifications, you must first register the type of notifications you want to use in Apple's push notification service, such as the following code, which indicates that both the tag and the sound types of notifications are registered:

-(void) applicationdidfinishlaunching: (UIApplication *) app {//other setup tasks here ....

[[UIApplication Sharedapplication] Registerforremotenotificationtypes: (uiremotenotificationtypebadge| Uiremotenotificationtypesound)];}

How to use various notifications

Applications that use local notifications can use banners, reminders, tags, sounds, but applications that use push notifications need to first register the type of notification to use.

There are a few things to keep in mind when using notifications:

1. Guarantee that the content of the tag is up to date

2. Do not issue multiple notifications for the same event

3. Notification content does not include the name of the application

For tagged notifications, the tag disappears automatically when all the items are resolved.

In banners and reminders, and at the top of the notification hub, the iOS system automatically displays the name of the app in the message, so you don't need to include the name of the app when you design the content of the notification.

When designing the content of a notification, it is also important to note:

1. Focus on the expression of information, not the user's actions. Avoid prompting users to click on which button or how to open the app

2. Short, preferably no more than two lines. Long information is difficult to read quickly, and there must be scroll bars

3. Use sentence capitalization (Sentence-style capitalization, first letter capitalization) and appropriate punctuation, ending with a period

About "Reminders"

A reminder may contain one to two buttons. For reminders with two buttons, you need to leave the button on the left side of the alert, and put the action button on the right.

The button to the left of the point closes the reminder, and the button on the right side closes the reminder and opens the app.

If there is only one button, this button should be a OK button.

Clicking on this OK button will also turn off the reminder without opening the app.

For reminder notifications, be careful when designing the title of the action button:

1. Title can accurately describe the action of opening an app. For example, clicking on the play button will open the game and the user will be able to play immediately.

2. Use header capitalization (Title-style capitalization, capitalize each word's first letter)

3. Short Enough

iOS push: Local notification uilocalnotification

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.