我在我的 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/