美文网首页
iOS 通知

iOS 通知

作者: 一个半吊子工程师 | 来源:发表于2020-09-07 15:42 被阅读0次

本地推送通知(LocalNotification)
概念:由APP本身给应用程序推送消息,不需要服务器的支持
常见场景:时钟中秒表和计时器提醒你时间等等

远程推送通知(RemoteNotification)
概念:由服务器推送消息给用户,需要服务器的支持
常见场景:微信提醒新消息

本地推送通知(LocalNotification)

AppDelegate.m

#import "AppDelegate.h"
@interface AppDelegate ()
@end

@implementation AppDelegate

#pragma mark - 应用代理方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];

    //如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)
    if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {
        [self addLocalNotification];
    }else {
        [[UIApplication sharedApplication]registerUserNotificationSettings:  [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound  categories:nil]];
    }
    return YES;
}
#pragma mark 调用过用户注册通知方法之后执行(也就是调用完registerUserNotificationSettings:方法之后执行)

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

#pragma mark 进入前台后设置消息信息
-(void)applicationWillEnterForeground:(UIApplication *)application{
  [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标
}

#pragma mark - 私有方法
#pragma mark 添加本地通知
-(void)addLocalNotification{
  //定义本地通知对象
  UILocalNotification *notification=[[UILocalNotification alloc]init];
  //设置调用时间
  notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10.0];//通知触发的时间,10s以后
  notification.repeatInterval=2;//通知重复次数
  //notification.repeatCalendar=[NSCalendar currentCalendar];//当前日历,使用前最好设置时区等信息以便能够自动同步时间
  //设置通知属性
  notification.alertBody=@"本地通知测试,是否立即体验?"; //通知主体
  notification.applicationIconBadgeNumber=1;//应用程序图标右上角显示的消息数

  notification.alertAction=@"进入应用"; //待机界面的滑动动作提示
  notification.alertLaunchImage=@"Default";//通过点击通知打开应用时的启动图片,这里使用程序启动图片

  notification.soundName=UILocalNotificationDefaultSoundName;//收到通知时播放的声音,默认消息声音
 // notification.soundName=@"notification.caf";//通知声音(需要真机才能听到声音)
  //设置用户信息
  notification.userInfo=@{@"notificationId":@1,@"notificationUser":@"xxxx"};//绑定到通知上的其他附加信息
  //调用通知
  [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

#pragma mark 移除本地通知,在不需要此通知时记得移除
-(void)removeNotification{
  [[UIApplication sharedApplication] cancelAllLocalNotifications];
}
@end

相关文章

网友评论

      本文标题:iOS 通知

      本文链接:https://www.haomeiwen.com/subject/ynqlektx.html