1.iOS中的系统通知 自定义推送声音

iOS系统可支持本地通知和远程通知,一个通知在客户端收到的时候可能是一个通知窗体,可能会播放一段通知声音,还有可能在程序图标上增加一个数字,还有可能三者皆有。

本文描述ios系统中计划local notification,注册 remote notification,以及处理 local和remote notification的步骤。本文中客户端API中notification推送指的就是remote notfication的推送

第一个知识点:准备个人定制音频作为提示音,

请注意下面四个小问题-----

1,
系统能播放的四种音频数据格式
Linear PCM
MA4 (IMA/ADPCM)
?Law
aLaw
对应的后缀名可以是aiff, wav, or caf file. Then, 
2, 可以用afconvert来转换音频,例如把16位的线性PCM系统音频格式文件 Submarine.aiff 转换成IMA4音频,存为.CAF文件。
在终端执行即可
afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v
3, 如何确定音频格式呢。
打开QuickTime Player-> Movie menu->Show Movie Inspector 
4, 个人定制音频必须是30s以下。否则就播放默认的声音了。

第二个知识点:预定一个Local Notification
需要了解下面5个步骤
1, Allocate 和 initialize 一个 UILocalNotification对象。
2, fireDate属性赋值
3, 设置别的几个体型要素 提示框,提示音,图标上的提示数字。也可以带别的个性化数据:通过userInfo带出去。
4, 然后Schedule这个通知。通过UIApplication.scheduleLocalNotification来预定执行或者立马执行presentLocalNotificationNow:
5, 也可以取消这个通知。用这个方法:cancelLocalNotification和cancelAllLocalNotifications这个方法

看下代码
/

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/如何创建设定一个Notification
- ( void )scheduleNotificationWithItem:(ToDoItem *)item interval:( int )minutesBefore {
     NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
     NSDateComponents *dateComps = [[NSDateComponents alloc] init];
     [dateComps setDay:item.day];
     [dateComps setMonth:item.month];
     [dateComps setYear:item.year];
     [dateComps setHour:item.hour];
     [dateComps setMinute:item.minute];
     NSDate *itemDate = [calendar dateFromComponents:dateComps];
     [dateComps release];
  
     UILocalNotification *localNotif = [[UILocalNotification alloc] init];
     if (localNotif == nil)
         return ;
     localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
     localNotif.timeZone = [NSTimeZone defaultTimeZone];
  
     localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@ "%@ in %i minutes." , nil),
          item.eventName, minutesBefore];
     localNotif.alertAction = NSLocalizedString(@ "View Details" , nil);
  
     localNotif.soundName = UILocalNotificationDefaultSoundName;
     localNotif.applicationIconBadgeNumber = 1;
  
     NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];
     localNotif.userInfo = infoDict;
  
     [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
     [localNotif release];
}
 
 
 
 
//程序运行在后台时候如何提交一个UILocalNotification。
- ( void )applicationDidEnterBackground:(UIApplication *)application {
     NSLog(@ "Application entered background state." );
     // bgTask is instance variable
     NSAssert(self->bgTask == UIInvalidBackgroundTask, nil);
  
     bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
         dispatch_async(dispatch_get_main_queue(), ^{
             [application endBackgroundTask:self->bgTask];
             self->bgTask = UIInvalidBackgroundTask;
         });
     }];
  
     dispatch_async(dispatch_get_main_queue(), ^{
         while ([application backgroundTimeRemaining] > 1.0) {
             NSString * friend = [self checkForIncomingChat];
             if ( friend ) {
                 UILocalNotification *localNotif = [[UILocalNotification alloc] init];
                 if (localNotif) {
                     localNotif.alertBody = [NSString stringWithFormat:
                         NSLocalizedString(@ "%@ has a message for you." , nil),  friend ];
                     localNotif.alertAction = NSLocalizedString(@ "Read Message" , nil);
                     localNotif.soundName = @ "alarmsound.caf" ;
                     localNotif.applicationIconBadgeNumber = 1;
                     [application presentLocalNotificationNow:localNotif];
                     [localNotif release];
                     friend = nil;
                     break ;
                 }
             }
         }
         [application endBackgroundTask:self->bgTask];
         self->bgTask = UIInvalidBackgroundTask;
     });
  
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值