iOS 10 的推送通知问题 [英] Push notification issue with iOS 10

查看:23
本文介绍了iOS 10 的推送通知问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个应用程序,我已经实现了推送通知.目前它在苹果商店上线.到 iOS 9 推送工作正常,但在 iOS 10 之后它不起作用.

I've developed one application in that i've implemented push notification. Currently it's live on apple store. Upto iOS 9 push is working fine but after iOS 10 it is not working.

代码有什么问题?

推荐答案

对于使用 xCode 8 GM 的 iOS 10.

For iOS 10 using xCode 8 GM.

我已使用适用于 iOS 10 的 xCode 8 GM 通过以下步骤解决了我的问题:

I have resolved my issue with following steps using xCode 8 GM for iOS 10:

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

1) In the targets, under Capabilities enable Push Notifications to add Push Notifications Entitlements.

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

2) Implement UserNotifications.framework into your app. Import UserNotifications.framework in your AppDelegate.

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

@end

3) 在 didFinishLaunchingWithOptions 方法中分配 UIUserNotificationSettings 并实现 UNUserNotificationCenter 委托.

3) In didFinishLaunchingWithOptions method assign UIUserNotificationSettings and implement UNUserNotificationCenter delegate.

#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) 现在终于实现了这两个委托方法.

4) Now finally implement this two delegate methods.

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

//============For 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 的推送通知.

Please remain the code as it is you are using for iOS 9, Only add lines of code to support Push notification for iOS 10 using UserNotifications.framework.

这篇关于iOS 10 的推送通知问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆