Pushkit 令牌未进入某些设备 iOS

Posted

技术标签:

【中文标题】Pushkit 令牌未进入某些设备 iOS【英文标题】:Pushkit token not getting in some devices iOS 【发布时间】:2017-07-12 09:50:10 【问题描述】:

我已经实现了PushKit。我已按照以下步骤操作:

1.) https://***.com/a/38184769/4970453

2.) https://***.com/a/28562124

我能够获得didUpdatePushCredentials 设备令牌。 工作在 --> iPhone 5s , iPhone6 Plus

didUpdatePushCredentials 不工作 --> iPhone6 和 iPhone7

我为所有设备使用相同的证书和构建。不知道具体问题。 如果有人遇到过这种问题,请分享解决方法。

我的代码和证书链接

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

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

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

【问题讨论】:

您检查过这些设备的互联网连接吗? 在项目功能中启用 voip 设置后台模式,还请检查设备设置中的应用推送通知设置应该启用。 更改捆绑包标识符对我来说似乎不是解决方案...... 【参考方案1】:

确保在证书页面中,您在 App ID 中启用了推送通知。 也尝试使用 3G/4G 连接。网络配置可能不允许设备获取令牌。

【讨论】:

【参考方案2】:

正确集成后,它应该适用于所有设备。任何特定设备都没有任何变化。

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

斯威夫特

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

    




目标 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,因此任何人都可以下载找出问题和解决方案。 为什么不将 pushRegistry 对象保存在 .h 文件中?另外为什么不在 AppDelegate 中?有什么原因吗? 检查我更新的答案,我对你的代码做了一些更改,下载这个 zip 并检查。【参考方案3】:

要让PushKit 正常工作,我们需要遵循以下步骤:

    在项目功能中启用推送通知 在后台模式下启用远程通知 将设备连接到互联网 允许应用接收推送通知

请参考this step-by-step tutorial。


由于仅在某些设备上发生,可能是第 3 步和第 4 步。

【讨论】:

【参考方案4】:

就我而言,我的某些设备没有收到 VoIP 令牌。这些设备的硬重置解决了我的问题。

【讨论】:

以上是关于Pushkit 令牌未进入某些设备 iOS的主要内容,如果未能解决你的问题,请参考以下文章

设备令牌未生成 iOS

某些设备的令牌无效

解析推送通知:iOS 设备令牌未定义

设备令牌未在 ios 8.1 中生成

突然 fcm 通知在某些 iOS 设备上不起作用

从 APN 迁移到 Firebase 后未重新生成 iOS 设备令牌