zoukankan      html  css  js  c++  java
  • ios APNS注册失败 本地push

    - (void)addLocalNotice:(NSString *)titlepush {

    if (@available(iOS 10.0, *)) {

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    // 标题

    content.title = titlepush;

    content.subtitle = titlepush;

    // 内容

    content.body = titlepush;

    // 声音

    // 默认声音

    // content.sound = [UNNotificationSound defaultSound];

    // 添加自定义声音

    content.sound = [UNNotificationSound soundNamed:@"Alert_ActivityGoalAttained_Salient_Haptic.caf"];

    // 角标 (我这里测试的角标无效,暂时没找到原因)

    content.badge = @1;

    // 多少秒后发送,可以将固定的日期转化为时间

    NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:10] timeIntervalSinceNow];

    // NSTimeInterval time = 10;

    // repeats,是否重复,如果重复的话时间必须大于60s,要不会报错

    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];

    /*

    //如果想重复可以使用这个,按日期

    // 周一早上 8:00 上班

    NSDateComponents *components = [[NSDateComponents alloc] init];

    // 注意,weekday默认是从周日开始

    components.weekday = 2;

    components.hour = 8;

    UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];

    */

    // 添加通知的标识符,可以用于移除,更新等操作

    NSString *identifier = @"noticeId";

    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];

    [center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {

    NSLog(@"成功添加推送");

    }];

    }else {

    UILocalNotification *notif = [[UILocalNotification alloc] init];

    // 发出推送的日期

    notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];

    // 推送的内容

    notif.alertBody = @"你已经10秒没出现了";

    // 可以添加特定信息

    notif.userInfo = @{@"noticeId":@"00001"};

    // 角标

    notif.applicationIconBadgeNumber = 1;

    // 提示音

    notif.soundName = UILocalNotificationDefaultSoundName;

    // 每周循环提醒

    notif.repeatInterval = NSCalendarUnitWeekOfYear;

    [[UIApplication sharedApplication] scheduleLocalNotification:notif];

    }

    }

    NSString *stringInfo=[NSString stringWithFormat:@"APNS error: %@", err];

    [self addLocalNotice:stringInfo];

  • 相关阅读:
    Docker是什么
    常用架构模式优缺点
    DDD领域模型自动生成?
    组合数【模板】
    组合数【模板】
    HDU 4800 Josephina and RPG【概率dp】
    HDU 4800 Josephina and RPG【概率dp】
    HDU 4424 Conquer a New Region【并查集】【思维题】
    HDU 4424 Conquer a New Region【并查集】【思维题】
    HDU 4427 Math Magic【dp+优化+滚动数组】【好题】
  • 原文地址:https://www.cnblogs.com/cxcoder/p/10773376.html
Copyright © 2011-2022 走看看