Easily handle IOS local message push

First, we need to understand a concept. The local notification here is the uilocalnotification class, which is completely different from the nsnotificationcenter notification center of the system.

1、 What can we do through local notification

Notification is actually a function managed by the IOS system. For example, some background applications need us to handle an activity, and applications that have exited remind us to wake up at a certain time. If a notification is registered, the system will send us a message when the notification is triggered. Therefore, we can add the function of notifying users to our app through the system, and it is widely used. For example, there are many kinds of applications with similar functions of checking in on time. Next, let's introduce how to register and set up a local notification.

2、 Understanding the uilocalnotification class

As the name suggests, this class is the local notification class we need to use. Let's first look at its properties:

Set the time when the system sends the notification (if it is the past time or 0, the notification will be sent immediately)

@property(nonatomic,copy) NSDate *fireDate;

Set time zone for time

@property(nonatomic,copy) NSTimeZone *timeZone;

Set periodic notifications

@property(nonatomic) NSCalendarUnit repeatInterval;

The nscalendarunit object is an enumeration that sets the notification period

Set the calendar table referenced by periodic notification

@property(nonatomic,copy) NSCalendar *repeatCalendar;

The following two functions are the new functions of ios8. They send notifications when users enter or leave an area

@property(nonatomic,copy) CLRegion *region;

Set whether the area detection notification is repeated (if yes, it will be sent every time it goes in and out, otherwise it will be sent only once)

@property(nonatomic,assign) BOOL regionTriggersOnce;

Set the body content of the notification

@property(nonatomic,copy) NSString *alertBody;

Hide slide start button

@property(nonatomic) BOOL hasAction;

Sets the prompt text for sliding open

@property(nonatomic,copy) NSString *alertAction;

Set the startup image to start after clicking the notification

@property(nonatomic,copy) NSString *alertLaunchImage;

The following method is a new method of ios8. It is the interface of Iwatch and the short title of notification

@property(nonatomic,copy) NSString *alertTitle;

System tone played when notification is received

@property(nonatomic,copy) NSString *soundName;

Set application icon header number

@property(nonatomic) NSInteger applicationIconBadgeNumber;

A user dictionary that can be used to pass notification message parameters

@property(nonatomic,copy) NSDictionary *userInfo;

Note: this string is the system default prompt tone

NSString *const UIlocalnotificationDefaultSoundName;

3、 Design process of local notification

First of all, if you want our app to realize the local notification function, you must be authorized by the user. Implement the following code in appdelegate:

When the user clicks allow or disallow, the following proxy method will be executed, in which we implement the processing logic

How to add local Notifications:

After realizing the above three steps, the sending and receiving of local notifications have been basically completed. There are still some details we need to consider:

After the application enters the foreground, clear the header on icon:

Clear this notification when it is no longer needed

[[UIApplication sharedApplication] cancelAlllocalnotifications];

4、 Gets the user parameter dictionary in the notification

Above, we mentioned a parameter

@property(nonatomic,copy) NSDictionary *userInfo;

We can set this parameter when registering the notification, and then use the get method when receiving the notification, but there are two cases:

1. If our app is at the front desk or when the background enters the front desk

This article has been sorted into IOS push tutorial. You are welcome to learn and read it.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>