iOS Client Local Push Implementation Code
- 2021-07-06 11:54:55
- OfStack
In this paper, we share the specific code of iOS local push for your reference. The specific contents are as follows
First, create a global local notification object and pop-up box
// Pop up local messages
@property(nonatomic,strong)UILocalNotification *localNotification;
@property(nonatomic,strong)UIAlertController *alertcontrol;
The second implementation in the code is as follows:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
// Judge whether the program is in an open state, that is, whether it is running in the foreground
if (application.applicationState == UIApplicationStateActive) {
// If the message has been received before and the user has not processed the pop-up box, close the original pop-up box
if (self.localNotification) {
[self.alertcontrol dismissViewControllerAnimated:YES completion:nil];
}
[self bulidLocationNotification:application userinfo:userInfo];
}else{
// Judge whether the program is not open, that is, whether it is running in the background or closed, and set the corner mark by Aurora push
if ([application applicationIconBadgeNumber]>0) {
[JPUSHService setBadge:[application applicationIconBadgeNumber]];
}
}
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@" Receive notification :%@", [self logDic:userInfo]);
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
// Popup message
if (self.localNotification) {
self.alertcontrol = [UIAlertController alertControllerWithTitle:@"" message:self.localNotification.alertBody preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@" Determine " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.localNotification = nil;
self.alertcontrol = nil;
}];
[self.alertcontrol addAction:action];
[self.window.rootViewController presentViewController:self.alertcontrol animated:YES completion:nil];
}
}
// Create a local push message
-(void)bulidLocationNotification:(UIApplication *)application userinfo:(NSDictionary *)userInfo{
self.localNotification = [[UILocalNotification alloc]init];
self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
self.localNotification.alertBody = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
self.localNotification.alertAction = @" Determine ";
self.localNotification.soundName = @"sound.caf";
self.localNotification.userInfo = userInfo;
[application presentLocalNotificationNow:self.localNotification];
}