您的位置:首页 > 移动开发 > IOS开发

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

2015-09-01 16:51 609 查看
iOS系统可支持本地通知和远程通知,一个通知在客户端收到的时候可能是一个通知窗体,可能会播放一段通知声音,还有可能在程序图标上增加一个数字,还有可能三者皆有。本文描述ios系统中计划localnotification,注册remotenotification,以及处理local和remotenotification的步骤。本文中客户端API中notification推送指的就是remotenotfication的推送第一个知识点:准备个人定制音频作为提示音,请注意下面四个小问题-----1,系统能播放的四种音频数据格式LinearPCMMA4(IMA/ADPCM)?LawaLaw对应的后缀名可以是aiff,wav,orcaffile.Then,2,可以用afconvert来转换音频,例如把16位的线性PCM系统音频格式文件Submarine.aiff转换成IMA4音频,存为.CAF文件。在终端执行即可afconvert/System/Library/Sounds/Submarine.aiff~/Desktop/sub.caf-dima4-fcaff-v3,如何确定音频格式呢。打开QuickTimePlayer->Moviemenu->ShowMovieInspector4,个人定制音频必须是30s以下。否则就播放默认的声音了。第二个知识点:预定一个LocalNotification需要了解下面5个步骤1,Allocate和initialize一个UILocalNotification对象。2,fireDate属性赋值3,设置别的几个体型要素提示框,提示音,图标上的提示数字。也可以带别的个性化数据:通过userInfo带出去。4,然后Schedule这个通知。通过UIApplication.scheduleLocalNotification来预定执行或者立马执行presentLocalNotificationNow:5,也可以取消这个通知。用这个方法:cancelLocalNotification和cancelAllLocalNotifications这个方法看下代码/?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
/如何创建设定一个Notification
-
(
void
)scheduleNotificationWithItem:(ToDoItem
*)iteminterval:(
int
)minutesBefore
{
NSCalendar
*calendar=[NSCalendarautoupdatingCurrentCalendar];
NSDateComponents
*dateComps=[[NSDateComponentsalloc]init];
[dateComps
setDay:item.day];
[dateComps
setMonth:item.month];
[dateComps
setYear:item.year];
[dateComps
setHour:item.hour];
[dateComps
setMinute:item.minute];
NSDate
*itemDate=[calendardateFromComponents:dateComps];
[dateComps
release];
UILocalNotification
*localNotif=[[UILocalNotificationalloc]init];
if

(localNotif==nil)
return
;
localNotif.fireDate
=[itemDateaddTimeInterval:-(minutesBefore*60)];
localNotif.timeZone
=[NSTimeZonedefaultTimeZone];
localNotif.alertBody
=[NSStringstringWithFormat:NSLocalizedString(@
"%@
in%iminutes."
,
nil),
item.eventName,
minutesBefore];
localNotif.alertAction
=NSLocalizedString(@
"View
Details"
,
nil);
localNotif.soundName
=UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber
=1;
NSDictionary
*infoDict=[NSDictionarydictionaryWithObject:item.eventNameforKey:ToDoItemKey];
localNotif.userInfo
=infoDict;
[[UIApplication
sharedApplication]scheduleLocalNotification:localNotif];
[localNotif
release];
}
//程序运行在后台时候如何提交一个UILocalNotification。
-
(
void
)applicationDidEnterBackground:(UIApplication
*)application{
NSLog(@
"Application
enteredbackgroundstate."
);
//
bgTaskisinstancevariable
NSAssert(self->bgTask
==UIInvalidBackgroundTask,nil);
bgTask
=[applicationbeginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(),
^{
[application
endBackgroundTask:self->bgTask];
self->bgTask
=UIInvalidBackgroundTask;
});
}];
dispatch_async(dispatch_get_main_queue(),
^{
while

([applicationbackgroundTimeRemaining]>1.0){
NSString
*
friend


=[selfcheckForIncomingChat];
if

(
friend
)
{
UILocalNotification
*localNotif=[[UILocalNotificationalloc]init];
if

(localNotif){
localNotif.alertBody
=[NSStringstringWithFormat:
NSLocalizedString(@
"%@
hasamessageforyou."
,
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;
});
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: