0
我有一個voip應用程序。現在UIlocalNotification沒有開始後臺任務
我在我的應用程序中使用UILocalNotification
來在後臺應用程序中警告來電通知。
當應用程序在後臺時,獲得來電後會調用以下函數。
-(void)showLocalNotification:(NSNotification *)notification {
NSDictionary *dic = notification.userInfo;
NSString *msg = [dic objectForKey:@"msg"];
NSString *sound = [dic objectForKey:@"sound"];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *_localNotification = [[UILocalNotification alloc]init];
//setting the fire dat of the local notification
_localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
//setting the time zone
_localNotification.timeZone = [NSTimeZone defaultTimeZone];
//setting the message to display
_localNotification.alertBody = msg;
//default notification sound
if ([sound length]==0) {
_localNotification.soundName = UILocalNotificationDefaultSoundName;
} else {
_localNotification.alertAction = NSLocalizedString(@"Receive", nil);
_localNotification.soundName = @"myringtone.aif";
}
//displaying the badge number
_localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
//schedule a notification at its specified time with the help of the app delegate
[[UIApplication sharedApplication]scheduleLocalNotification:_localNotification];
}
該功能設置scheduleLocal通知以顯示一個警報,兩個按鈕Cancel
和Receive
其中第二後觸發。
現在的問題是:
這本地通知似乎用戶,只有當後臺任務開始如下
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
}
但是,10分鐘後後臺任務停止。然後本地通知不會顯示給用戶,雖然它會觸發(點擊後會打印日誌)
我修改了applicationDidEnterBackground
函數,如下重新啓動後臺任務以進行長時間運行。但無法。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
expirationHandler = ^{
[app endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
}
self.bgTask = [app beginBackgroundTaskWithExpirationHandler:expirationHandler];
}
現在的問題是:
有沒有什麼辦法,這將觸發本地通知,當應用程序在後臺出現用戶?
如果後臺任務是強制性的,10分鐘後如何顯示本地通知以顯示用戶?
我使用iPhone 3gs
,iPhone 4
和iPhone 5
。