远程推送基本都没啥问题,按规范来走,证书做好给服务器,本地加载上证书基本都是可以实现的。
本地通知就需要自己创建通知,发送通知,比远程多了这两步,一般的会有几个需要注意的地方:
1,iOS8-iOS10是一套发通知方法,10以后是一套方法;
2,通知可能不显示,可能会覆盖,只显示一个;
3,iOS10以前,APP在前台时收到通知不下拉自动显示(搞了很久,有实现的留言探讨下);
4,APP在前台,下拉查看通知列表时,如果此时有通知发出,程序会调用在后台打开通知的方法。如果代码里显示通知内容是进行页面跳转,这个时候就实现了页面跳转,接收几个通知,就显示几个页面;
使用通知先注册通知:在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中注册以下代码:
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
先看iOS10以后发送本地通知,代码如下:
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"title";
content.body = @"body";
content.sound = [UNNotificationSound defaultSound];
content.userInfo =
@{@"userInfo":data,@"type" : [NSString stringWithFormat:@"%ld",(long)type]};
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: [[NSUUID UUID] UUIDString] content:content trigger:nil];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
NSLog(@"成功添加推送");
}];
注意设置代理,注意设置唯一identifier,如果不设置,会覆盖,只显示一个通知。下面实现代理,在发送通知的页面实现以下代理方法:
//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {//应用处于前台时的远程推送接受
} else {//应用处于前台时的本地推送接受
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);//
}
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
if (@available(iOS 10.0, *)) {
UNNotificationContent *content = response.notification.request.content;
// 如果在上面的通知方法中设置了一些,可以在这里打印额外信息的内容,就做到监听,也就可以根据额外信息,做出相应的判断
NSData *data = [content.userInfo objectForKey:@"userInfo"];
NSString *type = [content.userInfo objectForKey:@"type"];
NSUInteger c = [type integerValue];
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
[app processNotificationData:c data:data];
} else {
// Fallback on earlier versions
}
}
第一个方法是显示通知用的,第二个是接收通知用的。可以在第二个方法进行页面操作。
再来看iOS10以前,发通知代码:
// 1.创建本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 2.设置本地通知的内容
// 2.1.设置通知发出的时间
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:3.0];
// 2.2.设置通知的内容
localNote.alertBody = @"在干吗?";
// 2.3.设置滑块的文字(锁屏状态下:滑动来“解锁”)
localNote.alertAction = @"解锁";
// 2.4.决定alertAction是否生效
localNote.hasAction = NO;
// 2.5.设置点击通知的启动图片
localNote.alertLaunchImage = @"launch.png";
// 2.6.设置alertTitle
localNote.alertTitle = @"title";
// 2.7.设置有通知时的音效
localNote.soundName = @"buyao.wav";
// 2.8.设置应用程序图标右上角的数字
NSInteger i = [UIApplication sharedApplication].applicationIconBadgeNumber;
localNote.applicationIconBadgeNumber = i +1;
// 2.9.设置额外信息
localNote.userInfo = @{@"userInfo":data,@"type" : [NSString stringWithFormat:@"%ld",(long)type]};
// 3.调用通知
[[UIApplication sharedApplication] presentLocalNotificationNow:localNote];
再来看接收通知的方法,在APPdelegate实现:
/*
应用程序在进入前台,或者在前台的时候都会执行该方法
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// 必须要监听--应用程序在后台的时候进行的跳转
if (application.applicationState == UIApplicationStateInactive) {
NSLog(@"UIApplicationStateInactive");
// 如果在上面的通知方法中设置了一些,可以在这里打印额外信息的内容,就做到监听,也就可以根据额外信息,做出相应的判断
NSData *data = [notification.userInfo objectForKey:@"userInfo"];
NSString *type = [notification.userInfo objectForKey:@"type"];
NSUInteger c = [type integerValue];
[self processNotificationData:c data:data];
}else if (application.applicationState == UIApplicationStateActive){
NSLog(@"UIApplicationStateActive");
}else{
NSLog(@"UIApplicationStateBackground");
}
}
注意,如果在didReceiveLocalNotification实现了,
application.applicationIconBadgeNumber = 0;
那通知也不会实现.
Good luck!
如果能对您有帮助,请帮忙点赞!