一些iOS设备无法获取Pushkit令牌

7
我已经实现了 PushKit。我遵循了以下步骤:
1.) https://dev59.com/Tpbfa4cB1Zd3GeqPzdmy#38184769 2.) https://dev59.com/8l4d5IYBdhLWcg3wDe31#28562124 我能够获取 didUpdatePushCredentials 设备令牌。 在 --> iPhone 5s , iPhone6 Plus 上工作。 didUpdatePushCredentials 在 --> iPhone6 和 iPhone7 上不起作用。
我为所有设备使用相同的证书和构建。不知道确切的问题所在。 如果有人遇到这种问题,请分享解决方法。
我的代码和证书链接

代码 -> https://www.dropbox.com/sh/x2615t7xn8mavs3/AADbX5nBuF5_08YNPX8wI59ga?dl=0

证书 -> https://www.dropbox.com/sh/70l4htj1c46emog/AABxBalaoN1JP22dQp8-mNXGa?dl=0

 Solution -----> I have changed Bundle identifier And create New certificate with New BundleId.

你检查过这些设备的互联网连接了吗? - D4ttatraya
请在项目功能中启用VoIP设置的后台模式,并且还要检查设备设置中的应用程序推送通知设置是否已启用。 - MAhipal Singh
更改捆绑标识符对我来说似乎不是一个解决方案... - rockdaswift
4个回答

1

请确保在证书页面中启用了应用程序ID的推送通知功能。此外,请尝试使用3G / 4G连接。网络配置可能会阻止设备获取令牌。


0
为了使 PushKit 正常工作,我们需要遵循以下步骤:
  1. 在项目功能中启用推送通知
  2. 在后台模式中启用远程通知
  3. 将设备连接到互联网
  4. 允许应用接收推送通知

请参考 这个逐步教程


由于这种情况只发生在某些设备上,可能是第三步和第四步造成的。

0
在我的情况下,我的某些设备没有收到VoIP令牌。对这些设备进行硬重置可以解决我的问题。

0

如果正确集成,它应该在所有设备上都能正常工作,不会对任何特定设备进行更改。

您可以交叉验证您的步骤。

Swift

import UIKit
import PushKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,PKPushRegistryDelegate {

    var window: UIWindow?

    var isUserHasLoggedInWithApp: Bool = true
    var checkForIncomingCall: Bool = true
    var userIsHolding: Bool = true

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


        if #available(iOS 8.0, *){


            let viewAccept = UIMutableUserNotificationAction()
            viewAccept.identifier = "VIEW_ACCEPT"
            viewAccept.title = "Accept"
            viewAccept.activationMode = .Foreground
            viewAccept.destructive = false
            viewAccept.authenticationRequired =  false

            let viewDecline = UIMutableUserNotificationAction()
            viewDecline.identifier = "VIEW_DECLINE"
            viewDecline.title = "Decline"
            viewDecline.activationMode = .Background
            viewDecline.destructive = true
            viewDecline.authenticationRequired = false

            let INCOMINGCALL_CATEGORY = UIMutableUserNotificationCategory()
            INCOMINGCALL_CATEGORY.identifier = "INCOMINGCALL_CATEGORY"
            INCOMINGCALL_CATEGORY.setActions([viewAccept,viewDecline], forContext: .Default)

            if application.respondsToSelector("isRegisteredForRemoteNotifications")
            {
                let categories = NSSet(array: [INCOMINGCALL_CATEGORY])
                let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])

                let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as? Set<UIUserNotificationCategory>)

                application.registerUserNotificationSettings(settings)
                application.registerForRemoteNotifications()
            }

        }
        else{
            let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
            application.registerForRemoteNotificationTypes(types)
        }


        self.PushKitRegistration()

    return true
    }
    //MARK: - PushKitRegistration

    func PushKitRegistration()
    {

        let mainQueue = dispatch_get_main_queue()
        // Create a push registry object
        if #available(iOS 8.0, *) {

        let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)

        // Set the registry's delegate to self

        voipRegistry.delegate = self

        // Set the push type to VoIP

        voipRegistry.desiredPushTypes = [PKPushTypeVoIP]

        } else {
        // Fallback on earlier versions
        }


    }


    @available(iOS 8.0, *)
    func pushRegistry(registry: PKPushRegistry!, didUpdatePushCredentials credentials: PKPushCredentials!, forType type: String!) {
        // Register VoIP push token (a property of PKPushCredentials) with server

        let hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(credentials.token.bytes),
        count: credentials.token.length).map { String(format: "%02x", $0) }.joinWithSeparator("")

        print(hexString)


    }


    @available(iOS 8.0, *)
    func pushRegistry(registry: PKPushRegistry!, didReceiveIncomingPushWithPayload payload: PKPushPayload!, forType type: String!) {

        // Process the received push

        // Below process is specific to schedule local notification once pushkit payload received

        var arrTemp = [NSObject : AnyObject]()
        arrTemp = payload.dictionaryPayload

        let dict : Dictionary <String, AnyObject> = arrTemp["aps"] as! Dictionary<String, AnyObject>


        if isUserHasLoggedInWithApp // Check this flag then only proceed
        {

            if UIApplication.sharedApplication().applicationState == UIApplicationState.Background || UIApplication.sharedApplication().applicationState == UIApplicationState.Inactive
            {

                if checkForIncomingCall // Check this flag to know incoming call or something else
                {

                    var strTitle : String = dict["alertTitle"] as? String ?? ""
                    let strBody : String = dict["alertBody"] as? String ?? ""
                    strTitle = strTitle + "\n" + strBody

                    let notificationIncomingCall = UILocalNotification()

                    notificationIncomingCall.fireDate = NSDate(timeIntervalSinceNow: 1)
                    notificationIncomingCall.alertBody =  strTitle
                    notificationIncomingCall.alertAction = "Open"
                    notificationIncomingCall.soundName = "SoundFile.mp3"
                    notificationIncomingCall.category = dict["category"] as? String ?? ""

                    //"As per payload you receive"
                    notificationIncomingCall.userInfo = ["key1": "Value1"  ,"key2": "Value2" ]


                    UIApplication.sharedApplication().scheduleLocalNotification(notificationIncomingCall)

                }
                else
                {
                    //  something else
                }

            }
        }


    }

    //MARK: - Local Notification Methods

    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification){

        // Your interactive local notification events will be called at this place

    }


}

Objective C

#import <UIKit/UIKit.h>
#import <PushKit/PushKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate,PKPushRegistryDelegate>
{
    PKPushRegistry *pushRegistry;
}

@property (strong, nonatomic) UIWindow *window;

@end

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


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


    pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
    pushRegistry.delegate = self;
    pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

    return YES;
}

#define PushKit Delegate Methods

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
    if([credentials.token length] == 0) {
        NSLog(@"voip token NULL");
        return;
    }

    NSLog(@"PushCredentials: %@", credentials.token);
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type
{
    NSLog(@"didReceiveIncomingPushWithPayload");
}

https://github.com/hasyapanchasara/PushKit_SilentPushNotification

更新的答案

https://drive.google.com/file/d/0B7ooURy3zGWKYW5PZE1aN2pObW8/view?usp=sharing

更新的答案

以下是一些故障排除选项。

(1) 更改 com 标识符,然后重试

(2) 在应用程序委托中保留 puhkit 代码

我猜你可以更改你的捆绑标识符,然后再试一次,这样它就可以在所有设备上运行了。


你可以将项目压缩,只包含VOIP代码的部分,并在问题中提供驱动器URL,这样任何人都可以下载并找到问题和解决方案。 - Hasya
为什么不将pushRegistry对象保存在.h文件中?还有,为什么不保存在AppDelegate中?有什么原因吗? - Hasya
检查我的更新答案,我修改了你的代码,请下载此压缩文件并检查。 - Hasya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接