Easy access to iOS's local messaging push

  • 2020-12-20 03:46:47
  • OfStack

First, we need to understand the concept that the local notification is an UILocalNotification class, which is quite different from the NSNotificationCenter notification center of the system.

1. What can we do with local notification

Notifications, in fact, are one function managed by the IOS system. For example, some background applications do an activity that needs to be processed by us, applications that have quit will remind us to call at a certain time, etc. If a notification is registered, the system will send us a message when the notification is triggered. As a result, we were able to add notifications to our APP via the system, and they were widely used. For example, the alarm category app, which has a similar function of checking in on time. Now, let's learn how to register and set up a local notification.

2. Know the UILocalNotification class

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

Set the time at which the system will send the notification (if it is past time or 0, the notification will be issued immediately)

@property(nonatomic,copy) NSDate *fireDate;

Set the time zone

@property(nonatomic,copy) NSTimeZone *timeZone;

Set periodic notifications

@property(nonatomic) NSCalendarUnit repeatInterval;

The NSCalendarUnit object is an enumeration that sets the period of notification


typedef NS_OPTIONS(NSUInteger, NSCalendarUnit) {
 NSCalendarUnitEra  = kCFCalendarUnitEra,
 NSCalendarUnitYear  = kCFCalendarUnitYear,
 NSCalendarUnitMonth  = kCFCalendarUnitMonth,
 NSCalendarUnitDay  = kCFCalendarUnitDay,
 NSCalendarUnitHour  = kCFCalendarUnitHour,
 NSCalendarUnitMinute  = kCFCalendarUnitMinute,
 NSCalendarUnitSecond  = kCFCalendarUnitSecond,
 NSCalendarUnitWeekday  = kCFCalendarUnitWeekday,
 NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal,
 }

Set a calendar table for periodic notification references

@property(nonatomic,copy) NSCalendar *repeatCalendar;

The following two functions are new to IOS8 and send notifications when a user enters or leaves a region 1

@property(nonatomic,copy) CLRegion *region;

Set whether the zone detection notification is repeated (if it is YES, it will be sent every time it enters or leaves, otherwise it will only be sent once)

@property(nonatomic,assign) BOOL regionTriggersOnce;

Sets the body content of the notification

@property(nonatomic,copy) NSString *alertBody;

Whether to hide the slide start button

@property(nonatomic) BOOL hasAction;

Set the prompt text to slide open

@property(nonatomic,copy) NSString *alertAction;

Set the startup image that starts after clicking on the notification

@property(nonatomic,copy) NSString *alertLaunchImage;

The following method is a new method for IOS8, which is the interface to iwatch and the short title of the notification

@property(nonatomic,copy) NSString *alertTitle;

The system sound played when a notification is received

@property(nonatomic,copy) NSString *soundName;

Set the application Icon header number

@property(nonatomic) NSInteger applicationIconBadgeNumber;

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

@property(nonatomic,copy) NSDictionary *userInfo;

Note: This string is the default prompt

NSString *const UILocalNotificationDefaultSoundName;

3. Design process of local notification

First of all, if we want APP to implement the local notification function, we must be authorized by the user. The following code is implemented in Appdelegate:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 // Override point for customization after application launch.
 // Add local notification directly if you are already authorized, otherwise 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, in which we implement the processing logic


-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
 if (notificationSettings.types!=UIUserNotificationTypeNone) {
 [self addLocalNotification];
 }
}

Ways to add local notifications:


-(void)addLocalNotification{
 // Define a local notification object 
 UILocalNotification *notification=[[UILocalNotification alloc]init];
 // Set the call time 
 notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:0];// Immediately triggered 
 // Set notification properties 
 notification.alertBody=@"HELLO This is a local notice !"; // Notice the body 
 notification.applicationIconBadgeNumber=1;// The number of messages displayed in the upper right corner of the application icon 
 notification.alertAction=@" Open the application "; // Slide action prompt on standby interface  
 notification.soundName=UILocalNotificationDefaultSoundName;// The sound played when receiving a notification, the default message sound 
 // Call the notification 
 [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

After implementing the above three steps, the local notification has been sent and received almost completely. There are 1 more details we need to consider:

After the application enters the foreground, remove the header on Icon:


-(void)applicationWillEnterForeground:(UIApplication *)application{
 [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];// Go to the foreground and cancel the application message icon 
}

When this notification is no longer needed, clear it

[[UIApplication sharedApplication] cancelAllLocalNotifications];

4. Get the user parameter dictionary in the notification

Above, we mentioned 1 parameter

@property(nonatomic,copy) NSDictionary *userInfo;

We can set this parameter when we register a notification and then get it when we receive a notification using the get method, but there are two scenarios:

1. When OUR APP enters the front desk at the front desk or in the background

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;

This is the method that APP calls when it is notified to enter the foreground or the background

2. If our APP is in the closed state

If this is the case, we can only take the argument we want from launchOptions of the following function

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

Code examples are as follows:


 // Receive notification parameters  
 UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
 NSDictionary *userInfo= notification.userInfo;

This article has been sorted into iOS push tutorial, welcome to learn to read.


Related articles: