iOS本地通知

作者: oking | 来源:发表于2017-05-15 23:12 被阅读20次

Talk is cheap,show me the code.

- (void)configLocalNotification {
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    localeDate = [date dateByAddingTimeInterval:5];
    NSLog(@"localeDate : %@",localeDate);
    localNotif.fireDate = localeDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertAction = NSLocalizedString(@"View Details", nil);
    localNotif.alertBody = @"alert body";
    localNotif.repeatInterval = kCFCalendarUnitMinute;
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    // 自定义通知声音
    localNotif.soundName = @"Your ring name";
    localNotif.applicationIconBadgeNumber = 0;
    
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"stringID" forKey:@"id"];
    localNotif.userInfo = infoDict;
    
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

以上为创建本地通知。

- (void)registerLocalNotificaiton {
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

要想接收到通知,需要在-application:didFinishLaunchingWithOptions:中调用以上方法。

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
    NSLog(@"notif : %@",notif);
}

用户滑动通知调起App后会走以上方法,App启动后需要做的操作可以在此方法中处理。

如果想要自定义通知声音需要注意以下几点:

  • 音频数据格式
  • 音频在工程中的存放位置

自定义的通知声音支持4种音频数据格式:

  • Linear PCM
  • MA4 (IMA/ADPCM)
  • µLaw
  • aLaw
    文件后缀为aiff、wav、caf。
    音频文件一定要放在app的mainBundle目录中,否则会播放系统默认的声音。可以在自己工程的Build Phases - Copy Bundle Resources中查看是否有自己想要播放的声音文件,没有加上即可。

相关文章

  • iOS 本地推送通知

    本地推送通知 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略。 a...

  • iOS14开发- 通知

    iOS 中的通知主要分为 2 种,本地通知和远程通知。 本地通知 使用步骤 导入UserNotifications...

  • ios10新特性-UserNotification

    ios10新特性-UserNotification 引言:iOS的通知分本地通知和远程通知,iOS10之前采用的是...

  • iOS UserNotifications通知管理---本地通知

    iOS UserNotifications通知管理---本地通知篇 iOS 10对以前混乱的和通知相关的API进行...

  • IOS的通知

    通知详解 简书-iOS10 推送通知 UserNotifications iOS10本地通知UserNotifi...

  • iOS推送通知概览

    iOS推送通知概览 一、响应推送(本地通知 和 远程通知 都合适) iOS 10 以前 1. UIUserNoti...

  • 关于iOS通知那些事

    一、概述 通知分为本地通知和远程推送通知,iOS10中对于通知这一块改变较大,本文主要针对iOS10的通知,iOS...

  • iOS 推送通知及通知扩展

    级别: ★★☆☆☆标签:「iOS 本地推送」「iOS 远程推送」「iOS通知扩展」作者: dac_1033审校: ...

  • iOS消息通知产品设计

    ios消息类型有本地通知、推送消息以及系统消息。 消息类型: 本地通知:是由ios设备生成并发布的,无论应用程序是...

  • 通知及Block传值代码示例

    通知 在IOS中,主要有广播通知(broadcast notification)、本地通知(local notif...

网友评论

    本文标题:iOS本地通知

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