iOS 8 推送通知声音不适用于此特定案例

标签 ios apple-push-notifications

我在我的 V1 应用中启用了推送通知。但是,我在为推送通知注册设备时没有使用标志 UIUserNotificationTypeSound

正如预期的那样,发送推送通知时没有播放任何声音。稍后当我添加此标志时,声音仅针对新安装播放,而不针对尽管重新注册但一直在使用 V1 的忠实用户播放。

声音在设置中默认保持关闭状态。

我可以通过编程方式解决这个问题吗?

最佳答案

查看本教程,适用于 2015 年 1 月 2 日的今天

https://documentation.appboy.com/Enabling_Message_Channels/Push_Notifications/iOS

iOS 8 以非向后兼容的方式更改了通知注册。虽然您需要支持 iOS 7 和 8(并且不接受使用 8 SDK 构建的应用程序),但您可以检查您需要的选择器并有条件地为运行版本正确调用它们。

这是 UIApplication 上的一个类别,它将把这个逻辑隐藏在一个干净的界面后面,供您在 Xcode 5 和 Xcode 6 中使用。

标题:

//Call these from your application code for both iOS 7 and 8
//put this in the public header
@interface UIApplication (RemoteNotifications)

- (BOOL)pushNotificationsEnabled;
- (void)registerForPushNotifications;

@end
Implementation:

//these declarations are to quiet the compiler when using 7.x SDK
//put this interface in the implementation file of this category, so they are
//not visible to any other code.
@interface NSObject (IOS8)

- (BOOL)isRegisteredForRemoteNotifications;
- (void)registerForRemoteNotifications;

+ (id)settingsForTypes:(NSUInteger)types categories:(NSSet*)categories;
- (void)registerUserNotificationSettings:(id)settings;

@end

@implementation UIApplication (RemoteNotifications)

- (BOOL)pushNotificationsEnabled
{
    if ([self respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        return [self isRegisteredForRemoteNotifications];
    }
    else
    {
        return ([self enabledRemoteNotificationTypes] & UIRemoteNotificationTypeAlert);
    }
}

- (void)registerForPushNotifications
{
    if ([self respondsToSelector:@selector(registerForRemoteNotifications)])
    {
        [self registerForRemoteNotifications];

        Class uiUserNotificationSettings = NSClassFromString(@"UIUserNotificationSettings");

        //If you want to add other capabilities than just banner alerts, you'll need to grab their declarations from the iOS 8 SDK and define them in the same way.
        NSUInteger UIUserNotificationTypeAlert   = 1 << 2;

        id settings = [uiUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:[NSSet set]];            
        [self registerUserNotificationSettings:settings];

    }
    else
    {
        [self registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
    }
}

@end

关于iOS 8 推送通知声音不适用于此特定案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27558831/

相关文章:

iphone - iOS SDK - 以编程方式生成 PDF 文件

iphone - 开发证书推送通知

iphone - 我无法启用我的推送通知参数

ios - 错误连接APNS服务器

iphone - 使用推送通知在指定页面打开应用程序

ios - 未通过 TestFlight 收到推送通知

ios - 通过命令行从 iOS 设备卸载应用程序

ios - 如何在 iOS 中以编程方式设置 UIToolbar 的样式

ios - UICollectionReusableView header 可能的错误

ios - 更改 #define 参数