【问题标题】:Silent push notification doesn't work on iOS7 in background mode在后台模式下,静默推送通知在 iOS7 上不起作用
【发布时间】: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


【解决方案1】:

您没有提到启用后台提取。您是否在您的 App Capabilities 中启用了它?

并在您的 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" : ""
    };
}

祝你好运!

【讨论】:

  • 我没有启用它,因为我不使用application:performFetchWithCompletionHandler:。顺便说一句,我重申 iOS8 和 iOS9 的工作原理。 :(
  • 任何官方声明添加声音键?与此同时,我会试试这个。
【解决方案2】:

我不太确定这个问题,但也许您可以尝试更改 respondsToSelector。参考以下:

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])

【讨论】:

    【解决方案3】:

    正如本文所述,aps 需要包含 priority 才能在 iOS7 中工作。

     aps =     {
            badge = 7;
            "content-available" = 1;
            priority = 5;
        }; 
    

    检查这个:https://stackoverflow.com/a/23917593/554740

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 2021-05-02
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      相关资源
      最近更新 更多