How to test Push Notification is working in my application
In my application I am implementing the Push Notification Service.
I have a Content Provider server, which contains some products in it. I have generated the SSL Client Certificate and attached i开发者_Go百科t to my development Provisioning profile. This profile is also added to my application. I have written the code inside the delegate methods (guided by Apple guideline for implementing the Push Notification). All set.Now I want to test my application whether it is handing the push notification as per the requirement. Any idea on how can I test it would help me a lot.
Should I have to add new product to the content provider server to test this?If yes, how much will APNS take to send the push notification to my device?
I tried all 3 of the above suggestions with no success. In case someone else is ends up here looking for a solution to this, I found this and it works great:
Pusher https://github.com/noodlewerk/NWPusher
Very cool guide is posted here: Programming Apple Push Notification Services
And an application named PushMeBaby Mac Os App which you can download and use for sending push-notifications to your devices from a Mac.
APNS will send the push notification to your device as soon as your device will become available. If I've not mistaken your device pings APNS every minute.
What about curl
:
curl -d '{"aps":{"alert":"This is a test notification"}}' --cert YourCertificate.pem:YourPassword -H "apns-topic: com.example.yourapp" --http2 https://api.development.push.apple.com/3/device/YourDeviceToken
First you need curl
with http2
support.
And you need to convert your push certificate to pem
format using openssl
.
If you want to support both Certificate and Token based authentication with APNS, then you can try Push Notifications
You should try the the branch of PushMeBaby, it worked for me.
Try this online application, through which you could paste in your device token and provide the certificate, and so send push notification to any devices and lets you customize the data as well. http://pushmebaby.herokuapp.com
You can use APNS tester,its a very good tool for test APNS from Mac Machine link to download this software. 2 things you need to provide to test push notification
1.APNS certificate (.cer file) 2.Device token of user's iOS device
PushMeBaby is frozen when I test iOS10 in Xcode8. Try NWPusher, https://github.com/noodlewerk/NWPusher. A friendly and simple tool with GUI.
If you google you will see a number of websites that do this. I usually use https://www.apnstester.com and https://www.pushty.com
I created a small script to do that
import json
import jwt
import time
from hyper import HTTPConnection
ALGORITHM = 'ES256'
# fill these items
APNS_KEY_ID = ''
TEAM_ID = ''
BUNDLE_ID = ''
# put path to p8 file
APNS_AUTH_KEY = ''
# put device token id (of the notification receiver)
REGISTRATION_ID = ''
# let's do the magic :)
f = open(APNS_AUTH_KEY)
secret = f.read()
token = jwt.encode(
{
'iss': TEAM_ID,
'iat': time.time()
},
secret,
algorithm= ALGORITHM,
headers={
'alg': ALGORITHM,
'kid': APNS_KEY_ID,
}
)
path = '/3/device/{0}'.format(REGISTRATION_ID)
equest_headers = {
'apns-expiration': '0',
'apns-priority': '10',
'apns-topic': BUNDLE_ID,
'authorization': 'bearer {0}'.format(token.decode('ascii'))
}
connection = HTTPConnection('api.development.push.apple.com:443')
# put the payload you need
payload_data = {
'aps': {
'content-available': '1',
},
}
payload = json.dumps(payload_data).encode('utf-8')
connection.request(
'POST',
path,
payload,
headers=request_headers
)
resp = connection.get_response()
print(resp.status)
print(resp.read())
https://gist.github.com/IvanivOleg/7ba4072128b2c05a068a6826be68a3d3
Use terminal to test push notification with single line command
Install Houston in your Mac, run below command in terminal.
gem install houston
If you are facing error like this,
Fetching houston-2.4.0.gem ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
First run below commands in terminal to install Ruby
brew install ruby
export GEM_HOME="$HOME/.gem"
gem install rails
after successful installation run again
gem install houston
Go to the pem files folder and open terminal from that folder.
Run below command like
apn push "Device Token" -c PEM_FILE_NAME -m "MESSAGE"
Like:
apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9" -c myapp_apns_dev.pem -m "Testing"
after successful run of above command it will ask for PEM pass phrase which is password of your pem file.
If your app is lived then use production pem file name
like this,
apn push "5a4b74d5e5fc325b14d2f2641aa11bfb9744d1f88922822a5ed3512376d5f5b9" -c myapp_apns_pro.pem -m "Testing"
Done.
精彩评论