Implement Pushkit and Test in Development Behavior

Pushkit voip push notifications are not being received on iOS8.0.2

It only works on iOS8. With this SDK only works on iOS 8.1 (as of writing this it's beta 1). The reason for this is that in 8.0 the compiler linked is a dynlib and is not able to locate push kit framework for 32bits when running on 64bit hardware. But for now you need to compile for armv7 and armv7s.

How to send Push Notifications to test iOS PushKit integration online?

You can create your own php file.

Source https://github.com/hasyapanchasara/PushKit_SilentPushNotification

Use below structure to achieve your task.

Sample Image

Sample Image

Use this simplepush.php file

<?php

// Put your device token here (without spaces):


$deviceToken = '1234567890123456789';
//


// Put your private key's passphrase here:
$passphrase = 'ProjectName';

// Put your alert message here:
$message = 'My first push notification!';



$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'PemFileName.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
// 'ssl://gateway.push.apple.com:2195', $err,
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body

$body['aps'] = array(
'content-available'=> 1,
'alert' => $message,
'sound' => 'default',
'badge' => 0,
);



// Encode the payload as JSON

$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

Use below commands to create pem file and use it in above code

$ openssl x509 -in aps_development.cer -inform der -out PushCert.pem

# Convert .p12 to .pem. Enter your pass pharse which is the same pwd that you have given while creating the .p12 certificate. PEM pass phrase also same as .p12 cert.
$ openssl pkcs12 -nocerts -out PushKey1.pem -in pushkey.p12

Enter Import Password:

MAC verified OK

Enter PEM pass phrase:

Verifying - Enter PEM pass phrase:

# To remove passpharse for the key to access globally. This only solved my stream_socket_client() & certificate capath warnings.
$ openssl rsa -in PushKey1.pem -out PushKey1_Rmv.pem

Enter pass phrase for PushChatKey1.pem:

writing RSA key

# To join the two .pem file into one file:
$ cat PushCert.pem PushKey1_Rmv.pem > ApnsDev.pem

After that go to simplepush.php location and fire command -> php simplepush.php

This way you can test your push kit notification setup architecture.

https://zeropush.com/guide/guide-to-pushkit-and-voip

https://www.raywenderlich.com/123862/push-notifications-tutorial

Download

Updated with Swift 4 code

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

}


}

Unable to receive VoIP notification using PushKit

Finally I fixed this issue.
There is ok for swift source code, the issue is I mis-send the push request to apple's development server gateway.sandbox.push.apple.com, actually we should send request to apple's production server gateway.push.apple.com.

If you are following M Penades' procedure, please note that you need modify Simplepush script, just replace ssl://gateway.sandbox.push.apple.com:2195 by ssl://gateway.push.apple.com:2195 at line 20 and try again.

Hope it works for you.

Pushkit token not getting in some devices iOS

It should work in all devices when integrated properly. there is nothing changed for any device specific.

You can cross verify your steps.

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

Updated answer

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

Updated answer

Below are some trouble shooting option.

(1) Change com identifier and try again

(2) Keep puhkit code in app delegeate

I guess, you can change your bundle identifier and try again, it has to be work with all devices.



Related Topics



Leave a reply



Submit