iOS触发本地推送,实现自定义推送。

//创建通知
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.title = @"戒指断开通知";
    content.subtitle = @"";
    content.body = @"您的戒指已断开,请重新连接";
    content.badge = @1;//角标数
    content.categoryIdentifier = @"categoryIdentifier";
    content.launchImageName=@"sc";
    //声音设置 [UNNotificationSound soundNamed:@"sound.mp3"] 通知文件要放到bundle里面
    UNNotificationSound *sound = [UNNotificationSound defaultSound];
    content.sound = sound;
    
    //添加附件
    //maxinum 10M
    NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"app480" ofType:@"png"];
    //maxinum 5M
//    NSString *audioFile = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
    //maxinum 50M
    //NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];

    UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"iamgeAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil];
//    UNNotificationAttachment *audioAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"audioAttachment" URL:[NSURL fileURLWithPath:audioFile] options:nil error:nil];
    //添加多个只能显示第一个
    content.attachments = @[imageAttachment];
     
    /** 通知触发机制
     Trigger 设置本地通知触发条件,它一共有以下几种类型:
     UNPushNotificaitonTrigger 推送服务的Trigger,由系统创建
     UNTimeIntervalNotificationTrigger 时间触发器,可以设置多长时间以后触发,是否重复。如果设置重复,重复时长要大于60s
     UNCalendarNotificationTrigger 日期触发器,可以设置某一日期触发
     UNLocationNotificationTrigger 位置触发器,用于到某一范围之后,触发通知。通过CLRegion设定具体范围。
     */
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
    
    //创建UNNotificationRequest通知请求对象
    NSString *requestIdentifier = @"requestIdentifier";
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:content trigger:trigger];
    
    //将通知加到通知中心
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        
    }];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值