Easy fix IOS local message push _ios

Source: Internet
Author: User
Tags define local set time notification center

First, we need to understand a concept, where local notification is the Uilocalnotification class, and the system's Nsnotificationcenter Notification Center is a completely different concept.

What we can do by local notice

notifications, in fact, is a function of the iOS system management, such as some background applications do some activities need us to deal with, has exited the application at a certain time to remind us, and so on, if the notification is registered, the system will send us messages when the notification is triggered. As a result, we can use the system to add notification users to our app, and the application is very extensive. For example, noisy type of application, have on-time check-in similar function application. Next, we'll explain how to register and set up a local alert.

Second, understand the Uilocalnotification class

As the name suggests, this class is the local notification class that we need to use to see some of its properties first:

Set the time the system sends a notification (if it is past time or 0, it will immediately initiate notification)

@property (nonatomic,copy) nsdate *firedate;

Set time zone for times

@property (nonatomic,copy) Nstimezone *timezone;

Set up periodic notifications

@property (nonatomic) Nscalendarunit repeatinterval;

The Nscalendarunit object is an enumeration that sets the cycle of notifications

typedef ns_options (Nsuinteger, nscalendarunit) {
 Nscalendarunitera  = Kcfcalendarunitera,
 Nscalendarunityear  = kcfcalendarunityear,
 nscalendarunitmonth  = Kcfcalendarunitmonth,
 Nscalendarunitday  = kcfcalendarunitday,
 nscalendarunithour  = Kcfcalendarunithour,
 Nscalendarunitminute  = Kcfcalendarunitminute,
 nscalendarunitsecond  = Kcfcalendarunitsecond,
 Nscalendarunitweekday  = kcfcalendarunitweekday,
 nscalendarunitweekdayordinal = Kcfcalendarunitweekdayordinal,
 }

Set up a calendar table for periodic notification references

@property (nonatomic,copy) Nscalendar *repeatcalendar;

These two functions are a new feature of the IOS8 that sends notifications when a user goes in or leaves an area

@property (nonatomic,copy) clregion *region;

Sets whether the zone detection notification is repeated (if yes, then it will be sent without any time, or only once)

@property (nonatomic,assign) BOOL regiontriggersonce;

Set the body content of a notification

@property (nonatomic,copy) NSString *alertbody;

Do you want to hide the slide Start button

@property (nonatomic) BOOL hasaction;

Set the slide open prompt text

@property (nonatomic,copy) NSString *alertaction;

Set up a startup picture that starts after clicking the notification

@property (nonatomic,copy) NSString *alertlaunchimage;

The following method is the new method of IOS8, is the iwatch interface, the short caption of the notice

@property (nonatomic,copy) NSString *alerttitle;

The system tone that is played when the notification is received

@property (nonatomic,copy) NSString *soundname;

Set the application icon header number

@property (nonatomic) Nsinteger applicationiconbadgenumber;

User dictionaries, which can be used to pass notification message parameters

@property (nonatomic,copy) nsdictionary *userinfo;

Note: This string is the system default beep

NSString *const Uilocalnotificationdefaultsoundname;

Third, the design process of local notification

First, to enable our app to implement the local notification function, we must get the user's authorization to implement the following code in Appdelegate:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
 Override point for customization after application launch.
 If you have already been authorized, add local alerts directly, or ask for authorization if
 ([[UIApplication sharedapplication]currentusernotificationsettings].types!= Uiusernotificationtypenone) {
 [self addlocalnotification];
 } else{
 [[UIApplication sharedapplication]registerusernotificationsettings:[uiusernotificationsettings Settingsfortypes:uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound Categories:nil]];
 }
 Return YES
}

When the user clicks Allow or disallow, the following proxy method is executed, where we implement the process logic

-(void) Application: (UIApplication *) application didregisterusernotificationsettings: (Uiusernotificationsettings * ) notificationsettings{
 if (notificationsettings.types!=uiusernotificationtypenone) {
 [self Addlocalnotification];
 }

How to add local alerts:

-(void) addlocalnotification{
 //define local notification object
 uilocalnotification *notification=[[uilocalnotification alloc] INIT];
 Set call time
 notification.firedate=[nsdate datewithtimeintervalsincenow:0];//immediate trigger
 //Set notification properties
 notification.alertbody=@ "HELLO, I am the local notice Oh!"; The notification principal
 notification.applicationiconbadgenumber=1;//the number of messages displayed in the upper-right corner of the application icon
 notification.alertaction=@ "Open Application"; The sliding action of the standby interface prompts 
 notification.soundname=uilocalnotificationdefaultsoundname;//to play the sound when the notification is received, the default message sound
 //Call notification
 [[UIApplication sharedapplication] schedulelocalnotification:notification];
}

To achieve the above three steps, the issue and acceptance of local notifications have been basically completed, and there are some details we need to consider:

When applied to the foreground, clear the header on the icon:

-(void) Applicationwillenterforeground: (uiapplication *) application{
 [[UIApplication sharedapplication] setapplicationiconbadgenumber:0];//to the foreground cancel the application message icon
}

When this notification is no longer needed, clear it

[[UIApplication sharedapplication] cancelalllocalnotifications];

Iv. getting the user parameter dictionary in the notification

In the above, we mentioned a parameter

@property (nonatomic,copy) nsdictionary *userinfo;

We can set this parameter at the time of registration and then use the Get method when we receive the notification, but here are two things:

1, if our app in the foreground or backstage to enter the foreground

Copy Code code as follows:
-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification;

This method is the method that the app calls when it receives a notification in the foreground or backstage.

2, if our app is off the state

If this is the case, we can only get the arguments we want from the launchoptions of the following function

Copy Code code as follows:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions;

The code example is as follows:

 Receive notification parameters 
 uilocalnotification *notification=[launchoptions valueforkey: Uiapplicationlaunchoptionslocalnotificationkey];
 Nsdictionary *userinfo= Notification.userinfo;

This article has been organized into the "iOS push Tutorial", welcome to learn to read.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.