IOS push: local notification UILocalNotification

Source: Internet
Author: User
Tags notification center

IOS push: local notification UILocalNotification

Notification is a very common information transfer mechanism in smartphone application programming. It can save resources and check information status without consuming resources ), apps in iOS are divided into two different Notification types: local and remote. Local Notification is centrally managed by NotificationManager in iOS. You only need to add encapsulated local Notification objects to the queue of the system Notification management mechanism. The system will trigger local Notification at the specified time, the application only needs to design the method for processing Notification to complete the whole Notification process.

The object used by local Notification is UILocalNotification. The properties of UILocalNotification cover all the content required to process Notification. UILocalNotification attributes include fireDate, timeZone, repeatInterval, repeatCalendar, alertBody, alertAction, hasAction, alertLaunchImage, applicationIconBadgeNumber, soundName, and userInfo.


Scheduling of UILocalNotification

FireDate, timeZone, repeatInterval, and repeatCalendar are used for UILocalNotification scheduling. FireDate is the exact time when UILocalNotification is triggered. TimeZone indicates whether the excitation time of UILocalNotification changes according to the time zone. If it is set to nil, UILocalNotification will be triggered after a period of time, rather than a specific time. RepeatInterval is the time difference between the repeated calls of UILocalNotification. However, the time difference is based entirely on the calendar unit (NSCalendarUnit), such as the unit of the weekly calls and NSWeekCalendarUnit. If this parameter is not set, the call will not be repeated. RepeatCalendar is the reference calendar for the calendar unit used by the UILocalNotification repeated excitation. If it is not set, the default calendar will be used as the reference calendar.

Reminder content of UILocalNotification

AlertBody, alertAction, hasAction, and alertLaunchImage are processed by the system when the application is not running.



1. Add a local push

// 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"]; // The time when the notification is triggered.

*/

// Chuagjian

One local push

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

If (noti ){

// Set the push time

Noti. fireDate = date; // = now

// Set the time zone

Noti. timeZone = [NSTimeZone defaultTimeZone];

// Set the repetition Interval

Noti. repeatInterval = NSWeekCalendarUnit;

// Push sound

Noti. soundName = uilocalnotificationdefasoundname;

// Content

Noti. alertBody = @ "push content ";

// Number in the Red Circle displayed on the icon

Noti. applicationIconBadgeNumber = 1;

// Set userinfo for ease of use when you need to undo it later

NSDictionary * infoDic = [NSDictionary dictionaryWithObject: @ "name" forKey: @ "key"];

Noti. userInfo = infoDic;

// Add push to uiapplication

UIApplication * app = [UIApplication sharedApplication];

[App scheduleLocalNotification: noti];

}


2. Receive local push messages when running the program


-(Void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification

{

UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "receive local reminder in app"

Message: notification. alertBody

Delegate: nil

CancelButtonTitle: @ "OK"

OtherButtonTitles: nil];

[Alert show];

// Here, you can use useinfo of notification to do something you want to do.

Application. applicationIconBadgeNumber-= 1;

}


3. Cancel a local push

UIApplication * app = [UIApplication sharedApplication];

// Obtain the local push Array

NSArray * localArr = [app scheduledlocalications ications];

// Declare the 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 whether an existing push with the same key is found

If (! LocalNoti ){

// Initialization does not exist

LocalNoti = [[UILocalNotification alloc] init];

}

If (localNoti &&! State ){

// Cancel push without pushing

[App cancelLocalNotification: localNoti];

[LocalNoti release];

Return;

}

}

4. Two methods are used to cancel registration of local notifications, one is to cancel the specified notification, and the other is to cancel all registration notifications:

[[UIApplication sharedApplication] cancelLocalNotification: localNotification];

[[UIApplication sharedApplication] cancelAllLocalNotification];

5. Four notification types for iOS5

5.1. Banner)

The banner notification is a new feature in iOS5. It is a horizontal bar displayed on the top of the screen. It will automatically disappear in a few seconds. A banner notification will display the small icons of the Program (29 & times; 29 icons are displayed on the low split screen, 58 & times; 58 icons are displayed on the high split screen), the program name, and the notification content. The small icon helps users to see at a glance which application is reminding them.

5.2. Reminder (Alert)

The reminder notification does not disappear automatically. You must interact with the notification to disable it. The designer needs to design the specific content of the notification, and sometimes the action button design the title. The background style of the whole reminder notification, including the button style in it, is immutable, so designers and developers should not be creative here. Because of its openness, Android can use self-designed controls, while iOS may do so to maintain UI style consistency.

5.3. Mark (Badge)

A tag notification is a red oval icon in the upper right corner of the program icon. The number displayed in the tag indicates the number of notifications to be processed by the user. Similarly, the color, shape, and size of the tag cannot be changed. The number of updated applications in the App Store and the number of unread emails received in Mail are marked to notify users.

5.4. Sound)

Voice notification is also a notification method for iOS. It supports customization and can be used with the preceding three notification types.

6. Local notifications and push notifications

IOS apps will use local notifications or push notifications to remind users:

6.1. local notification

Local notifications of an application are generated and published by the user's iOS device, regardless of whether the application runs on the frontend. Like a calendar application or a to-do list application, you can send a local notification to remind users that a meeting is about to begin.

6.2. push notifications

The Push Notification of an application is released as follows: the remote server (Provider) of the application first sends a Notification to Apple's Push Notification service (APNS ), the Notification Server of apple then pushes the notification to all iOS devices installed with the application.

To use push notifications, you must first register the types of notifications you want to use in the push notification service of apple, for example, the following code indicates that two types of notifications, tag and sound, are registered simultaneously:

-(Void) applicationDidFinishLaunching :( UIApplication *) app {// other setup tasks here ....

[[UIApplication sharedApplication] registerForRemoteNotificationTypes :( UIRemoteNotificationTypeBadge | uiremotenotiftypetypesound)];}

How to use various notifications

Apps that use local notifications can use banners, reminders, tags, and sounds. However, apps that use push notifications must first register the types of notifications to use.

Pay attention to the following points when using notifications:

1. Ensure that the marked content is up-to-date

2. Do not send multiple notifications to the same event

3. The notification content does not need to contain the application name

For tag notifications, when all items are resolved, the tag will automatically disappear.

In banners, reminders, and the notification center on the top, iOS automatically displays the application name in the message. Therefore, when designing the notification content, you do not need to include the app name.

Note the following when designing the notification content:

1. Focus on the expression of information, rather than user actions. Avoid prompting the user which button to click or how to open the app

2. Short, preferably no more than two rows. It is difficult to read long information quickly, and there must be a scroll bar.

3. Use the sentence capitalized (sentence-style capitalization, the first letter of the first word capitalized) and appropriate punctuation marks, ending with generally use periods

"Reminder"

A reminder may contain one or two buttons. For reminders with two buttons, you need to put the button for closing the reminder on the left and the action button on the right.

Click the button on the left to close the reminder. click the button on the right to close the reminder and open the application.

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

Clicking this OK button will only close the reminder and will not open the application.

Note the following when designing the title of the action button:

1. The title can accurately describe the action of opening an application. For example, you can click the Play button to open the game and immediately Play it.

2. Use title-style capitalization to uppercase the first letter of each word)

3. Brief enough

When you receive a new notification when you lock the screen, the title of the action button will be displayed in the "slide to view" information, replacing the word "view, and convert all letters to lowercase letters.

About "sound"

When a user does not use a mobile phone, the voice can attract their attention. This prevents the user from missing important information.

We can use the built-in sound or custom sound. If you use a custom sound, make sure it is short enough and carefully crafted.

About "notification bar"

As long as the screen is not locked, the notification center can be opened from the top of the screen.

In a game, such as fruit ninja, iOS5 makes it easier to open the notification center to prevent your fingers from slipping out of the screen: A drop-down entry similar to a non-slip pattern appears from the top of the screen, and then drag it down again to open the notification center. Otherwise, the drop-down record is automatically collapsed several seconds later. The design with a slide drop-down bar can help new users quickly create a mental model of the pull-down open notification center.

OS5 organizes the notifications in the drop-down notification bar. The notifications of the same app are grouped in chronological order. To delete a notification message of an app, you need to click the "X" button on the right and change the "X" button to "clear". You can click "clear" again to remove all notifications under the app. The two-step operation reduces the probability of accidental deletion, and the interactive form is much more friendly than the common pop-up mode window with secondary confirmation.

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.