推送iOS 10的通知问题

时间:2016-09-14 12:34:42

标签: ios push-notification ios10

我已经开发了一个应用程序,因为我实现了推送通知。目前它在苹果商店上市。 iOS 9推送工作正常,但iOS 10之后无法正常工作。

代码有什么问题?

6 个答案:

答案 0 :(得分:115)

对于使用xCode 8 GM的iOS 10。

我已经使用xCode 8 GM for iOS 10执行以下步骤解决了我的问题:

1)在目标中,在功能下启用推送通知以添加推送通知权利。

2)在您的应用中实施UserNotifications.framework。在AppDelegate中导入UserNotifications.framework。

#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder   <UIApplicationDelegate,UNUserNotificationCenterDelegate>

@end

3)在didFinishLaunchingWithOptions方法中,分配UIUserNotificationSettings并实施UNUserNotificationCenter委托。

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
         if( !error ){
             [[UIApplication sharedApplication] registerForRemoteNotifications];
         }
     }];  
}

return YES;
}

4)现在终于实现了这两个委托方法。

// ============对于iOS 10 =============

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{

    //Called when a notification is delivered to a foreground app. 

    NSLog(@"Userinfo %@",notification.request.content.userInfo);

    completionHandler(UNNotificationPresentationOptionAlert);
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{

   //Called to let your app know which action was selected by the user for a given notification.

   NSLog(@"Userinfo %@",response.notification.request.content.userInfo);

}

请保留您在iOS 9中使用的代码, 仅使用UserNotifications.framework添加代码行以支持iOS 10的推送通知。

答案 1 :(得分:24)

在iOS 10之前一切正常,在我的情况下,仅功能设置会导致此问题。

必须打开推送通知。

enter image description here

答案 2 :(得分:18)

iOS 10静音推送通知存在问题。在iOS9及更早版本中,发送具有其他数据字段但数据中具有空aps属性的推送通知正常工作。但是在iOS10中,具有空aps属性的推送通知根本没有达到didReceiveRemoteNotification app委托方法,这意味着我的所有静默推送通知(我们在内部用于在应用程序打开时触发操作的通知)已停止在iOS10中工作。

我能够通过在推送通知的aps部分添加至少一个属性而不推送对我的应用的更新来修复此问题,在我的情况下,我刚刚添加了badge: 0和我的静音推送通知在iOS 10中再次开始工作。我希望这有助于其他人!

答案 3 :(得分:7)

@Ashish Shah代码的swift 3版本是:

select to_char(to_date('20160101', 'YYYYMMDD') + level - 1, 'YYYYMMDD') as dt
from   dual
connect by level <= 1 + to_date('20160104', 'YYYYMMDD') - to_date('20160101', 'YYYYMMDD')
;


DT     
--------
20160101
20160102
20160103
20160104

答案 4 :(得分:0)

不要忘记,在测试时,您必须使用sandbox地址才能使用通知。

答案 5 :(得分:-1)

在iOS上,应用程序通过调用registerUserNotificationSettings:的{​​{1}}策略来向客户端授权以获取推送警告。

应用程序调用{​​{1}}(iOS)的UIApplication技术或registerForRemoteNotifications:(OS X)的策略UIApplication

应用程序为registerForRemoteNotificationTypes:(iOS)或NSApplication(OS X)执行application:didRegisterForRemoteNotificationsWithDeviceToken:技术,以获取推送权益产生的一种小工具令牌。

如果注册失败,应用程序会对UIApplicationDelegate(iOS)或NSApplicationDelegate(OS X)执行application:didFailToRegisterForRemoteNotificationsWithError:技术以避免错误。

相关问题