我有这种奇怪的情况,我的应用支持iOS7及以上版本。它做了什么,它通过使用静默通知启用Remote notifications
。
我知道上面的iOS7和iOS8有不同的逻辑来处理通知。我这样做了:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
这里的通知收到
{
aps = {
"content-available" = 1;
};
}
它的作用是,app接收静默通知,然后设置localNotification,见下文:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.alertBody = @"testing";
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
所有它都适用于iOS8和iOS9的后台模式和前台模式。当应用程序位于前台时,它将触发didReceiveLocalNotification
。
但是当我在iOS7中进行测试时,如果应用程序处于后台模式,则无效。我试图弄清楚这是如何发生的,而其他操作系统工作正常。我在应用程序打开时进行了测试,它确实收到了推送通知,并且didReceiveLocalNotification
被触发了。但是当进入后台时,没有任何事情发生(没有本地推送通知)。
答案 0 :(得分:0)
您没有提及启用后台提取功能。您是否已在应用功能中启用它?
并在AppDelegate.m
中设置此项- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
这是你的委托方法
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
// do your thing here. maybe calling a view controller class or stuff
}
但如果你不进行后台获取,请尝试这样做:
{
aps = {
"content-available" : 1,
"sound" : ""
};
}
祝你好运!
答案 1 :(得分:0)
我不确定这个问题,但也许您可以尝试更改respondsToSelector。请参阅以下内容:
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
答案 2 :(得分:0)
正如本文所述,aps需要包含priority
才能在iOS7中运行。
aps = {
badge = 7;
"content-available" = 1;
priority = 5;
};