Local notifications pushed by iOS UILocalNotification

  • 2020-12-16 06:09:02
  • OfStack

Abstract: Notification is a very common mechanism to transmit information in smartphone application programming, and it can save resources very well. There is no need to consume resources to constantly check the information status (Pooling). Under iOS, there are two different TYPES of Notification, local and remote. The local Notification is managed by NotificationManager under iOS. All that is needed is to add the encapsulated local Notification object to the system Notification management mechanism queue, and the system will excite the local Notification at a specified time. The application only needs to design the method to handle Notification to complete the whole Notification process.
Notification is a very common mechanism to transmit information in smartphone application programming, and it can save resources without consuming resources to constantly check the status of information (Pooling). Under iOS, there are two different TYPES of Notification, local and remote. The local Notification is managed by NotificationManager 1 under iOS. All that is needed is to add the encapsulated local Notification object to the system Notification management mechanism queue, and the system will fire the local Notification at a specified time. The application only needs to design the method to process Notification to complete the whole Notification process.

The object used by the local Notification is UILocalNotification, and the attributes of UILocalNotification cover everything needed to process Notification. The attributes of UILocalNotification are fireDate, timeZone, repeatInterval, repeatCalendar, alertBody, alertAction, hasAction, alertLaunchImage, applicationIconBadgeNumber, soundName and userInfo.

UILocalNotification scheduling

fireDate, timeZone, repeatInterval and repeatCalendar are used for the scheduling of UILocalNotification. fireDate is the exact time of UILocalNotification's excitation. timeZone is whether the excitation time of UILocalNotification changes according to the time zone. If it is set to nil, then UILocalNotification will be fired after a period of time rather than at an exact time. repeatInterval is the time difference between UILocalNotification being repeatedly fired, but the time difference is entirely based on the calendar unit (NSCalendarUnit), such as the weekly firing unit, NSWeekCalendarUnit, which will not be repeatedly fired if it is not set. repeatCalendar is the calendar unit used for UILocalNotification repeat firing which needs to be referred to. If not set, the system default calendar will be used as the reference calendar.

UILocalNotification reminder content

alertBody, alertAction, hasAction and alertLaunchImage are handled by the system when the application is not running

1. Add a local push


// Set up the 20 Seconds later  
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];
/*
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"HH:mm:ss"]; 
NSDate *now = [formatter dateFromString:@"15:00:00"];// The time the notification is triggered 
*/
 //chuagjian
1 Local push 
 UILocalNotification *noti = [[[UILocalNotification alloc] init] autorelease];
 if (noti) {
  // Set push time 
  noti.fireDate = date;//=now
  // Set the time zone 
  noti.timeZone = [NSTimeZone defaultTimeZone];
  // Set the repetition interval 
  noti.repeatInterval = NSWeekCalendarUnit;
  // Push the sound 
  noti.soundName = UILocalNotificationDefaultSoundName;
  // content 
  noti.alertBody = @" Push content ";
  // Displayed in the icon The number in the red circle above 
  noti.applicationIconBadgeNumber = 1;
  // Set up the userinfo  Easy to use later when you need to undo 
  NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
  noti.userInfo = infoDic;
  // Add push to uiapplication  
  UIApplication *app = [UIApplication sharedApplication];
  [app scheduleLocalNotification:noti]; 
 }

2. Receive the local push message when the program is running


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Receive a local reminder  in app"
message:notification.alertBody
 delegate:nil
 cancelButtonTitle:@" determine "
 otherButtonTitles:nil];
[alert show];
// Here, you can get through notification the useinfo Dry, 1 Something you want to do 
application.applicationIconBadgeNumber -= 1;

}

3. Cancel one local push


UIApplication *app = [UIApplication sharedApplication];
 // Gets the local push array 
 NSArray *localArr = [app scheduledLocalNotifications];
 
 // Declare 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 the same is found that already exists key The push 
  if (!localNoti) {
   // There is no   Initialize the 
   localNoti = [[UILocalNotification alloc] init];
  }

  

  if (localNoti && !state) {
   // Don't push   Cancel the push 
   [app cancelLocalNotification:localNoti];
   [localNoti release];
   return;
  }

}

4. There are two ways to cancel the local notice of registration: one is to cancel the specified notice, and the other is to cancel all the registration notices:


[[UIApplication sharedApplication] cancelLocalNotification:localNotification]; 
[[UIApplication sharedApplication] cancelAllLocalNotification]; 

5. The four notification types of iOS5

5.1. Banners (Banner)

Banner notifications, a new feature in iOS5, are bars that appear at the top of the screen and disappear automatically after a few seconds. 1 banner notification displays the small icon of the application (29 in a lower split screen) & times; Icon of 29, high score screen shows 58 & times; 58 icon), the name of the program and the content of the notification. Small ICONS help users see which apps are alerting them immediately.

5.2. Reminder (Alert)

The notification does not automatically disappear and requires the user to interact with it to close it. Designers need to design the specific content of the notification, and sometimes action button design title. The entire background style of the notification, including the buttons inside, is immutable, so designers and developers should not get creative here. Android, due to its openness, allows us to use controls of our own design, while iOS probably did this to keep the UI style 1 stylistic.

5.3. Marking (Badge)

Tag notifications are the red oval markers that appear in the upper right corner of the program icon, and the number displayed inside indicates the number of notifications that need to be processed by the user. Likewise, the color, shape, and size of the tag cannot be changed. App Store has the number of updated applications, and the number of unread messages received in Mail is notified to the user with a tag.

5.4. Voice (Sound)

Sound prompts are also one of the iOS notifications, which are customizable and can be used with the previous three notification types.

6. Local notifications and push notifications

The iOS application uses local notifications or push notifications to remind users:

6.1. Local notifications

Local notifications for the application are 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 an ES136en-ES137en list app, you can send out a local notification reminding users that a meeting is about to start.

6.2 push notifications

The app's push notification goes like this: The app'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 with the app installed.

To use push notifications, you must first register with Apple's Push notifications service which types of notifications you want to use. For example, the following 1 code indicates that you have registered for both tagged and voiced notifications:


- (void)applicationDidFinishLaunching:(UIApplication *)app {// other setup tasks here ... .
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound)];}

How do I use various notifications

An application that uses local notifications can use banners, reminders, tags, and sounds, but an application that uses push notifications needs to register the notification type to be used first.

Note the following when using notifications:

1. Make sure the content of the tag is up to date

2. Do not send multiple notifications for the same event

3. The notification should not contain the name of the application

For markup notifications, the markup will disappear automatically when all matters are resolved.

In banners and reminders, as well as in the notification center at the top, the iOS system automatically displays the name of the application in the message, so there is no need to include the name of app when designing the content of the notification.

When designing the content of the notification, you should also note:

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

2. Keep it short, preferably no longer than two lines. Long messages are difficult to read quickly, and scroll bars are inevitable

3. Use sentence capitalization (ES180en-ES181en capitalization) and proper punctuation, and use full stops at the end of the first word, as usual

About "Reminders"

A reminder may contain between one and two buttons. For reminders with two buttons, place the off button on the left and action button on the right.

Clicking the button on the left will turn off the reminder, and clicking the button on the right will turn off the reminder and open the app.

If there are only 1 button, the button should be 1 OK button.

Clicking the OK button also turns off the alert, not open the app.

For reminder notifications, it should be noted when designing action button title:

title can accurately describe the action of opening an application. Click the Play button, for example, to open the game, and the user can play it right away

2. Use header capitalization (title-ES207en capitalization, capitalizing the first letter of each word)

Keep it short

When you receive a new notification while on the lock screen, action button title is displayed in the message "slide to view", replacing the word "view" and converting all letters to lowercase.

About "Voice"

As for the voice prompt, the voice can attract users' attention when they are not using the phone, which can avoid users missing important information.

We can use the built-in sound of the system, or we can customize the sound. If you're using a custom sound, make sure it's short and well-crafted.

About the "Notice Board"

As long as the screen is not locked, you can slide down from the top of the screen to open the notification center.

If it is in the game, such as fruit ninja this kind of game, in order to avoid the finger accidentally slip to the outside of the screen and opened the notification center and iOS5 increase the difficulty to open the notification center: first, from the top of the screen down with similar antiskid lines under the brace, appear under the brace after drag and drop down again, can only open the notification center, otherwise the brace fold automatically after a few seconds. The skid strip design here helps new users quickly build a mental model of a drop-down notification center.

OS5 organizes the notifications in the drop-down notification bar and combines them with the 1 app notifications in chronological order starting at 1. To delete an app notification message, click the "X" button on the right and the "X" button becomes "Clear". The user can only remove all notifications under this app by clicking clear again. The two-step operation reduces the chance of missed deletions, and the interactive form is much friendlier than the usual modal window with two confirmations popping up.


Related articles: