【发布时间】:2016-03-05 04:45:12
【问题描述】:
我有这种奇怪的情况,我的应用程序支持 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 被触发。但是当进入后台时,什么也没有发生(没有本地推送通知)。
【问题讨论】:
标签: ios ios7 notifications push-notification