iOS Development---Local notifications (uilocalnotification)

Source: Internet
Author: User

iOS Development---Local notifications (uilocalnotification)Tags: iOS local notifications2016-07-12 10:28 1437 people read comments (0) favorite reports Classification:IOS (+)

Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.

The notification mechanism in iOS is also called a messaging mechanism, which consists of two categories: a local notification, and a push notification, also known as a remote notification.
Local notifications are triggered by a local app, which is a form of notification based on time behavior, such as alarm timing, to-do reminders, or when an app is not used for a period of time and is usually prompted to use the app, etc. are local notifications. Creating a local notification is usually divided into the following steps:

    • Create a uilocalnotification.
    • Sets the time firedate for processing notifications.
    • Configure the contents of the Notification: notification body, notification sound, icon number, and so on.
    • Configure custom data parameter userinfo for notification delivery (this step is optional).
    • Call notifications, you can use Schedulelocalnotification: Schedule a notification as scheduled, or use Presentlocalnotificationnow to call notifications immediately.

Simple implementation of a local notification application, the specific code is as follows:
In Appdelegate:

#import"AppDelegate.h"#import"ViewController.h"@interfaceAppdelegate ()@end@implementationappdelegate-(BOOL) Application: (UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {_window = [[UIWindow Alloc]initwithframe:[uiscreen Mainscreen]. bounds]; _window. backgroundcolor = [Uicolor colorwithred:249/255.0 Green:249/255.0 Blue:249/255.0 Alpha:1.0];Set the global navigation bar style and color [[Uinavigationbar appearance] Setbartintcolor:[Uicolor colorwithred:23/255.0 Green:180/255.0 Blue:237/255.0 Alpha:1]]; [[Uinavigationbar appearance] setbarstyle:uibarstyleblack]; Viewcontroller *VC = [[Viewcontroller alloc]init]; _window. Rootviewcontroller = VC; [_window makekeyandvisible];If you have received authorization to send a notification, create a local notification, or request authorization (note: If you do not request authorization in the settings there is no corresponding notification settings item, that is, if the request has never been sent, even through the settings can not open the message allowed setting)if ([[UIApplication Sharedapplication]currentusernotificationsettings]. Types! = Uiusernotificationtypenone) {[Self addlocalnotification]; }else{[[UIApplication Sharedapplication] registerusernotificationsettings:[uiusernotificationsettings settingsForTypes: Uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound Categories:Nil]]; }ReturnYES;}#pragma mark-**************** is executed after the user registration notification method has been called (that is, after the Registerusernotificationsettings: method is executed)-(void) Application: (UIApplication *) Application didregisterusernotificationsettings: (Uiusernotificationsettings *) notificationsettings{if (notificationsettings. Types! = Uiusernotificationtypenone) {[Self addlocalnotification]; }}#pragma mark-**************** to set message information after entering the foreground-(void) Applicationwillenterforeground: (UIApplication *) application {[[UIApplication Sharedapplication]setapplicationiconbadgenumber:0];Go to foreground to cancel app message icon}#pragma mark-**************** private methodAdd Local Notifications-(void) addlocalnotification{Defining local Notification objectsUilocalnotification *notification = [[Uilocalnotification Alloc]init];Set call time notification. firedate = [NSDate Datewithtimeintervalsincenow:10.0];Notification trigger time, after 10s notification. Repeatinterval =2;Notification Repeat CountSet the notification properties notification. Alertbody = @"How do you Want me to---joker Xue, click to see-----";Notification Body Notification. Applicationiconbadgenumber =1;The number of unread messages displayed in the upper-right corner of the application notification. alertaction = @"Open App";Sliding action tips for the Standby interface notification. Alertlaunchimage = @"Default";Click the notification to open the app when the start picture, here to use the program to start the picture//notification.soundname=uilocalnotificationdefaultsoundname;//the sound that is played when the notification is received, the default message sound Notification[email protected] "MSG.CAF"; //notify sound (requires real machine to hear sound) //set User information Notification "id": @1,@ "user": @< Span class= "hljs-string" > "XF"}; //call notification [[uiapplication Sharedapplication] Schedulelocalnotification:notification];}  #pragma mark-**************** remove local notifications and remember to remove when this notification is not required-(uiapplication Sharedapplication] Cancelalllocalnotifications];}  @end             
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • ,
    • ,
    • ,
    • up-
    • -
    • +
    • -
    • +
    • *
    • +
    • ,
    • ,
    • +
    • $
    • "
    • " "
    • "
    • "
    • " "
    • "
    • "
    • "
    • "
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • ,
    • ,
    • ,
    • up-
    • -
    • +
    • -
    • +
    • *
    • +
    • ,
    • ,
    • +
    • $
    • "
    • " "
    • "
    • "
    • " "
    • "
    • "
    • "
    • "

Note:
-the notification type must be registered before using the notification, and if the user does not allow the application to send notifications, the notification cannot be sent later unless the user manually opens the notification to iOS settings.
-local notifications are uniformly scheduled by the operating system and can only be notified if the app exits the background or shuts down.
The sound of the notification is played by the iOS system, and the format must be one of the linear PCM, MA4 (IMA/ADPCM), Μlaw, Alaw, and the playback time must be within 30s, otherwise the system sound will be replaced, and the custom sound file must be placed in main boundle.
-The number of local notifications is limited, and most recent local notifications can be up to 64, which is ignored by the system.
-If you want to remove a local notification you can call UIApplication's cancellocalnotification: or cancelalllocalnotifications to remove the specified notification or all notifications.




Top
0
Step

iOS Development---Local notifications (uilocalnotification)

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.